From 407aa6bfc271f636bd4a41b8719361c56c384c99 Mon Sep 17 00:00:00 2001 From: M4rk9696 Date: Fri, 26 Jul 2019 01:15:30 +0530 Subject: [PATCH 1/4] feat: allow test environment to run with preset/transform --- CHANGELOG.md | 1 + e2e/__tests__/transform.test.ts | 11 +++++++++++ e2e/babel-plugin-jest-hoist/package.json | 5 ++++- .../package.json | 3 +++ e2e/transform-linked-modules/package.json | 3 ++- .../babel-jest-ignored/babel.config.js | 2 +- .../multiple-transformers/package.json | 5 ++++- .../__tests__/add.test.js | 11 +++++++++++ .../transform-environment/babel.config.js | 18 ++++++++++++++++++ .../transform-environment/environment.ts | 16 ++++++++++++++++ .../transform-environment/package.json | 14 ++++++++++++++ .../transform-environment/tsconfig.json | 6 ++++++ packages/jest-repl/src/cli/runtime-cli.ts | 8 ++++++-- packages/jest-runner/src/runTest.ts | 4 +++- packages/jest-runner/tsconfig.json | 1 + 15 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 e2e/transform/transform-environment/__tests__/add.test.js create mode 100644 e2e/transform/transform-environment/babel.config.js create mode 100644 e2e/transform/transform-environment/environment.ts create mode 100644 e2e/transform/transform-environment/package.json create mode 100644 e2e/transform/transform-environment/tsconfig.json 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..f57a151246bf --- /dev/null +++ b/e2e/transform/transform-environment/babel.config.js @@ -0,0 +1,18 @@ +/** + * 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-typescript'], + [ + '@babel/preset-env', + { + targets: {node: 8}, + }, + ], + ], +}; diff --git a/e2e/transform/transform-environment/environment.ts b/e2e/transform/transform-environment/environment.ts new file mode 100644 index 000000000000..59c7cffef2f3 --- /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 {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..1cb2b9035591 --- /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": "^24.9.0" + } +} 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/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"} From 9a5787ab40cf75b1daca8112a358a639706ac590 Mon Sep 17 00:00:00 2001 From: Mark1626 Date: Sun, 15 Nov 2020 11:28:25 +0530 Subject: [PATCH 2/4] chore: fix test, update e2e dep --- e2e/transform/transform-environment/package.json | 2 +- e2e/v8-coverage/no-sourcemap/package.json | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/e2e/transform/transform-environment/package.json b/e2e/transform/transform-environment/package.json index 1cb2b9035591..e1104c0a57f9 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": "^24.9.0" + "jest-environment-node": "^26.6.2" } } diff --git a/e2e/v8-coverage/no-sourcemap/package.json b/e2e/v8-coverage/no-sourcemap/package.json index e63e40a94614..388b57b569d5 100644 --- a/e2e/v8-coverage/no-sourcemap/package.json +++ b/e2e/v8-coverage/no-sourcemap/package.json @@ -6,6 +6,9 @@ "transform": { "\\.[jt]sx?$": "babel-jest", "\\.css$": "/cssTransform.js" - } + }, + "transformIgnorePatterns": [ + "jest-environment-node" + ] } } From ab4f9fbb775020c66be5b8103d86bfa36572dfac Mon Sep 17 00:00:00 2001 From: Mark1626 Date: Sun, 15 Nov 2020 17:44:24 +0530 Subject: [PATCH 3/4] fix: Fix failing empty-sourcemap test --- e2e/v8-coverage/empty-sourcemap/babel.config.js | 10 +++++++++- e2e/v8-coverage/no-sourcemap/package.json | 5 +---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/e2e/v8-coverage/empty-sourcemap/babel.config.js b/e2e/v8-coverage/empty-sourcemap/babel.config.js index 5be9674d9d91..f57a151246bf 100644 --- a/e2e/v8-coverage/empty-sourcemap/babel.config.js +++ b/e2e/v8-coverage/empty-sourcemap/babel.config.js @@ -6,5 +6,13 @@ */ module.exports = { - presets: ['@babel/preset-env', '@babel/preset-typescript'], + presets: [ + ['@babel/preset-typescript'], + [ + '@babel/preset-env', + { + targets: {node: 8}, + }, + ], + ], }; diff --git a/e2e/v8-coverage/no-sourcemap/package.json b/e2e/v8-coverage/no-sourcemap/package.json index 388b57b569d5..e63e40a94614 100644 --- a/e2e/v8-coverage/no-sourcemap/package.json +++ b/e2e/v8-coverage/no-sourcemap/package.json @@ -6,9 +6,6 @@ "transform": { "\\.[jt]sx?$": "babel-jest", "\\.css$": "/cssTransform.js" - }, - "transformIgnorePatterns": [ - "jest-environment-node" - ] + } } } From e58dd073cfeedf881529816c643220a9c9a072c1 Mon Sep 17 00:00:00 2001 From: Mark1626 Date: Sun, 15 Nov 2020 18:34:00 +0530 Subject: [PATCH 4/4] chore: Review changes --- e2e/transform/transform-environment/babel.config.js | 9 ++------- e2e/transform/transform-environment/environment.ts | 2 +- e2e/v8-coverage/empty-sourcemap/babel.config.js | 9 ++------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/e2e/transform/transform-environment/babel.config.js b/e2e/transform/transform-environment/babel.config.js index f57a151246bf..19dd8862b516 100644 --- a/e2e/transform/transform-environment/babel.config.js +++ b/e2e/transform/transform-environment/babel.config.js @@ -7,12 +7,7 @@ module.exports = { presets: [ - ['@babel/preset-typescript'], - [ - '@babel/preset-env', - { - targets: {node: 8}, - }, - ], + ['@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 index 59c7cffef2f3..9f702529c5d3 100644 --- a/e2e/transform/transform-environment/environment.ts +++ b/e2e/transform/transform-environment/environment.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {Config} from '@jest/types'; +import type {Config} from '@jest/types'; import NodeEnvironment from 'jest-environment-node'; export default class CustomEnvironment extends NodeEnvironment { diff --git a/e2e/v8-coverage/empty-sourcemap/babel.config.js b/e2e/v8-coverage/empty-sourcemap/babel.config.js index f57a151246bf..19dd8862b516 100644 --- a/e2e/v8-coverage/empty-sourcemap/babel.config.js +++ b/e2e/v8-coverage/empty-sourcemap/babel.config.js @@ -7,12 +7,7 @@ module.exports = { presets: [ - ['@babel/preset-typescript'], - [ - '@babel/preset-env', - { - targets: {node: 8}, - }, - ], + ['@babel/preset-env', {targets: {node: 'current'}}], + '@babel/preset-typescript', ], };