@split-tests/jest
Version:
Splits test files in Jest and ensures all parallel jobs finish work at a similar time
73 lines (67 loc) • 2.85 kB
JavaScript
;
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
const Sequencer = _interopDefault(require('@jest/test-sequencer'));
const core = require('@split-tests/core');
const path = _interopDefault(require('path'));
function junitAdapter(globalConfig, options) {
const rootDir = globalConfig.rootDir;
const junitPath = options.junit;
const junitFile = path.resolve(junitPath.replace("<rootDir>", rootDir));
const junit = core.loadXML(junitFile);
const summary = junit.testsuites;
return summary.testsuite.map((t) => {
const file = core.findFilenameInJUnit(t);
return {
path: path.resolve(rootDir, file),
time: parseFloat(t.time),
};
});
}
function getArg(name) {
const arg = process.argv.find((val) => val.startsWith(`--${name}`));
if (arg) {
return arg.replace(`--${name}=`, "");
}
}
class JobSequencer extends Sequencer {
// @ts-ignore
async sort(tests) {
if (process.env.JEST_JOBS_TOTAL || getArg("jobsTotal")) {
let total = parseInt(process.env.JEST_JOBS_TOTAL || getArg("jobsTotal"), 10);
let index = parseInt(process.env.JEST_JOBS_INDEX || getArg("jobsIndex"), 10);
if (process.env.JEST_NORMAL) {
return tests
.sort((a, b) => (a.path < b.path ? -1 : 1))
.filter((_, i) => i % total === index);
}
const config = tests[0].context.config;
let reports = [];
const normalizedTests = tests.sort((a, b) => (a.path < b.path ? -1 : 1));
if (config.globals && config.globals["split-tests"]) {
const options = config.globals["split-tests"];
if (options.junit) {
reports = junitAdapter(config, options);
}
else if (typeof options.reader === "function") {
// we might need to use absolute paths here
reports = await options.reader(normalizedTests);
}
if (options.jobs) {
if (options.jobs.total) {
total = options.jobs.total();
}
if (options.jobs.index) {
index = options.jobs.index();
}
}
}
reports = core.removeDeletedFiles(reports, normalizedTests);
reports = core.addNewFiles(reports, normalizedTests);
const groups = core.distribute(reports, total);
return groups[index].files.map((testFile) => normalizedTests.find((t) => t.path === testFile));
}
return tests;
}
}
module.exports = JobSequencer;
//# sourceMappingURL=index.cjs.js.map