summaryrefslogtreecommitdiff
path: root/scripts/frontend/parallel_ci_sequencer.js
blob: d7a674535a68d2c1329d17520f7f5a298cad36fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Sequencer = require('@jest/test-sequencer').default;

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) {
    const sortedTests = this.sortByPath(tests);
    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;
  }

  sortByPath(tests) {
    return tests.sort((test1, test2) => {
      if (test1.path < test2.path) {
        return -1;
      }
      if (test1.path > test2.path) {
        return 1;
      }
      return 0;
    });
  }

  distributeAcrossCINodes(tests) {
    return tests.filter((test, index) => {
      return index % this.ciNodeTotal === this.ciNodeIndex - 1;
    });
  }
}

module.exports = ParallelCISequencer;