Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,11 @@ An example of such function can be found in our default [jasmine2 test runner pa

Default: `@jest/test-sequencer`

This option allows you to use a custom sequencer instead of Jest's default.
This option allows you to use a custom sequencer instead of Jest's default. `sort` may optionally return a Promise.

Example:

Sort test path alphabetically
Sort test path alphabetically.

```js
const Sequencer = require('@jest/test-sequencer').default;
Expand Down
39 changes: 37 additions & 2 deletions e2e/__tests__/customTestSequencers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,43 @@ import runJest from '../runJest';
import {extractSummary} from '../Utils';
const dir = path.resolve(__dirname, '../custom-test-sequencer');

test('run prioritySequence first', () => {
const result = runJest(dir, ['-i']);
test('run prioritySequence first sync', () => {
const result = runJest(
dir,
[
'-i',
'--config',
JSON.stringify({
testSequencer: '<rootDir>/testSequencer.js',
}),
],
{},
);
expect(result.status).toBe(0);
const sequence = extractSummary(result.stderr)
.rest.replace(/PASS /g, '')
.split('\n');
expect(sequence).toEqual([
'./a.test.js',
'./b.test.js',
'./c.test.js',
'./d.test.js',
'./e.test.js',
]);
});

test('run prioritySequence first async', () => {
const result = runJest(
dir,
[
'-i',
'--config',
JSON.stringify({
testSequencer: '<rootDir>/testSequencerAsync.js',
}),
],
{},
);
expect(result.status).toBe(0);
const sequence = extractSummary(result.stderr)
.rest.replace(/PASS /g, '')
Expand Down
3 changes: 1 addition & 2 deletions e2e/custom-test-sequencer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"jest": {
"testEnvironment": "node",
"testSequencer": "<rootDir>/testSequencer.js"
"testEnvironment": "node"
}
}
23 changes: 23 additions & 0 deletions e2e/custom-test-sequencer/testSequencerAsync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

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

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);
});
}
}

module.exports = CustomSequencer;
2 changes: 1 addition & 1 deletion packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default (async function runJest({
}),
);

allTests = sequencer.sort(allTests);
allTests = await sequencer.sort(allTests);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the entire PR, the rest is test and docs.


if (globalConfig.listTests) {
const testsPaths = Array.from(new Set(allTests.map(test => test.path)));
Expand Down