diff --git a/CHANGELOG.md b/CHANGELOG.md index d9ad8d3e929b..e2d47d1a5316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728)) - `[jest-snapshot]`: [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792)) - `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751)) +- `[jest-runner]` [**BREAKING**] Run transforms over `runnner` ([#8823](https://github.com/facebook/jest/pull/8823)) ### Fixes diff --git a/e2e/__tests__/transform.test.ts b/e2e/__tests__/transform.test.ts index be5b7bf2735c..956a258b4d2d 100644 --- a/e2e/__tests__/transform.test.ts +++ b/e2e/__tests__/transform.test.ts @@ -216,3 +216,14 @@ describe('transform-environment', () => { expect(json.numPassedTests).toBe(1); }); }); + +describe('transform-runner', () => { + const dir = path.resolve(__dirname, '../transform/transform-runner'); + + it('should transform runner', () => { + const {json, stderr} = runWithJson(dir, ['--no-cache']); + expect(stderr).toMatch(/PASS/); + expect(json.success).toBe(true); + expect(json.numPassedTests).toBe(1); + }); +}); diff --git a/e2e/transform/transform-environment/package.json b/e2e/transform/transform-environment/package.json index e1104c0a57f9..c34e478bb8aa 100644 --- a/e2e/transform/transform-environment/package.json +++ b/e2e/transform/transform-environment/package.json @@ -9,6 +9,6 @@ "dependencies": { "@babel/preset-env": "^7.0.0", "@babel/preset-typescript": "^7.0.0", - "jest-environment-node": "^26.6.2" + "jest-environment-node": "file:../../../packages/jest-environment-node" } } diff --git a/e2e/transform/transform-runner/__tests__/add.test.js b/e2e/transform/transform-runner/__tests__/add.test.js new file mode 100644 index 000000000000..10dca79b8672 --- /dev/null +++ b/e2e/transform/transform-runner/__tests__/add.test.js @@ -0,0 +1,10 @@ +/** + * 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. + */ + +it('should add two numbers', () => { + expect(1 + 1).toBe(2); +}); diff --git a/e2e/transform/transform-runner/babel.config.js b/e2e/transform/transform-runner/babel.config.js new file mode 100644 index 000000000000..19dd8862b516 --- /dev/null +++ b/e2e/transform/transform-runner/babel.config.js @@ -0,0 +1,13 @@ +/** + * 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. + */ + +module.exports = { + presets: [ + ['@babel/preset-env', {targets: {node: 'current'}}], + '@babel/preset-typescript', + ], +}; diff --git a/e2e/transform/transform-runner/package.json b/e2e/transform/transform-runner/package.json new file mode 100644 index 000000000000..eea777afc0df --- /dev/null +++ b/e2e/transform/transform-runner/package.json @@ -0,0 +1,11 @@ +{ + "jest": { + "rootDir": "./", + "runner": "/runner.ts" + }, + "dependencies": { + "@babel/preset-env": "^7.0.0", + "@babel/preset-typescript": "^7.0.0", + "jest-environment-node": "file:../../../packages/jest-environment-node" + } +} diff --git a/e2e/transform/transform-runner/runner.ts b/e2e/transform/transform-runner/runner.ts new file mode 100644 index 000000000000..093ffc0242db --- /dev/null +++ b/e2e/transform/transform-runner/runner.ts @@ -0,0 +1,69 @@ +/** + * 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 throat from 'throat'; +import {TestResult, createEmptyTestResult} from '@jest/test-result'; +import type {Config} from '@jest/types'; +import { + OnTestFailure, + OnTestStart, + OnTestSuccess, + Test, + TestRunnerContext, + TestWatcher, +} from 'jest-runner'; + +export default class BaseTestRunner { + private _globalConfig: Config.GlobalConfig; + private _context: TestRunnerContext; + + constructor(globalConfig: Config.GlobalConfig, context?: TestRunnerContext) { + this._globalConfig = globalConfig; + this._context = context || {}; + } + + async runTests( + tests: Array, + watcher: TestWatcher, + onStart: OnTestStart, + onResult: OnTestSuccess, + onFailure: OnTestFailure, + ): Promise { + const mutex = throat(1); + return tests.reduce( + (promise, test) => + mutex(() => + promise + .then( + async (): Promise => { + await onStart(test); + return { + ...createEmptyTestResult(), + numPassingTests: 1, + testFilePath: test.path, + testResults: [ + { + ancestorTitles: [], + duration: 2, + failureMessages: [], + fullName: 'sample test', + location: null, + numPassingAsserts: 1, + status: 'passed', + title: 'sample test', + }, + ], + }; + }, + ) + .then(result => onResult(test, result)) + .catch(err => onFailure(test, err)), + ), + Promise.resolve(), + ); + } +} diff --git a/e2e/transform/transform-runner/tsconfig.json b/e2e/transform/transform-runner/tsconfig.json new file mode 100644 index 000000000000..174da5dc37bf --- /dev/null +++ b/e2e/transform/transform-runner/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "module": "commonjs", + } +} diff --git a/packages/jest-core/src/TestScheduler.ts b/packages/jest-core/src/TestScheduler.ts index 79fad4c133c4..cf1846b9f214 100644 --- a/packages/jest-core/src/TestScheduler.ts +++ b/packages/jest-core/src/TestScheduler.ts @@ -25,6 +25,7 @@ import { buildFailureTestResult, makeEmptyAggregatedTestResult, } from '@jest/test-result'; +import {ScriptTransformer} from '@jest/transform'; import type {Config} from '@jest/types'; import {formatExecError} from 'jest-message-util'; import TestRunner = require('jest-runner'); @@ -193,7 +194,10 @@ export default class TestScheduler { contexts.forEach(context => { const {config} = context; if (!testRunners[config.runner]) { - const Runner: typeof TestRunner = require(config.runner); + const transformer = new ScriptTransformer(config); + const Runner: typeof TestRunner = interopRequireDefault( + transformer.requireAndTranspileModule(config.runner), + ).default; const runner = new Runner(this._globalConfig, { changedFiles: this._context?.changedFiles, sourcesRelatedToTestsInChangedFiles: this._context diff --git a/packages/jest-core/src/__tests__/TestScheduler.test.js b/packages/jest-core/src/__tests__/TestScheduler.test.js index dc3f8d357201..464a923206fc 100644 --- a/packages/jest-core/src/__tests__/TestScheduler.test.js +++ b/packages/jest-core/src/__tests__/TestScheduler.test.js @@ -88,7 +88,9 @@ test('schedule tests run in parallel per default', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], runner: 'jest-runner-parallel', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []), @@ -109,7 +111,9 @@ test('schedule tests run in serial if the runner flags them', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], runner: 'jest-runner-serial', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []), @@ -130,8 +134,10 @@ test('should bail after `n` failures', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], rootDir: './', runner: 'jest-runner-serial', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []), @@ -160,8 +166,10 @@ test('should not bail if less than `n` failures', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], rootDir: './', runner: 'jest-runner-serial', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []), @@ -190,7 +198,9 @@ test('should set runInBand to run in serial', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], runner: 'jest-runner-parallel', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []), @@ -214,7 +224,9 @@ test('should set runInBand to not run in serial', async () => { const test = { context: { config: makeProjectConfig({ + moduleFileExtensions: ['.js'], runner: 'jest-runner-parallel', + transform: [], }), hasteFS: { matchFiles: jest.fn(() => []),