From 31e584355434c4fc96022f9e8b41f04c11d24343 Mon Sep 17 00:00:00 2001 From: Ahn <27772165+ahnpnl@users.noreply.github.com> Date: Sun, 28 Aug 2022 17:55:43 +0200 Subject: [PATCH] feat: migrate globals config to transformer config (#3780) DEPRECATION Define `ts-jest` config under `globals` is now deprecated. Please define the config via transformer config instead. --- .../__snapshots__/const-enum.test.ts.snap | 24 ++------------- .../hoist-jest/jest-isolated.config.js | 18 +++++------ e2e/ast-transformers/hoist-jest/package.json | 13 ++++---- .../transformer-in-ts/package.json | 9 ++---- .../jest-isolated.config.js | 30 +++++++++---------- .../transformer-options/package.json | 9 ++---- e2e/config-typing/jest.config.ts | 12 ++++---- e2e/const-enum/jest-isolated.config.js | 14 +++++---- e2e/coverage-report/jest-isolated.config.js | 12 ++++---- e2e/custom-compiler/ttypescript/package.json | 9 ++---- e2e/diagnostics/jest-disabled.config.js | 12 ++++---- e2e/diagnostics/jest-exclude.config.js | 16 +++++----- e2e/diagnostics/jest-ignored-code.config.js | 16 +++++----- e2e/diagnostics/jest-isolated.config.js | 12 ++++---- e2e/diagnostics/jest-warn.config.js | 16 +++++----- e2e/enum/jest-isolated.config.js | 12 ++++---- e2e/extend-ts-jest/package.json | 9 ++---- e2e/native-esm-js/isolated/package.json | 11 +++---- e2e/native-esm-js/non-isolated/package.json | 15 ++++------ e2e/native-esm-ts/jest-isolated.config.js | 14 ++++----- e2e/native-esm-ts/package.json | 9 ++---- e2e/presets/package.json | 9 ++---- .../disabled/jest-isolated.config.js | 18 +++++------ e2e/source-map/disabled/jest.config.js | 16 +++++----- .../enabled/jest-isolated.config.js | 12 ++++---- .../babel-cjs-file/jest.config.js | 14 ++++----- e2e/transform-js/babel-enabled/jest.config.js | 14 ++++----- e2e/transform-js/babel-js-file/jest.config.js | 14 ++++----- e2e/transform-js/no-type-check/jest.config.js | 14 ++++----- e2e/transform-js/type-check/jest.config.js | 12 ++++---- e2e/transform-tsx/jest-isolated.config.js | 12 ++++---- .../js-with-babel/jest-esm-isolated.config.js | 17 ++++++----- examples/js-with-babel/jest-esm.config.js | 15 ++++++---- .../js-with-babel/jest-isolated.config.js | 13 ++++---- examples/js-with-babel/jest.config.js | 11 ++++--- .../js-with-ts/jest-esm-isolated.config.js | 15 ++++++---- examples/js-with-ts/jest-esm.config.js | 13 ++++---- examples/js-with-ts/jest-isolated.config.js | 11 ++++--- .../react-app/jest-esm-isolated.config.js | 21 ++++++++----- examples/react-app/jest-esm.config.js | 18 +++++++---- examples/react-app/jest-isolated.config.js | 16 ++++++---- examples/react-app/jest.config.js | 16 +++++----- examples/ts-only/jest-esm-isolated.config.js | 15 ++++++---- examples/ts-only/jest-esm.config.js | 13 ++++---- examples/ts-only/jest-isolated.config.js | 11 ++++--- jest.config.js | 14 ++++----- src/cli/__snapshots__/cli.spec.ts.snap | 16 ++++++++++ src/cli/cli.spec.ts | 22 ++++++++++++++ src/cli/config/migrate.ts | 17 ++++++++++- src/index.ts | 5 ++-- src/legacy/index.ts | 4 ++- src/legacy/ts-jest-transformer.ts | 19 ++++++++++-- src/utils/messages.ts | 4 +++ 53 files changed, 405 insertions(+), 328 deletions(-) diff --git a/e2e/__tests__/__snapshots__/const-enum.test.ts.snap b/e2e/__tests__/__snapshots__/const-enum.test.ts.snap index a396165a23..725a11da5f 100644 --- a/e2e/__tests__/__snapshots__/const-enum.test.ts.snap +++ b/e2e/__tests__/__snapshots__/const-enum.test.ts.snap @@ -1,27 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`partial successfully runs the tests inside const-enum with 'isolatedModules: true' 1`] = ` -"FAIL __tests__/import-from-d-ts-no-js.spec.ts - ● Test suite failed to run - - Cannot find module '../hoo-constant' from '__tests__/import-from-d-ts-no-js.spec.ts' - - However, Jest was able to find: - '../hoo-constant.d.ts' - - You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node']. - - See https://jestjs.io/docs/configuration#modulefileextensions-arraystring - - > 1 | import { HooConstEnum } from '../hoo-constant' - | ^ - 2 | - 3 | const getTwo = (): string => HooConstEnum.two - 4 | - - at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:487:11) - at Object. (__tests__/import-from-d-ts-no-js.spec.ts:1:1) - -PASS __tests__/import-from-d-ts-has-js.spec.ts +"PASS __tests__/import-from-d-ts-has-js.spec.ts +PASS __tests__/import-from-d-ts-no-js.spec.ts PASS __tests__/import-from-ts.spec.ts" `; diff --git a/e2e/ast-transformers/hoist-jest/jest-isolated.config.js b/e2e/ast-transformers/hoist-jest/jest-isolated.config.js index 25d1604668..ea3636678d 100644 --- a/e2e/ast-transformers/hoist-jest/jest-isolated.config.js +++ b/e2e/ast-transformers/hoist-jest/jest-isolated.config.js @@ -1,18 +1,18 @@ /** @type {import('../../../dist').InitialOptionsTsJest} */ module.exports = { automock: true, - globals: { - 'ts-jest': { - isolatedModules: true, - tsconfig: { - allowJs: true, - }, - }, - }, moduleNameMapper: { react$: '/node_modules/react', }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + isolatedModules: true, + tsconfig: { + allowJs: true, + }, + }, + ], }, } diff --git a/e2e/ast-transformers/hoist-jest/package.json b/e2e/ast-transformers/hoist-jest/package.json index e6528f28ab..36aa93de76 100644 --- a/e2e/ast-transformers/hoist-jest/package.json +++ b/e2e/ast-transformers/hoist-jest/package.json @@ -4,18 +4,15 @@ }, "jest": { "automock": true, - "globals": { - "ts-jest": { - "tsconfig": { - "allowJs": true - } - } - }, "moduleNameMapper": { "react$": "/node_modules/react" }, "transform": { - "^.+\\.[tj]sx?$": "/../../../legacy.js" + "^.+\\.[tj]sx?$": ["/../../../legacy.js", { + "tsconfig": { + "allowJs": true + } + }] } } } diff --git a/e2e/ast-transformers/transformer-in-ts/package.json b/e2e/ast-transformers/transformer-in-ts/package.json index 8de92aca90..a5c517eef5 100644 --- a/e2e/ast-transformers/transformer-in-ts/package.json +++ b/e2e/ast-transformers/transformer-in-ts/package.json @@ -3,15 +3,12 @@ "esbuild": "~0.15.5" }, "jest": { - "globals": { - "ts-jest": { + "transform": { + "^.+\\.[tj]sx?$": ["/../../../legacy.js", { "astTransformers": { "before": ["/../../../src/transformers/hoist-jest.ts"] } - } - }, - "transform": { - "^.+\\.[tj]sx?$": "/../../../legacy.js" + }] } } } diff --git a/e2e/ast-transformers/transformer-options/jest-isolated.config.js b/e2e/ast-transformers/transformer-options/jest-isolated.config.js index d0b7ce8a22..1d7d4e858e 100644 --- a/e2e/ast-transformers/transformer-options/jest-isolated.config.js +++ b/e2e/ast-transformers/transformer-options/jest-isolated.config.js @@ -1,21 +1,21 @@ /** @type {import('../../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - isolatedModules: true, - astTransformers: { - before: [ - { - path: '/node_modules/@formatjs/ts-transformer/ts-jest-integration', - options: { - removeDefaultMessage: true, + transform: { + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + isolatedModules: true, + astTransformers: { + before: [ + { + path: '/node_modules/@formatjs/ts-transformer/ts-jest-integration', + options: { + removeDefaultMessage: true, + }, }, - }, - ], + ], + }, }, - }, - }, - transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + ], }, } diff --git a/e2e/ast-transformers/transformer-options/package.json b/e2e/ast-transformers/transformer-options/package.json index 6a3b723eb6..ee2817675f 100644 --- a/e2e/ast-transformers/transformer-options/package.json +++ b/e2e/ast-transformers/transformer-options/package.json @@ -5,8 +5,8 @@ "react-intl": "^6.1.0" }, "jest": { - "globals": { - "ts-jest": { + "transform": { + "^.+\\.[tj]sx?$": ["/../../../legacy.js", { "astTransformers": { "before": [ { @@ -17,10 +17,7 @@ } ] } - } - }, - "transform": { - "^.+\\.[tj]sx?$": "/../../../legacy.js" + }] } } } diff --git a/e2e/config-typing/jest.config.ts b/e2e/config-typing/jest.config.ts index c63f43e6dc..da893d146d 100644 --- a/e2e/config-typing/jest.config.ts +++ b/e2e/config-typing/jest.config.ts @@ -1,13 +1,13 @@ import type { InitialOptionsTsJest } from 'ts-jest' const jestCfg: InitialOptionsTsJest = { - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': 'ts-jest', + '^.+.tsx?$': [ + 'ts-jest', + { + isolatedModules: true, + }, + ], }, } diff --git a/e2e/const-enum/jest-isolated.config.js b/e2e/const-enum/jest-isolated.config.js index b26cdb58b7..f7d630ec78 100644 --- a/e2e/const-enum/jest-isolated.config.js +++ b/e2e/const-enum/jest-isolated.config.js @@ -1,11 +1,13 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + 'ts-jest': { + isolatedModules: true, + }, + }, + ], }, } diff --git a/e2e/coverage-report/jest-isolated.config.js b/e2e/coverage-report/jest-isolated.config.js index a4e809e916..52e2dc6523 100644 --- a/e2e/coverage-report/jest-isolated.config.js +++ b/e2e/coverage-report/jest-isolated.config.js @@ -1,12 +1,12 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { collectCoverageFrom: ['**/*.js', '**/*.ts', '!**/node_modules/**', '!**/coverage/**'], - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + isolatedModules: true, + }, + ], }, } diff --git a/e2e/custom-compiler/ttypescript/package.json b/e2e/custom-compiler/ttypescript/package.json index e35fe6c83b..1e840dd447 100644 --- a/e2e/custom-compiler/ttypescript/package.json +++ b/e2e/custom-compiler/ttypescript/package.json @@ -4,13 +4,10 @@ "ttypescript": "^1.5.13" }, "jest": { - "globals": { - "ts-jest": { - "compiler": "ttypescript" - } - }, "transform": { - "^.+.tsx?$": "/../../../legacy.js" + "^.+.tsx?$": ["/../../../legacy.js", { + "compiler": "ttypescript" + }] } } } diff --git a/e2e/diagnostics/jest-disabled.config.js b/e2e/diagnostics/jest-disabled.config.js index b0ca64c765..b6bf9e2aef 100644 --- a/e2e/diagnostics/jest-disabled.config.js +++ b/e2e/diagnostics/jest-disabled.config.js @@ -1,11 +1,11 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - diagnostics: false, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + diagnostics: false, + }, + ], }, } diff --git a/e2e/diagnostics/jest-exclude.config.js b/e2e/diagnostics/jest-exclude.config.js index b18806e22e..f4ab90c12b 100644 --- a/e2e/diagnostics/jest-exclude.config.js +++ b/e2e/diagnostics/jest-exclude.config.js @@ -1,13 +1,13 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - diagnostics: { - exclude: ['**/__tests__/diagnostics.spec.ts'], - }, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + diagnostics: { + exclude: ['**/__tests__/diagnostics.spec.ts'], + }, + }, + ], }, } diff --git a/e2e/diagnostics/jest-ignored-code.config.js b/e2e/diagnostics/jest-ignored-code.config.js index 6bea033881..879a5df196 100644 --- a/e2e/diagnostics/jest-ignored-code.config.js +++ b/e2e/diagnostics/jest-ignored-code.config.js @@ -1,13 +1,13 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - diagnostics: { - ignoreCodes: 'TS2741', - }, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + diagnostics: { + ignoreCodes: 'TS2741', + }, + }, + ], }, } diff --git a/e2e/diagnostics/jest-isolated.config.js b/e2e/diagnostics/jest-isolated.config.js index b26cdb58b7..d631cf3a62 100644 --- a/e2e/diagnostics/jest-isolated.config.js +++ b/e2e/diagnostics/jest-isolated.config.js @@ -1,11 +1,11 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + isolatedModules: true, + }, + ], }, } diff --git a/e2e/diagnostics/jest-warn.config.js b/e2e/diagnostics/jest-warn.config.js index 0cef38d0e2..cfe6fd2ef2 100644 --- a/e2e/diagnostics/jest-warn.config.js +++ b/e2e/diagnostics/jest-warn.config.js @@ -1,13 +1,13 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - diagnostics: { - warnOnly: true, - }, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + diagnostics: { + warnOnly: true, + }, + }, + ], }, } diff --git a/e2e/enum/jest-isolated.config.js b/e2e/enum/jest-isolated.config.js index b26cdb58b7..d631cf3a62 100644 --- a/e2e/enum/jest-isolated.config.js +++ b/e2e/enum/jest-isolated.config.js @@ -1,11 +1,11 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + isolatedModules: true, + }, + ], }, } diff --git a/e2e/extend-ts-jest/package.json b/e2e/extend-ts-jest/package.json index cf79ee3561..2b865b1038 100644 --- a/e2e/extend-ts-jest/package.json +++ b/e2e/extend-ts-jest/package.json @@ -1,12 +1,9 @@ { "jest": { - "globals": { - "ts-jest": { - "isolatedModules": true - } - }, "transform": { - "^.+\\.tsx?$": "./foo-transformer.js" + "^.+\\.tsx?$": ["./foo-transformer.js", { + "isolatedModules": true + }] } } } diff --git a/e2e/native-esm-js/isolated/package.json b/e2e/native-esm-js/isolated/package.json index fc4aa211e7..e118861526 100644 --- a/e2e/native-esm-js/isolated/package.json +++ b/e2e/native-esm-js/isolated/package.json @@ -2,18 +2,15 @@ "type": "module", "jest": { "extensionsToTreatAsEsm": [".ts"], - "globals": { - "ts-jest": { - "useESM": true, - "isolatedModules": true - } - }, "roots": ["", "/../__tests__"], "moduleNameMapper": { "@jest/globals": "/../../../node_modules/@jest/globals" }, "transform": { - "^.+.tsx?$": "/../../../legacy.js" + "^.+.tsx?$": ["/../../../legacy.js", { + "useESM": true, + "isolatedModules": true + }] } } } diff --git a/e2e/native-esm-js/non-isolated/package.json b/e2e/native-esm-js/non-isolated/package.json index 4d0edd416a..a1f7179cfe 100644 --- a/e2e/native-esm-js/non-isolated/package.json +++ b/e2e/native-esm-js/non-isolated/package.json @@ -2,20 +2,17 @@ "type": "module", "jest": { "extensionsToTreatAsEsm": [".ts"], - "globals": { - "ts-jest": { - "useESM": true, - "diagnostics": { - "exclude": ["**/native-esm-tla.spec.ts"] - } - } - }, "roots": ["", "/../__tests__"], "moduleNameMapper": { "@jest/globals": "/../../../node_modules/@jest/globals" }, "transform": { - "^.+.tsx?$": "/../../../legacy.js" + "^.+.tsx?$": ["/../../../legacy.js", { + "useESM": true, + "diagnostics": { + "exclude": ["**/native-esm-tla.spec.ts"] + } + }] } } } diff --git a/e2e/native-esm-ts/jest-isolated.config.js b/e2e/native-esm-ts/jest-isolated.config.js index c62cabebf1..2efd1bdabc 100644 --- a/e2e/native-esm-ts/jest-isolated.config.js +++ b/e2e/native-esm-ts/jest-isolated.config.js @@ -1,14 +1,14 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { extensionsToTreatAsEsm: ['.ts'], - globals: { - 'ts-jest': { - isolatedModules: true, - useESM: true, - }, - }, resolver: '/mjs-resolver.ts', transform: { - '^.+\\.m?tsx?$': '/../../legacy.js', + '^.+\\.m?tsx?$': [ + '/../../legacy.js', + { + isolatedModules: true, + useESM: true, + }, + ], }, } diff --git a/e2e/native-esm-ts/package.json b/e2e/native-esm-ts/package.json index 97b1e7dbc5..e99828b081 100644 --- a/e2e/native-esm-ts/package.json +++ b/e2e/native-esm-ts/package.json @@ -5,14 +5,11 @@ }, "jest": { "extensionsToTreatAsEsm": [".ts"], - "globals": { - "ts-jest": { - "useESM": true - } - }, "resolver": "/mjs-resolver.ts", "transform": { - "^.+\\.m?tsx?$": "/../../legacy.js" + "^.+\\.m?tsx?$": ["/../../legacy.js", { + "useESM": true + }] } } } diff --git a/e2e/presets/package.json b/e2e/presets/package.json index 16a509099c..e59b032994 100644 --- a/e2e/presets/package.json +++ b/e2e/presets/package.json @@ -1,15 +1,12 @@ { "jest": { - "globals": { - "ts-jest": { + "transform": { + "^.+.[tj]sx?$": ["/../../legacy.js", { "isolatedModules": true, "tsconfig": { "allowJs": true } - } - }, - "transform": { - "^.+.[tj]sx?$": "/../../legacy.js" + }] } } } diff --git a/e2e/source-map/disabled/jest-isolated.config.js b/e2e/source-map/disabled/jest-isolated.config.js index 0e93df47a6..cefe41aba6 100644 --- a/e2e/source-map/disabled/jest-isolated.config.js +++ b/e2e/source-map/disabled/jest-isolated.config.js @@ -2,15 +2,15 @@ module.exports = { displayName: 'sourcemap-disabled-isolated', roots: ['', '/../__tests__'], - globals: { - 'ts-jest': { - isolatedModules: true, - tsconfig: { - sourceMap: false, - }, - }, - }, transform: { - '^.+.tsx?$': '/../../../legacy.js', + '^.+.tsx?$': [ + '/../../../legacy.js', + { + isolatedModules: true, + tsconfig: { + sourceMap: false, + }, + }, + ], }, } diff --git a/e2e/source-map/disabled/jest.config.js b/e2e/source-map/disabled/jest.config.js index 1a0450792d..8c0a7a2c2d 100644 --- a/e2e/source-map/disabled/jest.config.js +++ b/e2e/source-map/disabled/jest.config.js @@ -2,14 +2,14 @@ module.exports = { displayName: 'sourcemap-disabled', roots: ['', '/../__tests__'], - globals: { - 'ts-jest': { - tsconfig: { - sourceMap: false, - }, - }, - }, transform: { - '^.+.tsx?$': '/../../../legacy.js', + '^.+.tsx?$': [ + '/../../../legacy.js', + { + tsconfig: { + sourceMap: false, + }, + }, + ], }, } diff --git a/e2e/source-map/enabled/jest-isolated.config.js b/e2e/source-map/enabled/jest-isolated.config.js index 1112a2b54e..83e1d9f668 100644 --- a/e2e/source-map/enabled/jest-isolated.config.js +++ b/e2e/source-map/enabled/jest-isolated.config.js @@ -2,12 +2,12 @@ module.exports = { displayName: 'sourcemap-enabled-isolated', roots: ['', '/../__tests__'], - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, transform: { - '^.+.tsx?$': '/../../../legacy.js', + '^.+.tsx?$': [ + '/../../../legacy.js', + { + isolatedModules: true, + }, + ], }, } diff --git a/e2e/transform-js/babel-cjs-file/jest.config.js b/e2e/transform-js/babel-cjs-file/jest.config.js index 74a5cfb9c0..4aae1aa589 100644 --- a/e2e/transform-js/babel-cjs-file/jest.config.js +++ b/e2e/transform-js/babel-cjs-file/jest.config.js @@ -2,17 +2,17 @@ module.exports = { displayName: 'babel-cjs-file', roots: ['', '/../__tests__/for-babel'], - globals: { - 'ts-jest': { - babelConfig: '/babel.config.cjs', - isolatedModules: true, - }, - }, moduleNameMapper: { '@babel/core': '/../../../node_modules/@babel/core', 'babel-jest': '/../../../node_modules/babel-jest', }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + babelConfig: '/babel.config.cjs', + isolatedModules: true, + }, + ], }, } diff --git a/e2e/transform-js/babel-enabled/jest.config.js b/e2e/transform-js/babel-enabled/jest.config.js index 77d1d4dcab..2c7e354357 100644 --- a/e2e/transform-js/babel-enabled/jest.config.js +++ b/e2e/transform-js/babel-enabled/jest.config.js @@ -2,17 +2,17 @@ module.exports = { displayName: 'babel-enabled', roots: ['', '/../__tests__/for-babel'], - globals: { - 'ts-jest': { - babelConfig: true, - isolatedModules: true, - }, - }, moduleNameMapper: { '@babel/core': '/../../../node_modules/@babel/core', 'babel-jest': '/../../../node_modules/babel-jest', }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + babelConfig: true, + isolatedModules: true, + }, + ], }, } diff --git a/e2e/transform-js/babel-js-file/jest.config.js b/e2e/transform-js/babel-js-file/jest.config.js index 55c91408fd..15ebfefa0b 100644 --- a/e2e/transform-js/babel-js-file/jest.config.js +++ b/e2e/transform-js/babel-js-file/jest.config.js @@ -2,17 +2,17 @@ module.exports = { displayName: 'babel-js-file', roots: ['', '/../__tests__/for-babel'], - globals: { - 'ts-jest': { - babelConfig: '/babel.config.js', - isolatedModules: true, - }, - }, moduleNameMapper: { '@babel/core': '/../../../node_modules/@babel/core', 'babel-jest': '/../../../node_modules/babel-jest', }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + babelConfig: '/babel.config.js', + isolatedModules: true, + }, + ], }, } diff --git a/e2e/transform-js/no-type-check/jest.config.js b/e2e/transform-js/no-type-check/jest.config.js index 468f9900e0..d68344c119 100644 --- a/e2e/transform-js/no-type-check/jest.config.js +++ b/e2e/transform-js/no-type-check/jest.config.js @@ -2,13 +2,13 @@ module.exports = { displayName: 'no-type-check', roots: ['', '/../__tests__/for-ts'], - globals: { - 'ts-jest': { - isolatedModules: true, - babelConfig: true, - }, - }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + isolatedModules: true, + babelConfig: true, + }, + ], }, } diff --git a/e2e/transform-js/type-check/jest.config.js b/e2e/transform-js/type-check/jest.config.js index 58d29fa0dd..67c28addb7 100644 --- a/e2e/transform-js/type-check/jest.config.js +++ b/e2e/transform-js/type-check/jest.config.js @@ -2,12 +2,12 @@ module.exports = { displayName: 'type-check', roots: ['', '/../__tests__/for-ts'], - globals: { - 'ts-jest': { - babelConfig: true, - }, - }, transform: { - '^.+.[tj]sx?$': '/../../../legacy.js', + '^.+.[tj]sx?$': [ + '/../../../legacy.js', + { + babelConfig: true, + }, + ], }, } diff --git a/e2e/transform-tsx/jest-isolated.config.js b/e2e/transform-tsx/jest-isolated.config.js index d2b16e75cd..8581551464 100644 --- a/e2e/transform-tsx/jest-isolated.config.js +++ b/e2e/transform-tsx/jest-isolated.config.js @@ -1,12 +1,12 @@ /** @type {import('../../dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - isolatedModules: true, - }, - }, testEnvironment: 'jsdom', transform: { - '^.+.tsx?$': '/../../legacy.js', + '^.+.tsx?$': [ + '/../../legacy.js', + { + isolatedModules: true, + }, + ], }, } diff --git a/examples/js-with-babel/jest-esm-isolated.config.js b/examples/js-with-babel/jest-esm-isolated.config.js index f1a90621c5..8019c15d49 100644 --- a/examples/js-with-babel/jest-esm-isolated.config.js +++ b/examples/js-with-babel/jest-esm-isolated.config.js @@ -1,12 +1,15 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel-esm', - globals: { - 'ts-jest': { - babelConfig: true, - isolatedModules: true, - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: true, + isolatedModules: true, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/js-with-babel/jest-esm.config.js b/examples/js-with-babel/jest-esm.config.js index 230fcae773..e2b7a29119 100644 --- a/examples/js-with-babel/jest-esm.config.js +++ b/examples/js-with-babel/jest-esm.config.js @@ -1,11 +1,14 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel-esm', - globals: { - 'ts-jest': { - babelConfig: true, - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: true, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/js-with-babel/jest-isolated.config.js b/examples/js-with-babel/jest-isolated.config.js index 12c530644c..f66cd552e7 100644 --- a/examples/js-with-babel/jest-isolated.config.js +++ b/examples/js-with-babel/jest-isolated.config.js @@ -1,10 +1,13 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel', - globals: { - 'ts-jest': { - babelConfig: true, - isolatedModules: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: true, + isolatedModules: true, + }, + ], }, } diff --git a/examples/js-with-babel/jest.config.js b/examples/js-with-babel/jest.config.js index 2a54960583..f5f8434e85 100644 --- a/examples/js-with-babel/jest.config.js +++ b/examples/js-with-babel/jest.config.js @@ -1,9 +1,12 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel', - globals: { - 'ts-jest': { - babelConfig: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: true, + }, + ], }, } diff --git a/examples/js-with-ts/jest-esm-isolated.config.js b/examples/js-with-ts/jest-esm-isolated.config.js index 2bf1f5c4a1..2bb3e781a2 100644 --- a/examples/js-with-ts/jest-esm-isolated.config.js +++ b/examples/js-with-ts/jest-esm-isolated.config.js @@ -1,11 +1,14 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts-esm', - globals: { - 'ts-jest': { - isolatedModules: true, - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.m?[tj]sx?$': [ + 'ts-jest', + { + isolatedModules: true, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/js-with-ts/jest-esm.config.js b/examples/js-with-ts/jest-esm.config.js index 594590d1cb..c384382851 100644 --- a/examples/js-with-ts/jest-esm.config.js +++ b/examples/js-with-ts/jest-esm.config.js @@ -1,10 +1,13 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts-esm', - globals: { - 'ts-jest': { - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.m?[tj]sx?$': [ + 'ts-jest', + { + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/js-with-ts/jest-isolated.config.js b/examples/js-with-ts/jest-isolated.config.js index 241040d822..0d7dbedd30 100644 --- a/examples/js-with-ts/jest-isolated.config.js +++ b/examples/js-with-ts/jest-isolated.config.js @@ -1,9 +1,12 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts', - globals: { - 'ts-jest': { - isolatedModules: true, - }, + transform: { + '^.+\\.[tj]sx?$': [ + 'ts-jest', + { + isolatedModules: true, + }, + ], }, } diff --git a/examples/react-app/jest-esm-isolated.config.js b/examples/react-app/jest-esm-isolated.config.js index 6fc3508003..39e26f4cac 100644 --- a/examples/react-app/jest-esm-isolated.config.js +++ b/examples/react-app/jest-esm-isolated.config.js @@ -1,14 +1,21 @@ const baseEsmCfg = require('./jest-esm.config') +const baseCfg = require('./jest.config') /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { ...baseEsmCfg, - globals: { - 'ts-jest': { - ...baseEsmCfg.globals['ts-jest'], - isolatedModules: true, - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + ...baseCfg.transform, + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: { + presets: ['react-app'], + }, + isolatedModules: true, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/react-app/jest-esm.config.js b/examples/react-app/jest-esm.config.js index cbf5e50e33..0acc8c17b5 100644 --- a/examples/react-app/jest-esm.config.js +++ b/examples/react-app/jest-esm.config.js @@ -4,11 +4,17 @@ const baseCfg = require('./jest.config') module.exports = { ...baseCfg, preset: 'ts-jest/presets/js-with-babel-esm', - globals: { - 'ts-jest': { - ...baseCfg.globals['ts-jest'], - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + ...baseCfg.transform, + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: { + presets: ['react-app'], + }, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/react-app/jest-isolated.config.js b/examples/react-app/jest-isolated.config.js index 7e4475db0b..cb6ce04631 100644 --- a/examples/react-app/jest-isolated.config.js +++ b/examples/react-app/jest-isolated.config.js @@ -3,10 +3,16 @@ const baseCfg = require('./jest.config') /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { ...baseCfg, - globals: { - 'ts-jest': { - ...baseCfg.globals['ts-jest'], - isolatedModules: true, - }, + transform: { + ...baseCfg.transform, + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: { + presets: ['react-app'], + }, + isolatedModules: true, + }, + ], }, } diff --git a/examples/react-app/jest.config.js b/examples/react-app/jest.config.js index f65b0f9543..9e5825265a 100644 --- a/examples/react-app/jest.config.js +++ b/examples/react-app/jest.config.js @@ -1,12 +1,5 @@ module.exports = { roots: ['/src'], - globals: { - 'ts-jest': { - babelConfig: { - presets: ['react-app'], - }, - }, - }, collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'], setupFiles: ['react-app-polyfill/jsdom'], setupFilesAfterEnv: ['/src/setupTests.ts'], @@ -14,7 +7,14 @@ module.exports = { testEnvironment: 'jsdom', transform: { '^.+\\.(js|jsx|mjs|cjs)$': '/config/jest/babelTransform.js', - '^.+\\.(ts|tsx)$': 'ts-jest', + '^.+\\.(ts|tsx)$': [ + 'ts-jest', + { + babelConfig: { + presets: ['react-app'], + }, + }, + ], '^.+\\.css$': '/config/jest/cssTransform.js', '^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': '/config/jest/fileTransform.js', }, diff --git a/examples/ts-only/jest-esm-isolated.config.js b/examples/ts-only/jest-esm-isolated.config.js index 44448189af..2587c77c4b 100644 --- a/examples/ts-only/jest-esm-isolated.config.js +++ b/examples/ts-only/jest-esm-isolated.config.js @@ -1,11 +1,14 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/default-esm', - globals: { - 'ts-jest': { - isolatedModules: true, - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + isolatedModules: true, + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/ts-only/jest-esm.config.js b/examples/ts-only/jest-esm.config.js index 54ad7d58de..d6bcc2918c 100644 --- a/examples/ts-only/jest-esm.config.js +++ b/examples/ts-only/jest-esm.config.js @@ -1,10 +1,13 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest/presets/default-esm', - globals: { - 'ts-jest': { - tsconfig: 'tsconfig-esm.json', - useESM: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + tsconfig: 'tsconfig-esm.json', + useESM: true, + }, + ], }, } diff --git a/examples/ts-only/jest-isolated.config.js b/examples/ts-only/jest-isolated.config.js index 828e02c34e..1db0ec2996 100644 --- a/examples/ts-only/jest-isolated.config.js +++ b/examples/ts-only/jest-isolated.config.js @@ -1,9 +1,12 @@ /** @type {import('ts-jest').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest', - globals: { - 'ts-jest': { - isolatedModules: true, - }, + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + isolatedModules: true, + }, + ], }, } diff --git a/jest.config.js b/jest.config.js index 863e7a29af..fae470f90d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,18 +1,18 @@ /** @type {import('./dist').InitialOptionsTsJest} */ module.exports = { - globals: { - 'ts-jest': { - tsconfig: 'tsconfig.spec.json', - isolatedModules: true, - }, - }, collectCoverageFrom: ['src/**/*.ts'], modulePathIgnorePatterns: ['examples/.*', 'website/.*'], setupFilesAfterEnv: ['/src/__helpers__/setup-jest.ts'], snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')], testPathIgnorePatterns: ['src/__mocks__/*', '/node_modules/', '/examples/', '/e2e/.*/__tests__', '\\.snap$'], transform: { - '^.+.tsx?$': '/legacy.js', + '^.+.tsx?$': [ + '/legacy.js', + { + tsconfig: 'tsconfig.spec.json', + isolatedModules: true, + }, + ], }, coverageProvider: 'v8', } diff --git a/src/cli/__snapshots__/cli.spec.ts.snap b/src/cli/__snapshots__/cli.spec.ts.snap index 45795b2084..19c085409b 100644 --- a/src/cli/__snapshots__/cli.spec.ts.snap +++ b/src/cli/__snapshots__/cli.spec.ts.snap @@ -1,5 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`config migrate should migrate globals ts-jest config to transformer config 1`] = ` +""jest": { + "transform": { + "^.+\\\\.jsx?$": "babel-jest", + "^.+\\\\.tsx?$": [ + "ts-jest", + { + "tsconfig": "./tsconfig.json" + } + ] + }, + "preset": "ts-jest/presets/js-with-babel" +} +" +`; + exports[`config migrate should migrate preset if valid preset value is used 1`] = ` " No migration needed for given Jest configuration diff --git a/src/cli/cli.spec.ts b/src/cli/cli.spec.ts index d808fcfeeb..0a01b42eb6 100644 --- a/src/cli/cli.spec.ts +++ b/src/cli/cli.spec.ts @@ -600,5 +600,27 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". } `) }) + + it('should migrate globals ts-jest config to transformer config', async () => { + fs.existsSync.mockImplementation(() => true) + jest.mock( + pkgPaths.next, + () => ({ + jest: { + globals: { + 'ts-jest': { + tsconfig: './tsconfig.json', + }, + }, + transform: { '^.+\\.jsx?$': 'babel-jest', '^.+\\.tsx?$': 'ts-jest' }, + }, + }), + { virtual: true }, + ) + + const res = await runCli(...noOption, pkgPaths.current) + + expect(res.stdout).toMatchSnapshot() + }) }) // migrate }) // config diff --git a/src/cli/config/migrate.ts b/src/cli/config/migrate.ts index 9bf18c43b1..72247f6aca 100644 --- a/src/cli/config/migrate.ts +++ b/src/cli/config/migrate.ts @@ -115,12 +115,27 @@ Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more inf Object.keys(migratedConfig.transform).forEach((key) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const val = (migratedConfig.transform as any)[key] - if (typeof val === 'string' && /\/?ts-jest(?:\/preprocessor\.js)?$/.test(val)) { + if (typeof val === 'string' && /\/?ts-jest?(?:\/preprocessor\.js)?$/.test(val)) { // eslint-disable-next-line ;(migratedConfig.transform as any)[key] = 'ts-jest' } }) } + + // migrate globals config to transformer config + const globalsTsJestConfig = migratedConfig.globals?.['ts-jest'] + if (globalsTsJestConfig && migratedConfig.transform) { + Object.keys(migratedConfig.transform).forEach((key) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const val = (migratedConfig.transform as any)[key] + if (typeof val === 'string' && val.includes('ts-jest')) { + // eslint-disable-next-line + ;(migratedConfig.transform as any)[key] = Object.keys(globalsTsJestConfig).length ? [val, globalsTsJestConfig] : val + } + }) + delete (migratedConfig.globals ?? Object.create(null))['ts-jest'] + } + // check if it's the same as the preset's one if ( preset && diff --git a/src/index.ts b/src/index.ts index 7395716a93..4a4c4d9d2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { TsJestTransformer } from './legacy/ts-jest-transformer' +import type { TsJestGlobalOptions } from './types' export * from './config' export * from './constants' @@ -11,7 +12,7 @@ export * from './utils' export * from './types' export default { - createTransformer() { - return new TsJestTransformer() + createTransformer(tsJestConfig?: TsJestGlobalOptions) { + return new TsJestTransformer(tsJestConfig) }, } diff --git a/src/legacy/index.ts b/src/legacy/index.ts index 9cfb6e5d73..8993db0b27 100644 --- a/src/legacy/index.ts +++ b/src/legacy/index.ts @@ -1,5 +1,7 @@ +import type { TsJestGlobalOptions } from '../types' + import { TsJestTransformer } from './ts-jest-transformer' export default { - createTransformer: (): TsJestTransformer => new TsJestTransformer(), + createTransformer: (tsJestConfig?: TsJestGlobalOptions): TsJestTransformer => new TsJestTransformer(tsJestConfig), } diff --git a/src/legacy/ts-jest-transformer.ts b/src/legacy/ts-jest-transformer.ts index 16597d9c7c..235126db59 100644 --- a/src/legacy/ts-jest-transformer.ts +++ b/src/legacy/ts-jest-transformer.ts @@ -11,10 +11,11 @@ import type { DepGraphInfo, ProjectConfigTsJest, TransformOptionsTsJest, + TsJestGlobalOptions, } from '../types' import { parse, stringify, JsonableValue, rootLogger } from '../utils' import { importer } from '../utils/importer' -import { Errors, interpolate } from '../utils/messages' +import { Deprecations, Errors, interpolate } from '../utils/messages' import { sha1 } from '../utils/sha1' import { VersionCheckers } from '../utils/version-checkers' @@ -55,7 +56,7 @@ export class TsJestTransformer implements SyncTransformer { private _depGraphs: Map = new Map() private _watchMode = false - constructor() { + constructor(private readonly tsJestConfig?: TsJestGlobalOptions) { this._logger = rootLogger.child({ namespace: 'ts-jest-transformer' }) VersionCheckers.jest.warn() /** @@ -105,7 +106,19 @@ export class TsJestTransformer implements SyncTransformer { // create the new record in the index this._logger.info('no matching config-set found, creating a new one') - configSet = this._createConfigSet(config) + if (config.globals?.['ts-jest']) { + this._logger.warn(Deprecations.GlobalsTsJestConfigOption) + } + configSet = this._createConfigSet( + this.tsJestConfig + ? { + ...config, + globals: { + 'ts-jest': this.tsJestConfig, + }, + } + : config, + ) const jest = { ...config } // we need to remove some stuff from jest config // this which does not depend on config diff --git a/src/utils/messages.ts b/src/utils/messages.ts index 43223df492..b23c35cd9b 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -39,6 +39,10 @@ export const enum Deprecations { ConfigOption = '"[jest-config].{{oldPath}}" is deprecated, use "[jest-config].{{newPath}}" instead.', ConfigOptionWithNote = '"[jest-config].{{oldPath}}" is deprecated, use "[jest-config].{{newPath}}" instead.\n ↳ {{note}}', ConfigOptionUseBabelRcNote = 'See `babel-jest` related issue: https://github.com/facebook/jest/issues/3845', + GlobalsTsJestConfigOption = 'Define `ts-jest` config under `globals` is deprecated. Please do\n' + + 'transform: {\n' + + " : ['ts-jest', { /* ts-jest config goes here in Jest */ }],\n" + + '},', } /**