Skip to content

Commit e512bc0

Browse files
authoredJun 14, 2020
fix(config): show version warning when using ts-jest without babel (#1729)
Fixes [undefined target](#1678 (comment)) Fixes [usage-with-Babel](#1678 (comment))
1 parent 3f39b52 commit e512bc0

16 files changed

+2918
-572
lines changed
 

‎e2e/__templates__/default/package-lock.json

+105-105
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎e2e/__templates__/default/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0-tmpl.0",
44
"private": true,
55
"devDependencies": {
6-
"@types/jest": "25.2.3",
7-
"@types/node": "12.12.44",
6+
"@types/jest": "26.0.0",
7+
"@types/node": "12.12.47",
88
"jest": "26.0.1",
99
"typescript": "3.9.5"
1010
},
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
module.exports = {
2+
presets: [
3+
"@babel/preset-env"
4+
]
25
}

‎e2e/__templates__/with-babel-7-string-config/package-lock.json

+1,242-133
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎e2e/__templates__/with-babel-7-string-config/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
"private": true,
55
"devDependencies": {
66
"@babel/core": "7.10.2",
7-
"@types/jest": "25.2.3",
8-
"@types/node": "12.12.44",
7+
"@babel/preset-env": "7.10.2",
8+
"@types/jest": "26.0.0",
9+
"@types/node": "12.12.47",
910
"babel-jest": "26.0.1",
1011
"jest": "26.0.1",
1112
"typescript": "3.9.5"
1213
},
1314
"wantedDependencies": {
1415
"@babel/core": "7",
16+
"@babel/preset-env": "7",
1517
"@types/jest": "latest",
1618
"@types/node": "12",
1719
"babel-jest": "latest",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
presets: [
3+
"@babel/preset-env"
4+
]
5+
}

‎e2e/__templates__/with-babel-7/package-lock.json

+1,242-133
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎e2e/__templates__/with-babel-7/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
"private": true,
55
"devDependencies": {
66
"@babel/core": "7.10.2",
7-
"@types/jest": "25.2.3",
8-
"@types/node": "12.12.44",
7+
"@babel/preset-env": "7.10.2",
8+
"@types/jest": "26.0.0",
9+
"@types/node": "12.12.47",
910
"babel-jest": "26.0.1",
1011
"jest": "26.0.1",
1112
"typescript": "3.9.5"
1213
},
1314
"wantedDependencies": {
1415
"@babel/core": "7",
16+
"@babel/preset-env": "7",
1517
"@types/jest": "latest",
1618
"@types/node": "12",
1719
"babel-jest": "latest",

‎e2e/__templates__/with-unsupported-version/package-lock.json

+105-105
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎e2e/__templates__/with-unsupported-version/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0-tmpl.0",
44
"private": true,
55
"devDependencies": {
6-
"@types/jest": "25.2.3",
7-
"@types/node": "12.12.44",
6+
"@types/jest": "26.0.0",
7+
"@types/node": "12.12.47",
88
"jest": "26.0.1",
99
"typescript": "3.3.4000"
1010
},

‎e2e/__tests__/__snapshots__/hoisting.test.ts.snap

+54-48
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ts-jest/presets/js-with-babel should pass using template "with-babel-7" 1`] = `
4+
√ jest
5+
↳ exit code: 0
6+
===[ STDOUT ]===================================================================
7+
8+
===[ STDERR ]===================================================================
9+
PASS ./main.spec.js
10+
√ spread
11+
12+
Test Suites: 1 passed, 1 total
13+
Tests: 1 passed, 1 total
14+
Snapshots: 0 total
15+
Time: XXs
16+
Ran all test suites.
17+
================================================================================
18+
`;
19+
20+
exports[`ts-jest/presets/js-with-babel should pass using template "with-babel-7-string-config" 1`] = `
21+
√ jest
22+
↳ exit code: 0
23+
===[ STDOUT ]===================================================================
24+
25+
===[ STDERR ]===================================================================
26+
PASS ./main.spec.js
27+
√ spread
28+
29+
Test Suites: 1 passed, 1 total
30+
Tests: 1 passed, 1 total
31+
Snapshots: 0 total
32+
Time: XXs
33+
Ran all test suites.
34+
================================================================================
35+
`;

‎e2e/__tests__/jest-presets.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { allPackageSetsWithPreset } from '../__helpers__/templates'
1+
import { PackageSets } from '../__helpers__/templates'
22
import { configureTestCase } from '../__helpers__/test-case'
33

44
// 'ts-jest' is tested in almost all test cases
@@ -8,11 +8,19 @@ import { configureTestCase } from '../__helpers__/test-case'
88
describe('ts-jest/presets/js-with-babel', () => {
99
const testCase = configureTestCase('preset-with-babel', { jestConfig: { preset: 'ts-jest/presets/js-with-babel' } })
1010

11-
testCase.runWithTemplates(allPackageSetsWithPreset, 1, (runTest, { testLabel }) => {
11+
testCase.runWithTemplates([PackageSets.default], 1, (runTest, { testLabel }) => {
1212
it(testLabel, () => {
1313
const result = runTest()
1414
expect(result.status).toBe(1)
1515
expect(result.stderr).toMatch(/(Couldn't|Cannot) find (preset|module) ["']@babel\/preset-env["']/)
1616
})
1717
})
18+
19+
testCase.runWithTemplates([PackageSets.babel7, PackageSets.babel7StringConfig], 0, (runTest, { testLabel }) => {
20+
it(testLabel, () => {
21+
const result = runTest()
22+
expect(result.status).toBe(0)
23+
expect(result).toMatchSnapshot()
24+
})
25+
})
1826
})

‎e2e/tsconfig.spec.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": []
4+
}

‎src/config/config-set.spec.ts

+90-35
Original file line numberDiff line numberDiff line change
@@ -802,18 +802,7 @@ describe('readTsConfig', () => {
802802

803803
describe('mismatch nodejs version and typescript target', () => {
804804
const logTarget = logTargetMock()
805-
806-
beforeEach(() => {
807-
logTarget.clear()
808-
cs = createConfigSet({ jestConfig: { rootDir: '/root', cwd: '/cwd' } as any })
809-
findConfig.mockImplementation((p: string) => `${p}/tsconfig.json`)
810-
})
811-
812-
afterEach(() => {
813-
findConfig.mockClear()
814-
})
815-
816-
function mismatchTestCaseContent(tsTarget: string, scriptTarget: ts.ScriptTarget) {
805+
function mismatchTestCaseContent(rawTarget: string | undefined, scriptTarget: ts.ScriptTarget) {
817806
parseConfig.mockImplementation((conf: any) => ({
818807
options: {
819808
...conf,
@@ -822,35 +811,101 @@ describe('readTsConfig', () => {
822811
fileNames: [],
823812
errors: [],
824813
}))
825-
readConfig.mockImplementation((p) => ({ config: { path: p, compilerOptions: { target: tsTarget } } }))
814+
readConfig.mockImplementation((p) => ({ config: { path: p, compilerOptions: { target: rawTarget } } }))
826815

827816
cs.readTsConfig()
828-
829-
// expect.toEqual gives weird result here so toContain is workaround for it.
830-
expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toContain(
831-
'[level:40] There is a mismatch between your ' +
832-
`NodeJs version ${process.version} and your TypeScript target ${tsTarget}. This might lead to some unexpected errors ` +
833-
'when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
834-
)
835-
836-
parseConfig.mockClear()
837-
readConfig.mockClear()
838817
}
839818

840-
/**
841-
* It seems like not possible to mock process.version so the condition here is needed
842-
*/
843-
if (process.version.startsWith('v10')) {
844-
// eslint-disable-next-line jest/expect-expect
845-
it('should show warning message when nodejs version is 10 and typescript target is higher than es2018', () => {
846-
mismatchTestCaseContent('es2019', ts.ScriptTarget.ES2019)
819+
describe.each([
820+
{ jestConfig: { rootDir: '/root', cwd: '/cwd' } as any, tsJestConfig: { babelConfig: true } },
821+
{ jestConfig: { rootDir: '/root', cwd: '/cwd' } as any },
822+
])('toggle warning message for users who are using ts-jest with babel or without babel', (config) => {
823+
const shouldAction = config.tsJestConfig?.babelConfig ? `shouldn't` : 'should'
824+
beforeEach(() => {
825+
logTarget.clear()
826+
cs = createConfigSet(config)
827+
findConfig.mockImplementation((p: string) => `${p}/tsconfig.json`)
847828
})
848-
} else {
849-
// eslint-disable-next-line jest/expect-expect
850-
it('should show warning message when nodejs version is 12 and typescript target is higher than es2019', () => {
851-
mismatchTestCaseContent('es2020', ts.ScriptTarget.ES2020)
829+
830+
afterEach(() => {
831+
findConfig.mockClear()
832+
parseConfig.mockClear()
833+
readConfig.mockClear()
852834
})
853-
}
835+
836+
/**
837+
* It seems like not possible to mock process.version so the condition here is needed
838+
*/
839+
if (process.version.startsWith('v10')) {
840+
it(
841+
`${shouldAction} show warning message when nodejs version is 10 and typescript target is higher than es2018` +
842+
` with tsconfig contains target`,
843+
() => {
844+
mismatchTestCaseContent('es2019', ts.ScriptTarget.ES2019)
845+
// eslint-disable-next-line
846+
config.tsJestConfig?.babelConfig
847+
? expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toBeUndefined()
848+
: // expect.toEqual gives weird result here so toContain is workaround for it.
849+
expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toContain(
850+
'[level:40] There is a mismatch between your ' +
851+
`NodeJs version ${process.version} and your TypeScript target es2019. This might lead to some unexpected errors ` +
852+
'when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
853+
)
854+
},
855+
)
856+
857+
it(
858+
`${shouldAction} show warning message when nodejs version is 10 and typescript target is higher than es2018` +
859+
` with tsconfig doesn't contain target`,
860+
() => {
861+
mismatchTestCaseContent(undefined, ts.ScriptTarget.ES2019)
862+
// eslint-disable-next-line
863+
config.tsJestConfig?.babelConfig
864+
? expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toBeUndefined()
865+
: // expect.toEqual gives weird result here so toContain is workaround for it.
866+
expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toContain(
867+
'[level:40] There is a mismatch between your ' +
868+
`NodeJs version ${process.version} and your TypeScript target es2019. This might lead to some unexpected errors ` +
869+
'when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
870+
)
871+
},
872+
)
873+
} else {
874+
it(
875+
`${shouldAction} show warning message when nodejs version is 12 and typescript target is higher than es2019` +
876+
` with tsconfig contains target`,
877+
() => {
878+
mismatchTestCaseContent('es2020', ts.ScriptTarget.ES2020)
879+
// eslint-disable-next-line
880+
config.tsJestConfig?.babelConfig
881+
? expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toBeUndefined()
882+
: // expect.toEqual gives weird result here so toContain is workaround for it.
883+
expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toContain(
884+
'[level:40] There is a mismatch between your ' +
885+
`NodeJs version ${process.version} and your TypeScript target es2020. This might lead to some unexpected errors ` +
886+
'when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
887+
)
888+
},
889+
)
890+
891+
it(
892+
`${shouldAction} show warning message when nodejs version is 12 and typescript target is higher than es2019` +
893+
` with tsconfig doesn't target`,
894+
() => {
895+
mismatchTestCaseContent(undefined, ts.ScriptTarget.ES2020)
896+
// eslint-disable-next-line
897+
config.tsJestConfig?.babelConfig
898+
? expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toBeUndefined()
899+
: // expect.toEqual gives weird result here so toContain is workaround for it.
900+
expect(logTarget.filteredLines(LogLevels.warn, Infinity)[0]).toContain(
901+
'[level:40] There is a mismatch between your ' +
902+
`NodeJs version ${process.version} and your TypeScript target es2020. This might lead to some unexpected errors ` +
903+
'when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
904+
)
905+
},
906+
)
907+
}
908+
})
854909
})
855910
}) // readTsConfig
856911

