Skip to content

Commit

Permalink
fix(cli): add migration tsConfig option for ts-jest config options (
Browse files Browse the repository at this point in the history
#2794)

Closes #2764
  • Loading branch information
ahnpnl committed Aug 3, 2021
1 parent 5d6e514 commit 781710b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/config/config-set.spec.ts
Expand Up @@ -51,7 +51,7 @@ describe('parsedTsConfig', () => {
})

it('should fallback to ES2015 as default target and CommonJS as default module when no target or module defined in tsconfig', () => {
const compilerOptions = get({ tsconfig: 'tsconfig.spec.json' }).options
const compilerOptions = get({ tsconfig: 'src/__mocks__/tsconfig-mocks.json' }).options

expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2015)
expect(compilerOptions.module).toBe(ts.ModuleKind.CommonJS)
Expand Down
29 changes: 29 additions & 0 deletions src/utils/__snapshots__/backports.spec.ts.snap
Expand Up @@ -233,6 +233,35 @@ Array [
]
`;
exports[`backportJestConfig with "globals.ts-jest.tsConfig" set to 'tsconfig.build.json' should have changed the config correctly: before 1`] = `
Object {
"globals": Object {
"ts-jest": Object {
"tsConfig": "tsconfig.build.json",
},
},
}
`;
exports[`backportJestConfig with "globals.ts-jest.tsConfig" set to 'tsconfig.build.json' should have changed the config correctly: migrated 1`] = `
Object {
"globals": Object {
"ts-jest": Object {
"tsconfig": "tsconfig.build.json",
},
},
}
`;
exports[`backportJestConfig with "globals.ts-jest.tsConfig" set to 'tsconfig.build.json' should warn the user 1`] = `
Array [
"[level:40] \\"[jest-config].globals.ts-jest.tsConfig\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsconfig\\" instead.
",
"[level:40] Your Jest configuration is outdated. Use the CLI to help migrating it: ts-jest config:migrate <config-file>.
",
]
`;
exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should have changed the config correctly: before 1`] = `
Object {
"globals": Object {
Expand Down
25 changes: 15 additions & 10 deletions src/utils/backports.spec.ts
@@ -1,7 +1,7 @@
import { inspect } from 'util'

import { testing } from 'bs-logger'
import set = require('lodash/set')
import set from 'lodash/set'

import { backportJestConfig } from './backports'

Expand All @@ -13,20 +13,23 @@ beforeEach(() => {
})

describe('backportJestConfig', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const makeTestsFor = (oldPath: string, _: string, values: any[]) => {
const makeTestsFor = (oldPath: string, values: unknown[]) => {
values.forEach((val) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let original: any

beforeEach(() => {
original = {}
set(original, oldPath, val)
})

describe(`with "${oldPath}" set to ${inspect(val)}`, () => {
it('should warn the user', () => {
backportJestConfig(logger, original)

expect(logTarget.lines.warn).toMatchSnapshot()
}) // should warn the user

it('should have changed the config correctly', () => {
expect(original).toMatchSnapshot('before')
expect(backportJestConfig(logger, original)).toMatchSnapshot('migrated')
Expand All @@ -35,17 +38,19 @@ describe('backportJestConfig', () => {
}) // for
} // makeTestsFor

makeTestsFor('globals.__TS_CONFIG__', 'globals.ts-jest.tsConfig', [{ foo: 'bar' }])
makeTestsFor('globals.__TS_CONFIG__', [{ foo: 'bar' }])

makeTestsFor('globals.__TRANSFORM_HTML__', [true, false])

makeTestsFor('globals.__TRANSFORM_HTML__', 'globals.ts-jest.stringifyContentPathRegex', [true, false])
makeTestsFor('globals.ts-jest.tsConfigFile', ['tsconfig.build.json'])

makeTestsFor('globals.ts-jest.tsConfigFile', 'globals.ts-jest.tsConfig', ['tsconfig.build.json'])
makeTestsFor('globals.ts-jest.tsConfig', ['tsconfig.build.json'])

makeTestsFor('globals.ts-jest.enableTsDiagnostics', 'globals.ts-jest.diagnostics', [true, false, '\\.spec\\.ts$'])
makeTestsFor('globals.ts-jest.enableTsDiagnostics', [true, false, '\\.spec\\.ts$'])

makeTestsFor('globals.ts-jest.useBabelrc', 'globals.ts-jest.babelConfig', [true, false])
makeTestsFor('globals.ts-jest.useBabelrc', [true, false])

makeTestsFor('globals.ts-jest.typeCheck', 'globals.ts-jest.isolatedModules', [true, false])
makeTestsFor('globals.ts-jest.typeCheck', [true, false])

makeTestsFor('globals.ts-jest.skipBabel', 'globals.ts-jest.babelConfig', [true, false])
makeTestsFor('globals.ts-jest.skipBabel', [true, false])
})
8 changes: 8 additions & 0 deletions src/utils/backports.ts
Expand Up @@ -62,6 +62,14 @@ export const backportJestConfig = <T extends Config.InitialOptions | Config.Proj
delete tsJest.tsConfigFile
}

if ('tsConfig' in tsJest) {
warnConfig('globals.ts-jest.tsConfig', 'globals.ts-jest.tsconfig')
if (tsJest.tsConfig) {
mergeTsJest.tsconfig = tsJest.tsConfig
}
delete tsJest.tsConfig
}

if ('enableTsDiagnostics' in tsJest) {
warnConfig('globals.ts-jest.enableTsDiagnostics', 'globals.ts-jest.diagnostics')
if (tsJest.enableTsDiagnostics) {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.build.json
@@ -1,7 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES5",
"sourceMap": false,
"inlineSources": false,
"inlineSourceMap": false,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES5",
"declaration": false,
"noEmit": true,
"downlevelIteration": true,
Expand Down

0 comments on commit 781710b

Please sign in to comment.