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.
Andrew Clark committed
Aug 7, 2020 at 16:32 UTC
0cd9a6de557f44dcfe79a4f287ca04b05d674bdc
4 files changed
+45
-1
.circleci/config.yml
+20
-1
@@ -19,6 +19,8 @@ aliases:
19
name: Install Packages
20
command: yarn --frozen-lockfile
21
22
+ - &TEST_PARALLELISM 20
23
+
24
- &attach_workspace
25
at: build
26
@@ -92,6 +94,7 @@ jobs:
94
RELEASE_CHANNEL_stable_yarn_test:
95
docker: *docker
96
environment: *environment
97
+ parallelism: *TEST_PARALLELISM
98
99
steps:
100
- checkout
@@ -102,6 +105,7 @@ jobs:
105
yarn_test:
106
docker: *docker
107
environment: *environment
108
+ parallelism: *TEST_PARALLELISM
109
steps:
110
- checkout
111
- *restore_yarn_cache
@@ -111,6 +115,7 @@ jobs:
115
RELEASE_CHANNEL_stable_yarn_test_www:
116
docker: *docker
117
environment: *environment
118
+ parallelism: *TEST_PARALLELISM
119
steps:
120
- checkout
121
- *restore_yarn_cache
@@ -120,6 +125,7 @@ jobs:
125
RELEASE_CHANNEL_stable_yarn_test_www_variant:
126
docker: *docker
127
environment: *environment
128
+ parallelism: *TEST_PARALLELISM
129
steps:
130
- checkout
131
- *restore_yarn_cache
@@ -129,6 +135,7 @@ jobs:
135
RELEASE_CHANNEL_stable_yarn_test_prod_www:
136
docker: *docker
137
environment: *environment
138
+ parallelism: *TEST_PARALLELISM
139
steps:
140
- checkout
141
- *restore_yarn_cache
@@ -138,6 +145,7 @@ jobs:
145
RELEASE_CHANNEL_stable_yarn_test_prod_www_variant:
146
docker: *docker
147
environment: *environment
148
+ parallelism: *TEST_PARALLELISM
149
steps:
150
- checkout
151
- *restore_yarn_cache
@@ -147,6 +155,7 @@ jobs:
155
yarn_test_www:
156
docker: *docker
157
environment: *environment
158
+ parallelism: *TEST_PARALLELISM
159
steps:
160
- checkout
161
- *restore_yarn_cache
@@ -156,6 +165,7 @@ jobs:
165
yarn_test_www_variant:
166
docker: *docker
167
environment: *environment
168
+ parallelism: *TEST_PARALLELISM
169
steps:
170
- checkout
171
- *restore_yarn_cache
@@ -165,6 +175,7 @@ jobs:
175
yarn_test_prod_www:
176
docker: *docker
177
environment: *environment
178
+ parallelism: *TEST_PARALLELISM
179
steps:
180
- checkout
181
- *restore_yarn_cache
@@ -174,6 +185,7 @@ jobs:
185
yarn_test_prod_www_variant:
186
docker: *docker
187
environment: *environment
188
+ parallelism: *TEST_PARALLELISM
189
steps:
190
- checkout
191
- *restore_yarn_cache
@@ -183,6 +195,7 @@ jobs:
195
RELEASE_CHANNEL_stable_yarn_test_persistent:
196
docker: *docker
197
environment: *environment
198
+ parallelism: *TEST_PARALLELISM
199
200
steps:
201
- checkout
@@ -193,6 +206,7 @@ jobs:
206
RELEASE_CHANNEL_stable_yarn_test_prod:
207
docker: *docker
208
environment: *environment
209
+ parallelism: *TEST_PARALLELISM
210
211
steps:
212
- checkout
@@ -203,6 +217,7 @@ jobs:
217
yarn_test_prod:
218
docker: *docker
219
environment: *environment
220
+ parallelism: *TEST_PARALLELISM
221
steps:
222
- checkout
223
- *restore_yarn_cache
@@ -212,7 +227,7 @@ jobs:
227
RELEASE_CHANNEL_stable_yarn_build:
228
docker: *docker
229
environment: *environment
215
- parallelism: 20
230
+ parallelism: *TEST_PARALLELISM
231
steps:
232
- checkout
233
- *restore_yarn_cache
@@ -344,6 +359,7 @@ jobs:
359
RELEASE_CHANNEL_stable_yarn_test_build:
360
docker: *docker
361
environment: *environment
362
+ parallelism: *TEST_PARALLELISM
363
steps:
364
- checkout
365
- attach_workspace: *attach_workspace
@@ -354,6 +370,7 @@ jobs:
370
yarn_test_build:
371
docker: *docker
372
environment: *environment
373
+ parallelism: *TEST_PARALLELISM
374
steps:
375
- checkout
376
- attach_workspace: *attach_workspace
@@ -404,6 +421,7 @@ jobs:
421
RELEASE_CHANNEL_stable_yarn_test_build_prod:
422
docker: *docker
423
environment: *environment
424
+ parallelism: *TEST_PARALLELISM
425
steps:
426
- checkout
427
- attach_workspace: *attach_workspace
@@ -414,6 +432,7 @@ jobs:
432
yarn_test_build_prod:
433
docker: *docker
434
environment: *environment
435
+ parallelism: *TEST_PARALLELISM
436
steps:
437
- checkout
438
- attach_workspace: *attach_workspace
packages/react/src/__tests__/ReactClassEquivalence-test.js
+4
@@ -38,6 +38,10 @@ function runJest(testFile) {
38
cwd,
39
env: Object.assign({}, process.env, {
40
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: '',
45
}),
46
});
47
scripts/jest/config.base.js
+2
@@ -22,6 +22,8 @@ module.exports = {
22
timers: 'fake',
23
snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],
24
25
+ testSequencer: require.resolve('./jestSequencer'),
26
+
27
// TODO: Upgrade to Jest 26 which uses jsdom 16 by default.
28
testEnvironment: require.resolve('jest-environment-jsdom-sixteen'),
29
};
scripts/jest/jestSequencer.js
new
+19
@@ -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;