UNPKG

1.49 kBJavaScriptView Raw
1/* @flow */
2/* eslint-env node */
3
4// Based off https://labs.contactually.com/parallelizing-jest-across-multiple-ci-containers-551e7d3e4cb0
5
6const glob = require('globby');
7const {testPathIgnorePatterns} = require('./jest.config.js');
8
9/**
10 * writes a list of files matching the glob pattern to stdout
11 * runs only the subset of files which fall within the job, set
12 * in the environment variables.
13 *
14 * JOB_COUNT is the number of jobs we will be splitting across, 1-indexed
15 * JOB_INDEX is the index of the job (subset of files) we should be running, 0-indexed
16 */
17const {JOB_COUNT = 1, JOB_INDEX = 0} /*: any */ = process.env;
18
19/**
20 * gets a list of files matching the given glob
21 * @returns {string}
22 */
23function getFiles() /*: Array<string> */ {
24 const allFiles = glob.sync(
25 [
26 '**/test.js',
27 '**/*.spec.js',
28 '**/*.test.js',
29 ...testPathIgnorePatterns.map(pattern => `!${pattern}`),
30 ],
31 {
32 gitignore: true,
33 ignore: ['node_modules'],
34 }
35 );
36
37 const filesPerJob = Math.floor(allFiles.length / JOB_COUNT);
38 const startIndex = filesPerJob * JOB_INDEX;
39 if (JOB_INDEX == JOB_COUNT - 1) {
40 return allFiles.slice(startIndex);
41 } else {
42 return allFiles.slice(startIndex, startIndex + filesPerJob);
43 }
44}
45
46const files = getFiles();
47
48/**
49 * Join the array of files is into a single string,
50 * a new glob pattern which will match all of the
51 * selected files, exclusively.
52 */
53process.stdout.write(files.map(str => '.*/' + str).join('|'));