2020-02-26 04:08:47 -05:00
|
|
|
const Sequencer = require('@jest/test-sequencer').default;
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
const sortByPath = (test1, test2) => {
|
|
|
|
if (test1.path < test2.path) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (test1.path > test2.path) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2020-02-26 04:08:47 -05:00
|
|
|
class ParallelCISequencer extends Sequencer {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.ciNodeIndex = Number(process.env.CI_NODE_INDEX || '1');
|
|
|
|
this.ciNodeTotal = Number(process.env.CI_NODE_TOTAL || '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(tests) {
|
2021-03-10 10:09:11 -05:00
|
|
|
const sortedTests = [...tests].sort(sortByPath);
|
2020-02-26 04:08:47 -05:00
|
|
|
const testsForThisRunner = this.distributeAcrossCINodes(sortedTests);
|
|
|
|
|
|
|
|
console.log(`CI_NODE_INDEX: ${this.ciNodeIndex}`);
|
|
|
|
console.log(`CI_NODE_TOTAL: ${this.ciNodeTotal}`);
|
|
|
|
console.log(`Total number of tests: ${tests.length}`);
|
|
|
|
console.log(`Total number of tests for this runner: ${testsForThisRunner.length}`);
|
|
|
|
|
|
|
|
return testsForThisRunner;
|
|
|
|
}
|
|
|
|
|
|
|
|
distributeAcrossCINodes(tests) {
|
|
|
|
return tests.filter((test, index) => {
|
|
|
|
return index % this.ciNodeTotal === this.ciNodeIndex - 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ParallelCISequencer;
|