Skip to content

Commit

Permalink
feat(jest-core): Add support for testSequencer written in ESM (#11207)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiAnAn committed Mar 20, 2021
1 parent 0e2d455 commit 2d96526
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
- `[jest-core]` Run failed tests interactively the same way we do with snapshots ([#10858](https://github.com/facebook/jest/pull/10858))
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))
- `[jest-core]` Add support for `testSequencer` written in ESM ([#11207](https://github.com/facebook/jest/pull/11207))
- `[jest-environment-node]` Add AbortController to globals ([#11182](https://github.com/facebook/jest/pull/11182))
- `[@jest/fake-timers]` Update to `@sinonjs/fake-timers` to v7 ([#11198](https://github.com/facebook/jest/pull/11198))
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))
Expand Down
32 changes: 32 additions & 0 deletions e2e/__tests__/customEsmTestSequencers.test.ts
@@ -0,0 +1,32 @@
/**
* 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.
*/

import * as path from 'path';
import {onNodeVersions} from '@jest/test-utils';
import {extractSummary} from '../Utils';
import runJest from '../runJest';
const dir = path.resolve(__dirname, '../custom-esm-test-sequencer');

onNodeVersions('^12.16.0 || >=13.7.0', () => {
test('run prioritySequence', () => {
const result = runJest(dir, ['-i'], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});

expect(result.exitCode).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',
]);
});
});
7 changes: 7 additions & 0 deletions e2e/custom-esm-test-sequencer/a.test.js
@@ -0,0 +1,7 @@
/**
* 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.
*/
test('a', () => {});
7 changes: 7 additions & 0 deletions e2e/custom-esm-test-sequencer/b.test.js
@@ -0,0 +1,7 @@
/**
* 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.
*/
test('b', () => {});
7 changes: 7 additions & 0 deletions e2e/custom-esm-test-sequencer/c.test.js
@@ -0,0 +1,7 @@
/**
* 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.
*/
test('c', () => {});
7 changes: 7 additions & 0 deletions e2e/custom-esm-test-sequencer/d.test.js
@@ -0,0 +1,7 @@
/**
* 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.
*/
test('d', () => {});
7 changes: 7 additions & 0 deletions e2e/custom-esm-test-sequencer/e.test.js
@@ -0,0 +1,7 @@
/**
* 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.
*/
test('e', () => {});
8 changes: 8 additions & 0 deletions e2e/custom-esm-test-sequencer/package.json
@@ -0,0 +1,8 @@
{
"type": "module",
"jest": {
"testEnvironment": "node",
"transform": {},
"testSequencer": "<rootDir>/testSequencer.mjs"
}
}
15 changes: 15 additions & 0 deletions e2e/custom-esm-test-sequencer/testSequencer.mjs
@@ -0,0 +1,15 @@
/**
* 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.
*/

import Sequencer from '@jest/test-sequencer';

export default class CustomSequencer extends Sequencer.default {
sort(tests) {
const copyTests = Array.from(tests);
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
}
}
8 changes: 4 additions & 4 deletions packages/jest-core/src/runJest.ts
Expand Up @@ -20,7 +20,7 @@ import type {Config} from '@jest/types';
import type {ChangedFiles, ChangedFilesPromise} from 'jest-changed-files';
import type {Test} from 'jest-runner';
import type {Context} from 'jest-runtime';
import {interopRequireDefault, tryRealpath} from 'jest-util';
import {requireOrImportModule, tryRealpath} from 'jest-util';
import {JestHook, JestHookEmitter} from 'jest-watcher';
import type FailedTestsCache from './FailedTestsCache';
import SearchSource from './SearchSource';
Expand Down Expand Up @@ -142,9 +142,9 @@ export default async function runJest({
failedTestsCache?: FailedTestsCache;
filter?: Filter;
}): Promise<void> {
const Sequencer: typeof TestSequencer = interopRequireDefault(
require(globalConfig.testSequencer),
).default;
const Sequencer: typeof TestSequencer = await requireOrImportModule(
globalConfig.testSequencer,
);
const sequencer = new Sequencer();
let allTests: Array<Test> = [];

Expand Down

0 comments on commit 2d96526

Please sign in to comment.