diff --git a/CHANGELOG.md b/CHANGELOG.md index 209b691dcf6f..becc3c9cf1e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874)) - `[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)) ### Fixes diff --git a/e2e/__tests__/transform.test.ts b/e2e/__tests__/transform.test.ts index c906f94333ca..be5b7bf2735c 100644 --- a/e2e/__tests__/transform.test.ts +++ b/e2e/__tests__/transform.test.ts @@ -205,3 +205,14 @@ describe('transformer caching', () => { expect(loggedFiles).toHaveLength(2); }); }); + +describe('transform-environment', () => { + const dir = path.resolve(__dirname, '../transform/transform-environment'); + + it('should transform the environment', () => { + 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/babel-plugin-jest-hoist/package.json b/e2e/babel-plugin-jest-hoist/package.json index ab80492fd75d..c498b7ef21bf 100644 --- a/e2e/babel-plugin-jest-hoist/package.json +++ b/e2e/babel-plugin-jest-hoist/package.json @@ -7,6 +7,9 @@ }, "jest": { "automock": true, - "testEnvironment": "node" + "testEnvironment": "node", + "transformIgnorePatterns": [ + "/jest-environment-node/" + ] } } diff --git a/e2e/coverage-transform-instrumented/package.json b/e2e/coverage-transform-instrumented/package.json index db634e8daac3..3bb4016f58a2 100644 --- a/e2e/coverage-transform-instrumented/package.json +++ b/e2e/coverage-transform-instrumented/package.json @@ -6,6 +6,9 @@ }, "testRegex": "/__tests__/.*\\.(js)$", "testEnvironment": "node", + "transformIgnorePatterns": [ + "/jest-environment-node/" + ], "moduleFileExtensions": [ "js" ] diff --git a/e2e/transform-linked-modules/package.json b/e2e/transform-linked-modules/package.json index 76fce8cdf931..1bea4d7d023b 100644 --- a/e2e/transform-linked-modules/package.json +++ b/e2e/transform-linked-modules/package.json @@ -4,7 +4,8 @@ "transformIgnorePatterns": [ "/node_modules/", "/__tests__", - "/ignored/" + "/ignored/", + "/jest-environment-node/" ], "transform": { "\\.js$": "/preprocessor.js" diff --git a/e2e/transform/babel-jest-ignored/babel.config.js b/e2e/transform/babel-jest-ignored/babel.config.js index 7178989f46c0..e35eb8dc019c 100644 --- a/e2e/transform/babel-jest-ignored/babel.config.js +++ b/e2e/transform/babel-jest-ignored/babel.config.js @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -module.exports = {only: ['blablabla']}; +module.exports = {only: ['blablabla', /jest-environment-node/]}; diff --git a/e2e/transform/multiple-transformers/package.json b/e2e/transform/multiple-transformers/package.json index 47cbbff146f2..074d79491bfc 100644 --- a/e2e/transform/multiple-transformers/package.json +++ b/e2e/transform/multiple-transformers/package.json @@ -5,7 +5,10 @@ "\\.js$": "/jsPreprocessor.js", "\\.svg$": "/filePreprocessor.js" }, - "testEnvironment": "node" + "testEnvironment": "node", + "transformIgnorePatterns": [ + "/jest-environment-node/" + ] }, "dependencies": { "@babel/core": "^7.0.0", diff --git a/e2e/transform/transform-environment/__tests__/add.test.js b/e2e/transform/transform-environment/__tests__/add.test.js new file mode 100644 index 000000000000..b3f97f774baf --- /dev/null +++ b/e2e/transform/transform-environment/__tests__/add.test.js @@ -0,0 +1,11 @@ +/** + * 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', () => { + // eslint-disable-next-line no-undef + expect(one + 1).toBe(2); +}); diff --git a/e2e/transform/transform-environment/babel.config.js b/e2e/transform/transform-environment/babel.config.js new file mode 100644 index 000000000000..19dd8862b516 --- /dev/null +++ b/e2e/transform/transform-environment/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-environment/environment.ts b/e2e/transform/transform-environment/environment.ts new file mode 100644 index 000000000000..9f702529c5d3 --- /dev/null +++ b/e2e/transform/transform-environment/environment.ts @@ -0,0 +1,16 @@ +/** + * 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 type {Config} from '@jest/types'; +import NodeEnvironment from 'jest-environment-node'; + +export default class CustomEnvironment extends NodeEnvironment { + constructor(config: Config.ProjectConfig) { + super(config); + this.global.one = 1; + } +} diff --git a/e2e/transform/transform-environment/package.json b/e2e/transform/transform-environment/package.json new file mode 100644 index 000000000000..e1104c0a57f9 --- /dev/null +++ b/e2e/transform/transform-environment/package.json @@ -0,0 +1,14 @@ +{ + "jest": { + "testEnvironment": "/environment.ts", + "transformIgnorePatterns": [ + "jest-environment-node" + ], + "rootDir": "./" + }, + "dependencies": { + "@babel/preset-env": "^7.0.0", + "@babel/preset-typescript": "^7.0.0", + "jest-environment-node": "^26.6.2" + } +} diff --git a/e2e/transform/transform-environment/tsconfig.json b/e2e/transform/transform-environment/tsconfig.json new file mode 100644 index 000000000000..174da5dc37bf --- /dev/null +++ b/e2e/transform/transform-environment/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "module": "commonjs", + } +} diff --git a/e2e/v8-coverage/empty-sourcemap/babel.config.js b/e2e/v8-coverage/empty-sourcemap/babel.config.js index 5be9674d9d91..19dd8862b516 100644 --- a/e2e/v8-coverage/empty-sourcemap/babel.config.js +++ b/e2e/v8-coverage/empty-sourcemap/babel.config.js @@ -6,5 +6,8 @@ */ module.exports = { - presets: ['@babel/preset-env', '@babel/preset-typescript'], + presets: [ + ['@babel/preset-env', {targets: {node: 'current'}}], + '@babel/preset-typescript', + ], }; diff --git a/packages/jest-repl/src/cli/runtime-cli.ts b/packages/jest-repl/src/cli/runtime-cli.ts index 29ee1bdbef7e..61fe494222e7 100644 --- a/packages/jest-repl/src/cli/runtime-cli.ts +++ b/packages/jest-repl/src/cli/runtime-cli.ts @@ -11,10 +11,11 @@ import chalk = require('chalk'); import yargs = require('yargs'); import {CustomConsole} from '@jest/console'; import type {JestEnvironment} from '@jest/environment'; +import {ScriptTransformer} from '@jest/transform'; import type {Config} from '@jest/types'; import {deprecationEntries, readConfig} from 'jest-config'; import Runtime from 'jest-runtime'; -import {setGlobal, tryRealpath} from 'jest-util'; +import {interopRequireDefault, setGlobal, tryRealpath} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import * as args from './args'; import {VERSION} from './version'; @@ -73,7 +74,10 @@ export async function run( watchman: globalConfig.watchman, }); - const Environment: typeof JestEnvironment = require(config.testEnvironment); + const transformer = new ScriptTransformer(config); + const Environment: typeof JestEnvironment = interopRequireDefault( + transformer.requireAndTranspileModule(config.testEnvironment), + ).default; const environment = new Environment(config); setGlobal( environment.global, diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 1174753dfb5d..d6251b1cf347 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -19,6 +19,7 @@ import { } from '@jest/console'; import type {JestEnvironment} from '@jest/environment'; import type {TestResult} from '@jest/test-result'; +import {ScriptTransformer} from '@jest/transform'; import type {Config} from '@jest/types'; import {getTestEnvironment} from 'jest-config'; import * as docblock from 'jest-docblock'; @@ -102,8 +103,9 @@ async function runTestInternal( }); } + const transformer = new ScriptTransformer(config); const TestEnvironment: typeof JestEnvironment = interopRequireDefault( - require(testEnvironment), + transformer.requireAndTranspileModule(testEnvironment), ).default; const testFramework: TestFramework = interopRequireDefault( process.env.JEST_CIRCUS === '1' diff --git a/packages/jest-runner/tsconfig.json b/packages/jest-runner/tsconfig.json index 69c0f3da86e8..d89ad8ced039 100644 --- a/packages/jest-runner/tsconfig.json +++ b/packages/jest-runner/tsconfig.json @@ -15,6 +15,7 @@ {"path": "../jest-resolve"}, {"path": "../jest-runtime"}, {"path": "../jest-test-result"}, + {"path": "../jest-transform"}, {"path": "../jest-types"}, {"path": "../jest-worker"}, {"path": "../jest-util"}