‎src/config/config-set.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export const IGNORE_DIAGNOSTIC_CODES = [
7070
*/
7171
export const TS_JEST_OUT_DIR = '$$ts-jest$$'
7272

73+
const TARGET_TO_VERSION_MAPPING: Record<number, string> = {
74+
[ScriptTarget.ES2018]: 'es2018',
75+
[ScriptTarget.ES2019]: 'es2019',
76+
[ScriptTarget.ES2020]: 'es2020',
77+
[ScriptTarget.ESNext]: 'ESNext',
78+
}
79+
7380
/**
7481
* @internal
7582
*/
@@ -810,12 +817,13 @@ export class ConfigSet {
810817
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
811818
const compilationTarget = result.options.target!
812819
if (
813-
(nodeJsVer.startsWith('v10') && compilationTarget > ScriptTarget.ES2018) ||
814-
(nodeJsVer.startsWith('v12') && compilationTarget > ScriptTarget.ES2019)
820+
!this.tsJest.babelConfig &&
821+
((nodeJsVer.startsWith('v10') && compilationTarget > ScriptTarget.ES2018) ||
822+
(nodeJsVer.startsWith('v12') && compilationTarget > ScriptTarget.ES2019))
815823
) {
816824
const message = interpolate(Errors.MismatchNodeTargetMapping, {
817825
nodeJsVer: process.version,
818-
compilationTarget: config.compilerOptions.target,
826+
compilationTarget: config.compilerOptions.target ?? TARGET_TO_VERSION_MAPPING[compilationTarget],
819827
})
820828
logger.warn(message)
821829
}

0 commit comments

Comments
 (0)
Please sign in to comment.