Skip to content

Commit 0cd9a6d

Browse files
authored
Parallelize Jest in CI (#19552)
Uses CircleCI's `parallelism` key to split our test jobs across multiple processes, like we do for the build job.
1 parent 2d9ec91 commit 0cd9a6d

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

.circleci/config.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ aliases:
1919
name: Install Packages
2020
command: yarn --frozen-lockfile
2121

22+
- &TEST_PARALLELISM 20
23+
2224
- &attach_workspace
2325
at: build
2426

@@ -92,6 +94,7 @@ jobs:
9294
RELEASE_CHANNEL_stable_yarn_test:
9395
docker: *docker
9496
environment: *environment
97+
parallelism: *TEST_PARALLELISM
9598

9699
steps:
97100
- checkout
@@ -102,6 +105,7 @@ jobs:
102105
yarn_test:
103106
docker: *docker
104107
environment: *environment
108+
parallelism: *TEST_PARALLELISM
105109
steps:
106110
- checkout
107111
- *restore_yarn_cache
@@ -111,6 +115,7 @@ jobs:
111115
RELEASE_CHANNEL_stable_yarn_test_www:
112116
docker: *docker
113117
environment: *environment
118+
parallelism: *TEST_PARALLELISM
114119
steps:
115120
- checkout
116121
- *restore_yarn_cache
@@ -120,6 +125,7 @@ jobs:
120125
RELEASE_CHANNEL_stable_yarn_test_www_variant:
121126
docker: *docker
122127
environment: *environment
128+
parallelism: *TEST_PARALLELISM
123129
steps:
124130
- checkout
125131
- *restore_yarn_cache
@@ -129,6 +135,7 @@ jobs:
129135
RELEASE_CHANNEL_stable_yarn_test_prod_www:
130136
docker: *docker
131137
environment: *environment
138+
parallelism: *TEST_PARALLELISM
132139
steps:
133140
- checkout
134141
- *restore_yarn_cache
@@ -138,6 +145,7 @@ jobs:
138145
RELEASE_CHANNEL_stable_yarn_test_prod_www_variant:
139146
docker: *docker
140147
environment: *environment
148+
parallelism: *TEST_PARALLELISM
141149
steps:
142150
- checkout
143151
- *restore_yarn_cache
@@ -147,6 +155,7 @@ jobs:
147155
yarn_test_www:
148156
docker: *docker
149157
environment: *environment
158+
parallelism: *TEST_PARALLELISM
150159
steps:
151160
- checkout
152161
- *restore_yarn_cache
@@ -156,6 +165,7 @@ jobs:
156165
yarn_test_www_variant:
157166
docker: *docker
158167
environment: *environment
168+
parallelism: *TEST_PARALLELISM
159169
steps:
160170
- checkout
161171
- *restore_yarn_cache
@@ -165,6 +175,7 @@ jobs:
165175
yarn_test_prod_www:
166176
docker: *docker
167177
environment: *environment
178+
parallelism: *TEST_PARALLELISM
168179
steps:
169180
- checkout
170181
- *restore_yarn_cache
@@ -174,6 +185,7 @@ jobs:
174185
yarn_test_prod_www_variant:
175186
docker: *docker
176187
environment: *environment
188+
parallelism: *TEST_PARALLELISM
177189
steps:
178190
- checkout
179191
- *restore_yarn_cache
@@ -183,6 +195,7 @@ jobs:
183195
RELEASE_CHANNEL_stable_yarn_test_persistent:
184196
docker: *docker
185197
environment: *environment
198+
parallelism: *TEST_PARALLELISM
186199

187200
steps:
188201
- checkout
@@ -193,6 +206,7 @@ jobs:
193206
RELEASE_CHANNEL_stable_yarn_test_prod:
194207
docker: *docker
195208
environment: *environment
209+
parallelism: *TEST_PARALLELISM
196210

197211
steps:
198212
- checkout
@@ -203,6 +217,7 @@ jobs:
203217
yarn_test_prod:
204218
docker: *docker
205219
environment: *environment
220+
parallelism: *TEST_PARALLELISM
206221
steps:
207222
- checkout
208223
- *restore_yarn_cache
@@ -212,7 +227,7 @@ jobs:
212227
RELEASE_CHANNEL_stable_yarn_build:
213228
docker: *docker
214229
environment: *environment
215-
parallelism: 20
230+
parallelism: *TEST_PARALLELISM
216231
steps:
217232
- checkout
218233
- *restore_yarn_cache
@@ -344,6 +359,7 @@ jobs:
344359
RELEASE_CHANNEL_stable_yarn_test_build:
345360
docker: *docker
346361
environment: *environment
362+
parallelism: *TEST_PARALLELISM
347363
steps:
348364
- checkout
349365
- attach_workspace: *attach_workspace
@@ -354,6 +370,7 @@ jobs:
354370
yarn_test_build:
355371
docker: *docker
356372
environment: *environment
373+
parallelism: *TEST_PARALLELISM
357374
steps:
358375
- checkout
359376
- attach_workspace: *attach_workspace
@@ -404,6 +421,7 @@ jobs:
404421
RELEASE_CHANNEL_stable_yarn_test_build_prod:
405422
docker: *docker
406423
environment: *environment
424+
parallelism: *TEST_PARALLELISM
407425
steps:
408426
- checkout
409427
- attach_workspace: *attach_workspace
@@ -414,6 +432,7 @@ jobs:
414432
yarn_test_build_prod:
415433
docker: *docker
416434
environment: *environment
435+
parallelism: *TEST_PARALLELISM
417436
steps:
418437
- checkout
419438
- attach_workspace: *attach_workspace

packages/react/src/__tests__/ReactClassEquivalence-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function runJest(testFile) {
3838
cwd,
3939
env: Object.assign({}, process.env, {
4040
REACT_CLASS_EQUIVALENCE_TEST: 'true',
41+
// Remove these so that the test file is not filtered out by the mechanism
42+
// we use to parallelize tests in CI
43+
CIRCLE_NODE_TOTAL: '',
44+
CIRCLE_NODE_INDEX: '',
4145
}),
4246
});
4347

scripts/jest/config.base.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module.exports = {
2222
timers: 'fake',
2323
snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],
2424

25+
testSequencer: require.resolve('./jestSequencer'),
26+
2527
// TODO: Upgrade to Jest 26 which uses jsdom 16 by default.
2628
testEnvironment: require.resolve('jest-environment-jsdom-sixteen'),
2729
};

scripts/jest/jestSequencer.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const Sequencer = require('@jest/test-sequencer').default;
4+
5+
class CustomSequencer extends Sequencer {
6+
sort(tests) {
7+
if (process.env.CIRCLE_NODE_TOTAL) {
8+
// In CI, parallelize tests across multiple tasks.
9+
const nodeTotal = parseInt(process.env.CIRCLE_NODE_TOTAL, 10);
10+
const nodeIndex = parseInt(process.env.CIRCLE_NODE_INDEX, 10);
11+
tests = tests
12+
.sort((a, b) => (a.path < b.path ? -1 : 1))
13+
.filter((_, i) => i % nodeTotal === nodeIndex);
14+
}
15+
return tests;
16+
}
17+
}
18+
19+
module.exports = CustomSequencer;

0 commit comments

Comments
 (0)