Skip to content

Commit

Permalink
Parallelize Jest in CI
Browse files Browse the repository at this point in the history
Uses CircleCI's `parallelism` key to split our test jobs across multiple
processes, like we do for the build job.
  • Loading branch information
acdlite committed Aug 6, 2020
1 parent 32ff428 commit f2b073c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
20 changes: 19 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ aliases:
name: Install Packages
command: yarn --frozen-lockfile

- &TEST_PARALLELISM 20

- &attach_workspace
at: build

Expand Down Expand Up @@ -102,6 +104,7 @@ jobs:
yarn_test:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -111,6 +114,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_www:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -120,6 +124,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_www_variant:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -129,6 +134,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_prod_www:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -138,6 +144,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_prod_www_variant:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -147,6 +154,7 @@ jobs:
yarn_test_www:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -156,6 +164,7 @@ jobs:
yarn_test_www_variant:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -165,6 +174,7 @@ jobs:
yarn_test_prod_www:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -174,6 +184,7 @@ jobs:
yarn_test_prod_www_variant:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -183,6 +194,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_persistent:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM

steps:
- checkout
Expand All @@ -193,6 +205,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_prod:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM

steps:
- checkout
Expand All @@ -203,6 +216,7 @@ jobs:
yarn_test_prod:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand All @@ -212,7 +226,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_build:
docker: *docker
environment: *environment
parallelism: 20
parallelism: *TEST_PARALLELISM
steps:
- checkout
- *restore_yarn_cache
Expand Down Expand Up @@ -344,6 +358,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_build:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- attach_workspace: *attach_workspace
Expand All @@ -354,6 +369,7 @@ jobs:
yarn_test_build:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- attach_workspace: *attach_workspace
Expand Down Expand Up @@ -404,6 +420,7 @@ jobs:
RELEASE_CHANNEL_stable_yarn_test_build_prod:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- attach_workspace: *attach_workspace
Expand All @@ -414,6 +431,7 @@ jobs:
yarn_test_build_prod:
docker: *docker
environment: *environment
parallelism: *TEST_PARALLELISM
steps:
- checkout
- attach_workspace: *attach_workspace
Expand Down
11 changes: 8 additions & 3 deletions packages/react/src/__tests__/ReactClassEquivalence-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ function runJest(testFile) {
'Expected this test to run as a result of one of test commands.',
);
}

const env = Object.assign({}, process.env);
// Remove these so that the test file is not filtered out by the mechanism
// we use to parallelize tests in CI
delete env.CIRCLE_NODE_TOTAL;
delete env.CIRCLE_NODE_INDEX;

const result = spawnSync('yarn' + extension, [command, testFile], {
cwd,
env: Object.assign({}, process.env, {
REACT_CLASS_EQUIVALENCE_TEST: 'true',
}),
env,
});

if (result.error) {
Expand Down
2 changes: 2 additions & 0 deletions scripts/jest/config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
timers: 'fake',
snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],

testSequencer: require.resolve('./jestSequencer'),

// TODO: Upgrade to Jest 26 which uses jsdom 16 by default.
testEnvironment: require.resolve('jest-environment-jsdom-sixteen'),
};
19 changes: 19 additions & 0 deletions scripts/jest/jestSequencer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const Sequencer = require('@jest/test-sequencer').default;

class CustomSequencer extends Sequencer {
sort(tests) {
if (process.env.CIRCLE_NODE_TOTAL) {
// In CI, parallelize tests across multiple tasks.
const nodeTotal = parseInt(process.env.CIRCLE_NODE_TOTAL, 10);
const nodeIndex = parseInt(process.env.CIRCLE_NODE_INDEX, 10);
tests = tests
.sort((a, b) => (a.path < b.path ? -1 : 1))
.filter((_, i) => i % nodeTotal === nodeIndex);
}
return tests;
}
}

module.exports = CustomSequencer;

0 comments on commit f2b073c

Please sign in to comment.