Skip to content

Commit

Permalink
refactor(config): remove support for astTransformers string array (#…
Browse files Browse the repository at this point in the history
…2129)

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"]
           }
        }
     }
  }
}
```
  • Loading branch information
ahnpnl authored Nov 11, 2020
1 parent 5bbfd06 commit 1e0b2ce
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 138 deletions.
57 changes: 0 additions & 57 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,63 +127,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
Expand Down
16 changes: 0 additions & 16 deletions e2e/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
25 changes: 0 additions & 25 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ describe('compilerModule', () => {
}) // compilerModule

describe('customTransformers', () => {
const logger = testing.createLoggerMock()

it.each([
{},
{
Expand Down Expand Up @@ -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: ['<rootDir>/__mocks__/dummy-transformer'],
},
resolve: null,
}).customTransformers,
).toMatchInlineSnapshot(`
Object {
"before": Array [
[Function],
[Function],
],
}
`)
})
})

describe('tsCompiler', () => {
Expand Down
61 changes: 23 additions & 38 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,49 +291,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),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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:
Expand Down
1 change: 0 additions & 1 deletion src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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',
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',
PackageJson = 'The option `packageJson` is deprecated and will be removed in ts-jest 27. This option is not used by internal `ts-jest`',
}

Expand Down

0 comments on commit 1e0b2ce

Please sign in to comment.