Skip to content

Commit

Permalink
feat: sequencer saving may be async (#10980)
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Dec 26, 2020
1 parent 4102243 commit e84f682
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874))
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
Expand Down
20 changes: 20 additions & 0 deletions e2e/__tests__/customTestSequencers.test.ts
Expand Up @@ -59,3 +59,23 @@ test('run prioritySequence first async', () => {
'./e.test.js',
]);
});

test('run failed tests async', () => {
const result = runJest(
dir,
[
'--onlyFailures',
'-i',
'--config',
JSON.stringify({
testSequencer: '<rootDir>/testSequencerAsync.js',
}),
],
{},
);
expect(result.exitCode).toBe(0);
const sequence = extractSummary(result.stderr)
.rest.replace(/PASS /g, '')
.split('\n');
expect(sequence).toEqual(['./c.test.js', './d.test.js']);
});
22 changes: 13 additions & 9 deletions e2e/custom-test-sequencer/testSequencerAsync.js
Expand Up @@ -7,16 +7,20 @@

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

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

class CustomSequencer extends Sequencer {
sort(tests) {
return new Promise(resolve => {
setTimeout(() => {
const copyTests = Array.from(tests);
resolve(
copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1)),
);
}, 50);
});
async sort(tests) {
await sleep(50);
const copyTests = Array.from(tests);
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
}

async allFailedTests(tests) {
await sleep(50);
return tests.filter(
t => t.path.endsWith('c.test.js') || t.path.endsWith('d.test.js'),
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/runJest.ts
Expand Up @@ -204,7 +204,7 @@ export default async function runJest({
if (failedTestsCache) {
allTests = failedTestsCache.filterTests(allTests);
} else {
allTests = sequencer.allFailedTests(allTests);
allTests = await sequencer.allFailedTests(allTests);
}
}

Expand Down Expand Up @@ -272,7 +272,7 @@ export default async function runJest({
testSchedulerContext,
).scheduleTests(allTests, testWatcher);

sequencer.cacheResults(allTests, results);
await sequencer.cacheResults(allTests, results);

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

0 comments on commit e84f682

Please sign in to comment.