Skip to content

Commit 2d96526

Browse files
authored
feat(jest-core): Add support for testSequencer written in ESM (#11207)
1 parent 0e2d455 commit 2d96526

File tree

10 files changed

+95
-4
lines changed

10 files changed

+95
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
1515
- `[jest-core]` Run failed tests interactively the same way we do with snapshots ([#10858](https://github.com/facebook/jest/pull/10858))
1616
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))
17+
- `[jest-core]` Add support for `testSequencer` written in ESM ([#11207](https://github.com/facebook/jest/pull/11207))
1718
- `[jest-environment-node]` Add AbortController to globals ([#11182](https://github.com/facebook/jest/pull/11182))
1819
- `[@jest/fake-timers]` Update to `@sinonjs/fake-timers` to v7 ([#11198](https://github.com/facebook/jest/pull/11198))
1920
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import {onNodeVersions} from '@jest/test-utils';
10+
import {extractSummary} from '../Utils';
11+
import runJest from '../runJest';
12+
const dir = path.resolve(__dirname, '../custom-esm-test-sequencer');
13+
14+
onNodeVersions('^12.16.0 || >=13.7.0', () => {
15+
test('run prioritySequence', () => {
16+
const result = runJest(dir, ['-i'], {
17+
nodeOptions: '--experimental-vm-modules --no-warnings',
18+
});
19+
20+
expect(result.exitCode).toBe(0);
21+
const sequence = extractSummary(result.stderr)
22+
.rest.replace(/PASS /g, '')
23+
.split('\n');
24+
expect(sequence).toEqual([
25+
'./a.test.js',
26+
'./b.test.js',
27+
'./c.test.js',
28+
'./d.test.js',
29+
'./e.test.js',
30+
]);
31+
});
32+
});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
test('a', () => {});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
test('b', () => {});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
test('c', () => {});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
test('d', () => {});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
test('e', () => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"testEnvironment": "node",
5+
"transform": {},
6+
"testSequencer": "<rootDir>/testSequencer.mjs"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import Sequencer from '@jest/test-sequencer';
9+
10+
export default class CustomSequencer extends Sequencer.default {
11+
sort(tests) {
12+
const copyTests = Array.from(tests);
13+
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
14+
}
15+
}

packages/jest-core/src/runJest.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {Config} from '@jest/types';
2020
import type {ChangedFiles, ChangedFilesPromise} from 'jest-changed-files';
2121
import type {Test} from 'jest-runner';
2222
import type {Context} from 'jest-runtime';
23-
import {interopRequireDefault, tryRealpath} from 'jest-util';
23+
import {requireOrImportModule, tryRealpath} from 'jest-util';
2424
import {JestHook, JestHookEmitter} from 'jest-watcher';
2525
import type FailedTestsCache from './FailedTestsCache';
2626
import SearchSource from './SearchSource';
@@ -142,9 +142,9 @@ export default async function runJest({
142142
failedTestsCache?: FailedTestsCache;
143143
filter?: Filter;
144144
}): Promise<void> {
145-
const Sequencer: typeof TestSequencer = interopRequireDefault(
146-
require(globalConfig.testSequencer),
147-
).default;
145+
const Sequencer: typeof TestSequencer = await requireOrImportModule(
146+
globalConfig.testSequencer,
147+
);
148148
const sequencer = new Sequencer();
149149
let allTests: Array<Test> = [];
150150

0 commit comments

Comments
 (0)