Skip to content

Commit e6b42fc

Browse files
authored
feat: allow opt-out version warning message by environment variable TS_JEST_DISABLE_VER_CHECKER (#1821)
Close #1774
1 parent d8367db commit e6b42fc

7 files changed

Lines changed: 119 additions & 45 deletions

File tree

docs/user/config/index.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ module.exports = {
147147

148148
### Introduction
149149

150-
All configuration of `ts-jest` specific options are located under `globals.ts-jest` path of your Jest config:
150+
All configuration of `ts-jest` specific options locate under `globals.ts-jest` path of your Jest config:
151151

152152
<div class="row"><div class="col-md-6" markdown="block">
153153

@@ -183,7 +183,7 @@ module.exports = {
183183

184184
#### IDE `ts-jest` config suggestion
185185

186-
To ultilize IDE suggestion, you can use `JSDOC` to provide suggested `ts-jest` configs for your Jest config:
186+
To utilize IDE suggestion, you can use `JSDOC` to provide suggested `ts-jest` configs for your Jest config:
187187

188188
```js
189189
/** @typedef {import('ts-jest')} */
@@ -215,7 +215,29 @@ All options have default values which should fit most of the projects. Click on
215215
| [**`stringifyContentPathRegex`**][stringifyContentPathRegex] | [Files which will become modules returning self content.][stringifyContentPathRegex] | `string`\|`RegExp` | _disabled_ |
216216
| [**`packageJson`**][packageJson] | [Package metadata.][packageJson] | `string`\|`object`\|`boolean` | _auto_ |
217217

218-
### Upgrading
218+
## Version checking
219+
220+
By default, `ts-jest` supports a range of versions for `jest`/`typescript`. One uses incompatible versions will receive a warning
221+
message while running tests. This warning message can be opt-out by setting environment variable `TS_JEST_DISABLE_VER_CHECKER`:
222+
223+
**Linux/MacOS**
224+
225+
```
226+
export TS_JEST_DISABLE_VER_CHECKER=true
227+
```
228+
229+
**Windows**
230+
231+
```
232+
set TS_JEST_DISABLE_VER_CHECKER=true
233+
```
234+
235+
### Note
236+
237+
As long as the environment variable `TS_JEST_DISABLE_VER_CHECKER` stays, the warning message will no longer show.
238+
This can lead to unexpected errors due to the usage of incompatible versions' dependencies. Use this environment variable with precautions.
239+
240+
## Upgrading
219241

220242
You can use the `config:migrate` tool of `ts-jest` CLI if you're coming from an older version to help you migrate your Jest configuration.
221243

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Array [
124124
]
125125
`;
126126
127-
exports[`ts-jest logging with unsupported version test should pass using template "with-unsupported-version" 1`] = `
127+
exports[`ts-jest logging with unsupported version test with TS_JEST_DISABLE_VER_CHECKER is not set in process.env should pass using template "with-unsupported-version" 1`] = `
128128
√ jest
129129
↳ exit code: 0
130130
===[ STDOUT ]===================================================================
@@ -142,3 +142,21 @@ exports[`ts-jest logging with unsupported version test should pass using templat
142142
Ran all test suites.
143143
================================================================================
144144
`;
145+
146+
exports[`ts-jest logging with unsupported version test with TS_JEST_DISABLE_VER_CHECKER is set in process.env should pass using template "with-unsupported-version" 1`] = `
147+
√ jest
148+
↳ exit code: 0
149+
===[ STDOUT ]===================================================================
150+
151+
===[ STDERR ]===================================================================
152+
PASS ./Hello.spec.ts
153+
Hello Class
154+
√ should create a new Hello
155+
156+
Test Suites: 1 passed, 1 total
157+
Tests: 1 passed, 1 total
158+
Snapshots: 0 total
159+
Time: XXs
160+
Ran all test suites.
161+
================================================================================
162+
`;

e2e/__tests__/logger.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,27 @@ describe('ts-jest logging', () => {
88
describe('with unsupported version test', () => {
99
const testCase = configureTestCase('simple')
1010

11-
testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => {
12-
it(testLabel, () => {
13-
const result = runTest()
14-
expect(result.status).toBe(0)
15-
expect(result).toMatchSnapshot()
11+
describe('with TS_JEST_DISABLE_VER_CHECKER is set in process.env', () => {
12+
testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => {
13+
it(testLabel, () => {
14+
process.env.TS_JEST_DISABLE_VER_CHECKER = 'true'
15+
16+
const result = runTest()
17+
expect(result.status).toBe(0)
18+
expect(result).toMatchSnapshot()
19+
20+
delete process.env.TS_JEST_DISABLE_VER_CHECKER
21+
})
22+
})
23+
})
24+
25+
describe('with TS_JEST_DISABLE_VER_CHECKER is not set in process.env', () => {
26+
testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => {
27+
it(testLabel, () => {
28+
const result = runTest()
29+
expect(result.status).toBe(0)
30+
expect(result).toMatchSnapshot()
31+
})
1632
})
1733
})
1834
})

src/util/__snapshots__/backports.spec.ts.snap

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Object {
1616
}
1717
`;
1818

19-
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should wran the user 1`] = `
19+
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should warn the user 1`] = `
2020
Array [
2121
"[level:40] \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead.
2222
",
@@ -43,7 +43,7 @@ Object {
4343
}
4444
`;
4545
46-
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should wran the user 1`] = `
46+
exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should warn the user 1`] = `
4747
Array [
4848
"[level:40] \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead.
4949
",
@@ -74,7 +74,7 @@ Object {
7474
}
7575
`;
7676
77-
exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should wran the user 1`] = `
77+
exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should warn the user 1`] = `
7878
Array [
7979
"[level:40] \\"[jest-config].globals.__TS_CONFIG__\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead.
8080
",
@@ -106,7 +106,7 @@ Object {
106106
}
107107
`;
108108
109-
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should wran the user 1`] = `
109+
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should warn the user 1`] = `
110110
Array [
111111
"[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead.
112112
",
@@ -135,7 +135,7 @@ Object {
135135
}
136136
`;
137137
138-
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should wran the user 1`] = `
138+
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should warn the user 1`] = `
139139
Array [
140140
"[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead.
141141
",
@@ -166,7 +166,7 @@ Object {
166166
}
167167
`;
168168
169-
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should wran the user 1`] = `
169+
exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should warn the user 1`] = `
170170
Array [
171171
"[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead.
172172
",
@@ -195,7 +195,7 @@ Object {
195195
}
196196
`;
197197
198-
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should wran the user 1`] = `
198+
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should warn the user 1`] = `
199199
Array [
200200
"[level:40] \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
201201
",
@@ -222,7 +222,7 @@ Object {
222222
}
223223
`;
224224
225-
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should wran the user 1`] = `
225+
exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should warn the user 1`] = `
226226
Array [
227227
"[level:40] \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
228228
",
@@ -251,7 +251,7 @@ Object {
251251
}
252252
`;
253253
254-
exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should wran the user 1`] = `
254+
exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should warn the user 1`] = `
255255
Array [
256256
"[level:40] \\"[jest-config].globals.ts-jest.tsConfigFile\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead.
257257
",
@@ -280,7 +280,7 @@ Object {
280280
}
281281
`;
282282
283-
exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to false should wran the user 1`] = `
283+
exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to false should warn the user 1`] = `
284284
Array [
285285
"[level:40] \\"[jest-config].globals.ts-jest.typeCheck\\" is deprecated, use \\"[jest-config].globals.ts-jest.isolatedModules\\" instead.
286286
",
@@ -309,7 +309,7 @@ Object {
309309
}
310310
`;
311311
312-
exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to true should wran the user 1`] = `
312+
exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to true should warn the user 1`] = `
313313
Array [
314314
"[level:40] \\"[jest-config].globals.ts-jest.typeCheck\\" is deprecated, use \\"[jest-config].globals.ts-jest.isolatedModules\\" instead.
315315
",
@@ -338,7 +338,7 @@ Object {
338338
}
339339
`;
340340
341-
exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should wran the user 1`] = `
341+
exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should warn the user 1`] = `
342342
Array [
343343
"[level:40] \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
344344
↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845
@@ -368,7 +368,7 @@ Object {
368368
}
369369
`;
370370
371-
exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to true should wran the user 1`] = `
371+
exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to true should warn the user 1`] = `
372372
Array [
373373
"[level:40] \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead.
374374
↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845

src/util/backports.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('backportJestConfig', () => {
2020
set(original, oldPath, val)
2121
})
2222
describe(`with "${oldPath}" set to ${inspect(val)}`, () => {
23-
it('should wran the user', () => {
23+
it('should warn the user', () => {
2424
backportJestConfig(logger, original)
2525
expect(logTarget.lines.warn).toMatchSnapshot()
2626
}) // should warn the user

src/util/version-checkers.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,29 @@ function describeChecker(
4242
expect(warnings).toHaveLength(1)
4343
expect(warnings[0].message).toMatch(testVersion ? 'has not been tested with ts-jest' : 'is not installed')
4444
})
45+
4546
it('should log only once with warn()', () => {
4647
checker.warn()
4748
checker.warn()
4849
expect(logTarget.messages.warn).toHaveLength(1)
4950
})
51+
5052
it('should throw with raise()', () => {
5153
expect(checker.raise).toThrow()
5254
// adds another time as it should throw all the time even if already called
5355
expect(checker.raise).toThrow()
5456
})
57+
58+
it('should not log or throw when TS_JEST_DISABLE_VER_CHECKER is set in process.env', () => {
59+
process.env.TS_JEST_DISABLE_VER_CHECKER = 'true'
60+
61+
checker.warn()
62+
63+
expect(logTarget.messages.warn).toHaveLength(0)
64+
expect(checker.raise).not.toThrow()
65+
66+
delete process.env.TS_JEST_DISABLE_VER_CHECKER
67+
})
5568
}) // describe unsupported version
5669
}) // unsupported versions loop
5770

@@ -60,10 +73,12 @@ function describeChecker(
6073
beforeEach(() => {
6174
pv.getPackageVersion.mockImplementation((name) => (name === moduleName ? testVersion : undefined))
6275
})
76+
6377
it('should not log with warn()', () => {
6478
checker.warn()
6579
expect(logTarget.messages.warn).toHaveLength(0)
6680
})
81+
6782
it('should not throw with raise()', () => {
6883
expect(checker.raise).not.toThrow()
6984
})

src/util/version-checkers.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,34 @@ function checkVersion(
4747
expectedRange: string,
4848
action: CheckVersionAction | undefined = 'warn',
4949
): boolean | never {
50-
const version = getPackageVersion(name)
51-
const success = !!version && satisfies(version, expectedRange)
50+
let success = true
51+
if (!('TS_JEST_DISABLE_VER_CHECKER' in process.env)) {
52+
const version = getPackageVersion(name)
53+
success = !!version && satisfies(version, expectedRange)
5254

53-
logger.debug(
54-
{
55-
actualVersion: version,
56-
expectedVersion: expectedRange,
57-
},
58-
'checking version of %s: %s',
59-
name,
60-
success ? 'OK' : 'NOT OK',
61-
)
55+
logger.debug(
56+
{
57+
actualVersion: version,
58+
expectedVersion: expectedRange,
59+
},
60+
'checking version of %s: %s',
61+
name,
62+
success ? 'OK' : 'NOT OK',
63+
)
6264

63-
if (!action || success) return success
65+
if (!action || success) return success
6466

65-
const message = interpolate(version ? Errors.UntestedDependencyVersion : Errors.MissingDependency, {
66-
module: name,
67-
actualVersion: version || '??',
68-
expectedVersion: rangeToHumanString(expectedRange),
69-
})
70-
if (action === 'warn') {
71-
logger.warn(message)
72-
} else if (action === 'throw') {
73-
logger.fatal(message)
74-
throw new RangeError(message)
67+
const message = interpolate(version ? Errors.UntestedDependencyVersion : Errors.MissingDependency, {
68+
module: name,
69+
actualVersion: version || '??',
70+
expectedVersion: rangeToHumanString(expectedRange),
71+
})
72+
if (action === 'warn') {
73+
logger.warn(message)
74+
} else if (action === 'throw') {
75+
logger.fatal(message)
76+
throw new RangeError(message)
77+
}
7578
}
7679

7780
return success

0 commit comments

Comments
 (0)