@zebrunner/javascript-agent-mocha
Version:
Zebrunner Agent for Mocha
190 lines (143 loc) • 4.99 kB
JavaScript
const { IPC_EVENTS } = require('./events');
const { connectIPCClient, publishIPCEvent } = require('./ipc-client');
const { parseTestInfo } = require('./object-transformer');
const {
isCurrentTestPresent,
isBlankString,
isNotEmptyArray,
isBuffer,
getBase64Encode,
getMimeTypeFromPath,
} = require('./utils');
const CurrentTest = {
initLogging: (test) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified');
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
// Mocha provides worker ID for parallel tests in this env variable
const testPid = process.env.MOCHA_WORKER_ID;
console.log(`Zebrunner: initLogging = ${testPid} for test = ${testInfo.title}`);
if (testPid) {
testInfo.pid = testPid;
publishIPCEvent(IPC_EVENTS.SAVE_TEST_WORKER_ID, { testInfo });
}
},
setMaintainer: (test, maintainer) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
if (isBlankString(maintainer)) {
console.log(`Maintainer must be a not blank string. Provided value is '${maintainer}'`);
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
publishIPCEvent(IPC_EVENTS.SET_TEST_MAINTAINER, { testInfo, maintainer });
},
attachLabel: (test, key, ...values) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
if (isBlankString(key)) {
console.log(`Label key must be a not blank string. Provided value is '${key}'`);
return;
}
if (!isNotEmptyArray(values)) {
console.log('You must provide at least one label value');
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
const labels = values.filter((value) => !isBlankString(value)).map((value) => ({ key, value }));
if (isNotEmptyArray(labels)) {
publishIPCEvent(IPC_EVENTS.ATTACH_TEST_LABELS, { testInfo, labels });
}
},
attachArtifactReference: (test, name, value) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
if (isBlankString(name)) {
console.log(
`Artifact reference name must be a not blank string. Provided value is '${name}'`,
);
return;
}
if (isBlankString(value)) {
console.log(
`Artifact reference value must be a not blank string. Provided value for name '${name}' is '${value}'`,
);
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
const references = [{ name, value }];
publishIPCEvent(IPC_EVENTS.ATTACH_TEST_ARTIFACT_REFERENCES, { testInfo, references });
},
uploadArtifactBuffer: (test, name, type, buffer) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
if (isBlankString(name)) {
console.log(`Artifact name must be a not blank string. Provided value is '${name}'`);
return;
}
if (isBlankString(type)) {
console.log(`Artifact type value must be a not blank string. Provided value is '${type}'`);
return;
}
if (!isBuffer(buffer)) {
console.log('Artifact buffer is incorrect');
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
const file = {
name,
type,
content: buffer,
};
publishIPCEvent(IPC_EVENTS.UPLOAD_TEST_ARTIFACT, { testInfo, file });
},
uploadArtifactFromFile: (test, name, filePath) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
if (isBlankString(name)) {
console.log(`Artifact name must be a not blank string. Provided value is '${name}'`);
return;
}
const buffer = getBase64Encode(filePath);
const mimeType = getMimeTypeFromPath(filePath);
if (isBlankString(buffer) || isBlankString(mimeType)) {
console.log(`Artifact file path '${filePath}' is incorrect or file not found`);
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
const file = {
name,
type: mimeType,
content: buffer,
};
publishIPCEvent(IPC_EVENTS.UPLOAD_TEST_ARTIFACT, { testInfo, file });
},
revertRegistration: (test) => {
if (!isCurrentTestPresent(test)) {
console.log('`test` object of the current test must be specified as the first argument');
return;
}
connectIPCClient();
const testInfo = parseTestInfo(test);
publishIPCEvent(IPC_EVENTS.REVERT_TEST_REGISTRATION, { testInfo });
},
};
module.exports = CurrentTest;