Skip to content

Commit ed90920

Browse files
authored
feat: support for transforming runner (#8854)
1 parent bc6dc7a commit ed90920

10 files changed

Lines changed: 139 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
77
- `[jest-snapshot]`: [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
88
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
9+
- `[jest-runner]` [**BREAKING**] Run transforms over `runnner` ([#8823](https://github.com/facebook/jest/pull/8823))
910

1011
### Fixes
1112

e2e/__tests__/transform.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,14 @@ describe('transform-environment', () => {
216216
expect(json.numPassedTests).toBe(1);
217217
});
218218
});
219+
220+
describe('transform-runner', () => {
221+
const dir = path.resolve(__dirname, '../transform/transform-runner');
222+
223+
it('should transform runner', () => {
224+
const {json, stderr} = runWithJson(dir, ['--no-cache']);
225+
expect(stderr).toMatch(/PASS/);
226+
expect(json.success).toBe(true);
227+
expect(json.numPassedTests).toBe(1);
228+
});
229+
});

e2e/transform/transform-environment/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"dependencies": {
1010
"@babel/preset-env": "^7.0.0",
1111
"@babel/preset-typescript": "^7.0.0",
12-
"jest-environment-node": "^26.6.2"
12+
"jest-environment-node": "file:../../../packages/jest-environment-node"
1313
}
1414
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
it('should add two numbers', () => {
9+
expect(1 + 1).toBe(2);
10+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
module.exports = {
9+
presets: [
10+
['@babel/preset-env', {targets: {node: 'current'}}],
11+
'@babel/preset-typescript',
12+
],
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"jest": {
3+
"rootDir": "./",
4+
"runner": "<rootDir>/runner.ts"
5+
},
6+
"dependencies": {
7+
"@babel/preset-env": "^7.0.0",
8+
"@babel/preset-typescript": "^7.0.0",
9+
"jest-environment-node": "file:../../../packages/jest-environment-node"
10+
}
11+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 throat from 'throat';
9+
import {TestResult, createEmptyTestResult} from '@jest/test-result';
10+
import type {Config} from '@jest/types';
11+
import {
12+
OnTestFailure,
13+
OnTestStart,
14+
OnTestSuccess,
15+
Test,
16+
TestRunnerContext,
17+
TestWatcher,
18+
} from 'jest-runner';
19+
20+
export default class BaseTestRunner {
21+
private _globalConfig: Config.GlobalConfig;
22+
private _context: TestRunnerContext;
23+
24+
constructor(globalConfig: Config.GlobalConfig, context?: TestRunnerContext) {
25+
this._globalConfig = globalConfig;
26+
this._context = context || {};
27+
}
28+
29+
async runTests(
30+
tests: Array<Test>,
31+
watcher: TestWatcher,
32+
onStart: OnTestStart,
33+
onResult: OnTestSuccess,
34+
onFailure: OnTestFailure,
35+
): Promise<void> {
36+
const mutex = throat(1);
37+
return tests.reduce(
38+
(promise, test) =>
39+
mutex(() =>
40+
promise
41+
.then(
42+
async (): Promise<TestResult> => {
43+
await onStart(test);
44+
return {
45+
...createEmptyTestResult(),
46+
numPassingTests: 1,
47+
testFilePath: test.path,
48+
testResults: [
49+
{
50+
ancestorTitles: [],
51+
duration: 2,
52+
failureMessages: [],
53+
fullName: 'sample test',
54+
location: null,
55+
numPassingAsserts: 1,
56+
status: 'passed',
57+
title: 'sample test',
58+
},
59+
],
60+
};
61+
},
62+
)
63+
.then(result => onResult(test, result))
64+
.catch(err => onFailure(test, err)),
65+
),
66+
Promise.resolve(),
67+
);
68+
}
69+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true,
4+
"module": "commonjs",
5+
}
6+
}

packages/jest-core/src/TestScheduler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
buildFailureTestResult,
2626
makeEmptyAggregatedTestResult,
2727
} from '@jest/test-result';
28+
import {ScriptTransformer} from '@jest/transform';
2829
import type {Config} from '@jest/types';
2930
import {formatExecError} from 'jest-message-util';
3031
import TestRunner = require('jest-runner');
@@ -193,7 +194,10 @@ export default class TestScheduler {
193194
contexts.forEach(context => {
194195
const {config} = context;
195196
if (!testRunners[config.runner]) {
196-
const Runner: typeof TestRunner = require(config.runner);
197+
const transformer = new ScriptTransformer(config);
198+
const Runner: typeof TestRunner = interopRequireDefault(
199+
transformer.requireAndTranspileModule(config.runner),
200+
).default;
197201
const runner = new Runner(this._globalConfig, {
198202
changedFiles: this._context?.changedFiles,
199203
sourcesRelatedToTestsInChangedFiles: this._context

packages/jest-core/src/__tests__/TestScheduler.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ test('schedule tests run in parallel per default', async () => {
8888
const test = {
8989
context: {
9090
config: makeProjectConfig({
91+
moduleFileExtensions: ['.js'],
9192
runner: 'jest-runner-parallel',
93+
transform: [],
9294
}),
9395
hasteFS: {
9496
matchFiles: jest.fn(() => []),
@@ -109,7 +111,9 @@ test('schedule tests run in serial if the runner flags them', async () => {
109111
const test = {
110112
context: {
111113
config: makeProjectConfig({
114+
moduleFileExtensions: ['.js'],
112115
runner: 'jest-runner-serial',
116+
transform: [],
113117
}),
114118
hasteFS: {
115119
matchFiles: jest.fn(() => []),
@@ -130,8 +134,10 @@ test('should bail after `n` failures', async () => {
130134
const test = {
131135
context: {
132136
config: makeProjectConfig({
137+
moduleFileExtensions: ['.js'],
133138
rootDir: './',
134139
runner: 'jest-runner-serial',
140+
transform: [],
135141
}),
136142
hasteFS: {
137143
matchFiles: jest.fn(() => []),
@@ -160,8 +166,10 @@ test('should not bail if less than `n` failures', async () => {
160166
const test = {
161167
context: {
162168
config: makeProjectConfig({
169+
moduleFileExtensions: ['.js'],
163170
rootDir: './',
164171
runner: 'jest-runner-serial',
172+
transform: [],
165173
}),
166174
hasteFS: {
167175
matchFiles: jest.fn(() => []),
@@ -190,7 +198,9 @@ test('should set runInBand to run in serial', async () => {
190198
const test = {
191199
context: {
192200
config: makeProjectConfig({
201+
moduleFileExtensions: ['.js'],
193202
runner: 'jest-runner-parallel',
203+
transform: [],
194204
}),
195205
hasteFS: {
196206
matchFiles: jest.fn(() => []),
@@ -214,7 +224,9 @@ test('should set runInBand to not run in serial', async () => {
214224
const test = {
215225
context: {
216226
config: makeProjectConfig({
227+
moduleFileExtensions: ['.js'],
217228
runner: 'jest-runner-parallel',
229+
transform: [],
218230
}),
219231
hasteFS: {
220232
matchFiles: jest.fn(() => []),

0 commit comments

Comments
 (0)