Skip to content

Commit e84f682

Browse files
authored
feat: sequencer saving may be async (#10980)
1 parent 4102243 commit e84f682

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
1010
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874))
1111
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
12+
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))
1213
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))
1314
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
1415
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))

e2e/__tests__/customTestSequencers.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@ test('run prioritySequence first async', () => {
5959
'./e.test.js',
6060
]);
6161
});
62+
63+
test('run failed tests async', () => {
64+
const result = runJest(
65+
dir,
66+
[
67+
'--onlyFailures',
68+
'-i',
69+
'--config',
70+
JSON.stringify({
71+
testSequencer: '<rootDir>/testSequencerAsync.js',
72+
}),
73+
],
74+
{},
75+
);
76+
expect(result.exitCode).toBe(0);
77+
const sequence = extractSummary(result.stderr)
78+
.rest.replace(/PASS /g, '')
79+
.split('\n');
80+
expect(sequence).toEqual(['./c.test.js', './d.test.js']);
81+
});

e2e/custom-test-sequencer/testSequencerAsync.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

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

10+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
11+
1012
class CustomSequencer extends Sequencer {
11-
sort(tests) {
12-
return new Promise(resolve => {
13-
setTimeout(() => {
14-
const copyTests = Array.from(tests);
15-
resolve(
16-
copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1)),
17-
);
18-
}, 50);
19-
});
13+
async sort(tests) {
14+
await sleep(50);
15+
const copyTests = Array.from(tests);
16+
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
17+
}
18+
19+
async allFailedTests(tests) {
20+
await sleep(50);
21+
return tests.filter(
22+
t => t.path.endsWith('c.test.js') || t.path.endsWith('d.test.js'),
23+
);
2024
}
2125
}
2226

packages/jest-core/src/runJest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default async function runJest({
204204
if (failedTestsCache) {
205205
allTests = failedTestsCache.filterTests(allTests);
206206
} else {
207-
allTests = sequencer.allFailedTests(allTests);
207+
allTests = await sequencer.allFailedTests(allTests);
208208
}
209209
}
210210

@@ -272,7 +272,7 @@ export default async function runJest({
272272
testSchedulerContext,
273273
).scheduleTests(allTests, testWatcher);
274274

275-
sequencer.cacheResults(allTests, results);
275+
await sequencer.cacheResults(allTests, results);
276276

277277
if (hasTests) {
278278
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'});

0 commit comments

Comments
 (0)