From 2c807d03a48dd49893f2b02fd7a2b267593b646d Mon Sep 17 00:00:00 2001 From: Ahn Date: Tue, 10 Nov 2020 11:43:28 +0100 Subject: [PATCH] refactor(config): remove support for `astTransformers` string array BREAKING CHANGE: One is defining ast transformers in `jest.config.js`/`package.json` should change to ``` // jest.config.js module.exports = { //... globals: { 'ts-jest': { astTransformers: { before: ['your_before_transformer_path'], after: ['your_after_transformer_path'], afterDeclarations: ['your_afterDeclarations_transformer_path'], } } } } ``` or ``` // package.json { "jest": { "globals": { "ts-jest": { "astTransformers": { "before": ["your_before_transformer_path"], "after": ["your_after_transformer_path"], "afterDeclarations": ["your_afterDeclarations_transformer_path"] } } } } } ``` --- .../__snapshots__/logger.test.ts.snap | 57 ----------------- e2e/__tests__/logger.test.ts | 16 ----- src/config/config-set.spec.ts | 25 -------- src/config/config-set.ts | 61 +++++++------------ src/types.ts | 2 +- src/utils/messages.ts | 1 - 6 files changed, 24 insertions(+), 138 deletions(-) diff --git a/e2e/__tests__/__snapshots__/logger.test.ts.snap b/e2e/__tests__/__snapshots__/logger.test.ts.snap index ada5ac4926..9f69ab1a8b 100644 --- a/e2e/__tests__/__snapshots__/logger.test.ts.snap +++ b/e2e/__tests__/__snapshots__/logger.test.ts.snap @@ -130,63 +130,6 @@ Array [ ] `; -exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "default" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers - PASS ./Hello.spec.ts - Hello Class - √ should create a new Hello - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers - PASS ./Hello.spec.ts - Hello Class - √ should create a new Hello - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7-string-config" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers - PASS ./Hello.spec.ts - Hello Class - √ should create a new Hello - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - exports[`ts-jest logging deprecation warning with packageJson config should pass using template "default" 1`] = ` √ jest ↳ exit code: 0 diff --git a/e2e/__tests__/logger.test.ts b/e2e/__tests__/logger.test.ts index 8c191f7b7a..80676ab89e 100644 --- a/e2e/__tests__/logger.test.ts +++ b/e2e/__tests__/logger.test.ts @@ -92,22 +92,6 @@ describe('ts-jest logging', () => { } describe('deprecation warning', () => { - describe('with astTransformers config as string array', () => { - const testCase = configureTestCase('simple', { - tsJestConfig: { - astTransformers: [] - } - }) - - testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot() - }) - }) - }) - describe('with packageJson config', () => { const testCase = configureTestCase('simple', { tsJestConfig: { diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 95b0b50dd1..1f155ffc67 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -154,8 +154,6 @@ describe('compilerModule', () => { }) // compilerModule describe('customTransformers', () => { - const logger = testing.createLoggerMock() - it.each([ {}, { @@ -189,29 +187,6 @@ describe('customTransformers', () => { expect(cs.customTransformers).toMatchSnapshot() }) - - it('should return an object containing all resolved transformers when astTransformers config is an array', () => { - expect( - createConfigSet({ - jestConfig: { - rootDir: 'src', - cwd: 'src', - } as any, - logger, - tsJestConfig: { - astTransformers: ['/__mocks__/dummy-transformer'], - }, - resolve: null, - }).customTransformers, - ).toMatchInlineSnapshot(` - Object { - "before": Array [ - [Function], - [Function], - ], - } - `) - }) }) describe('tsCompiler', () => { diff --git a/src/config/config-set.ts b/src/config/config-set.ts index ae736bdc89..0131e41332 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -294,49 +294,34 @@ export class ConfigSet { before: [hoisting(this)], } if (astTransformers) { - if (Array.isArray(astTransformers)) { - this.logger.warn(Deprecations.AstTransformerArrayConfig) + const resolveTransformers = (transformers: (string | AstTransformer)[]) => + transformers.map((transformer) => { + let transformerPath: string + if (typeof transformer === 'string') { + transformerPath = this.resolvePath(transformer, { nodeResolve: true }) - this.customTransformers = { - before: [ - ...this.customTransformers.before, - ...astTransformers.map((transformer) => { - const transformerPath = this.resolvePath(transformer, { nodeResolve: true }) - - return require(transformerPath).factory(this) - }), - ], - } - } else { - const resolveTransformers = (transformers: (string | AstTransformer)[]) => - transformers.map((transformer) => { - let transformerPath: string - if (typeof transformer === 'string') { - transformerPath = this.resolvePath(transformer, { nodeResolve: true }) - - return require(transformerPath).factory(this) - } else { - transformerPath = this.resolvePath(transformer.path, { nodeResolve: true }) + return require(transformerPath).factory(this) + } else { + transformerPath = this.resolvePath(transformer.path, { nodeResolve: true }) - return require(transformerPath).factory(this, transformer.options) - } - }) - if (astTransformers.before) { - this.customTransformers = { - before: [...this.customTransformers.before, ...resolveTransformers(astTransformers.before)], + return require(transformerPath).factory(this, transformer.options) } + }) + if (astTransformers.before) { + this.customTransformers = { + before: [...this.customTransformers.before, ...resolveTransformers(astTransformers.before)], } - if (astTransformers.after) { - this.customTransformers = { - ...this.customTransformers, - after: resolveTransformers(astTransformers.after), - } + } + if (astTransformers.after) { + this.customTransformers = { + ...this.customTransformers, + after: resolveTransformers(astTransformers.after), } - if (astTransformers.afterDeclarations) { - this.customTransformers = { - ...this.customTransformers, - afterDeclarations: resolveTransformers(astTransformers.afterDeclarations), - } + } + if (astTransformers.afterDeclarations) { + this.customTransformers = { + ...this.customTransformers, + afterDeclarations: resolveTransformers(astTransformers.afterDeclarations), } } } diff --git a/src/types.ts b/src/types.ts index 1e930234ec..ce4c9a6d1b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -87,7 +87,7 @@ export interface TsJestGlobalOptions { /** * Custom transformers (mostly used by jest presets) */ - astTransformers?: string[] | ConfigCustomTransformer + astTransformers?: ConfigCustomTransformer /** * TS diagnostics - less to be reported if `isolatedModules` is `true`. It can be: diff --git a/src/utils/messages.ts b/src/utils/messages.ts index a699df89d4..f521150ef0 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -37,7 +37,6 @@ export const enum Deprecations { 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', HelperMovedToUtils = "The `{{helper}}` helper has been moved to `ts-jest/utils`. Use `import { {{helper}} } from 'ts-jest/utils'` instead.", - AstTransformerArrayConfig = 'The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers', TsConfig = 'The option `tsConfig` is deprecated and will be removed in ts-jest 27, use `tsconfig` instead', PackageJson = 'The option `packageJson` is deprecated and will be removed in ts-jest 27. This option is not used by internal `ts-jest`', }