From e6b42fcd7a75c7b14e636a45cda04de18a46908b Mon Sep 17 00:00:00 2001 From: Ahn Date: Tue, 21 Jul 2020 18:41:57 +0200 Subject: [PATCH] feat: allow opt-out version warning message by environment variable `TS_JEST_DISABLE_VER_CHECKER` (#1821) Close #1774 --- docs/user/config/index.md | 28 +++++++++-- .../__snapshots__/logger.test.ts.snap | 20 +++++++- e2e/__tests__/logger.test.ts | 26 ++++++++-- src/util/__snapshots__/backports.spec.ts.snap | 26 +++++----- src/util/backports.spec.ts | 2 +- src/util/version-checkers.spec.ts | 15 ++++++ src/util/version-checkers.ts | 47 ++++++++++--------- 7 files changed, 119 insertions(+), 45 deletions(-) diff --git a/docs/user/config/index.md b/docs/user/config/index.md index afa854dd34..7facb3eb1d 100644 --- a/docs/user/config/index.md +++ b/docs/user/config/index.md @@ -147,7 +147,7 @@ module.exports = { ### Introduction -All configuration of `ts-jest` specific options are located under `globals.ts-jest` path of your Jest config: +All configuration of `ts-jest` specific options locate under `globals.ts-jest` path of your Jest config:
@@ -183,7 +183,7 @@ module.exports = { #### IDE `ts-jest` config suggestion -To ultilize IDE suggestion, you can use `JSDOC` to provide suggested `ts-jest` configs for your Jest config: +To utilize IDE suggestion, you can use `JSDOC` to provide suggested `ts-jest` configs for your Jest config: ```js /** @typedef {import('ts-jest')} */ @@ -215,7 +215,29 @@ All options have default values which should fit most of the projects. Click on | [**`stringifyContentPathRegex`**][stringifyContentPathRegex] | [Files which will become modules returning self content.][stringifyContentPathRegex] | `string`\|`RegExp` | _disabled_ | | [**`packageJson`**][packageJson] | [Package metadata.][packageJson] | `string`\|`object`\|`boolean` | _auto_ | -### Upgrading +## Version checking + +By default, `ts-jest` supports a range of versions for `jest`/`typescript`. One uses incompatible versions will receive a warning +message while running tests. This warning message can be opt-out by setting environment variable `TS_JEST_DISABLE_VER_CHECKER`: + +**Linux/MacOS** + +``` +export TS_JEST_DISABLE_VER_CHECKER=true +``` + +**Windows** + +``` +set TS_JEST_DISABLE_VER_CHECKER=true +``` + +### Note + +As long as the environment variable `TS_JEST_DISABLE_VER_CHECKER` stays, the warning message will no longer show. +This can lead to unexpected errors due to the usage of incompatible versions' dependencies. Use this environment variable with precautions. + +## Upgrading 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. diff --git a/e2e/__tests__/__snapshots__/logger.test.ts.snap b/e2e/__tests__/__snapshots__/logger.test.ts.snap index e584da1cba..c4035da845 100644 --- a/e2e/__tests__/__snapshots__/logger.test.ts.snap +++ b/e2e/__tests__/__snapshots__/logger.test.ts.snap @@ -124,7 +124,7 @@ Array [ ] `; -exports[`ts-jest logging with unsupported version test should pass using template "with-unsupported-version" 1`] = ` +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`] = ` √ jest ↳ exit code: 0 ===[ STDOUT ]=================================================================== @@ -142,3 +142,21 @@ exports[`ts-jest logging with unsupported version test should pass using templat Ran all test suites. ================================================================================ `; + +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`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + 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. + ================================================================================ +`; diff --git a/e2e/__tests__/logger.test.ts b/e2e/__tests__/logger.test.ts index 25f5014d73..04c25bab78 100644 --- a/e2e/__tests__/logger.test.ts +++ b/e2e/__tests__/logger.test.ts @@ -8,11 +8,27 @@ describe('ts-jest logging', () => { describe('with unsupported version test', () => { const testCase = configureTestCase('simple') - testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot() + describe('with TS_JEST_DISABLE_VER_CHECKER is set in process.env', () => { + testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => { + it(testLabel, () => { + process.env.TS_JEST_DISABLE_VER_CHECKER = 'true' + + const result = runTest() + expect(result.status).toBe(0) + expect(result).toMatchSnapshot() + + delete process.env.TS_JEST_DISABLE_VER_CHECKER + }) + }) + }) + + describe('with TS_JEST_DISABLE_VER_CHECKER is not set in process.env', () => { + testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest() + expect(result.status).toBe(0) + expect(result).toMatchSnapshot() + }) }) }) }) diff --git a/src/util/__snapshots__/backports.spec.ts.snap b/src/util/__snapshots__/backports.spec.ts.snap index 78e45b6d3d..77d85b21de 100644 --- a/src/util/__snapshots__/backports.spec.ts.snap +++ b/src/util/__snapshots__/backports.spec.ts.snap @@ -16,7 +16,7 @@ Object { } `; -exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should wran the user 1`] = ` +exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to false should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead. ", @@ -43,7 +43,7 @@ Object { } `; -exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should wran the user 1`] = ` +exports[`backportJestConfig with "globals.__TRANSFORM_HTML__" set to true should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.__TRANSFORM_HTML__\\" is deprecated, use \\"[jest-config].globals.ts-jest.stringifyContentPathRegex\\" instead. ", @@ -74,7 +74,7 @@ Object { } `; -exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should wran the user 1`] = ` +exports[`backportJestConfig with "globals.__TS_CONFIG__" set to { foo: 'bar' } should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.__TS_CONFIG__\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead. ", @@ -106,7 +106,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to '\\\\.spec\\\\.ts$' should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead. ", @@ -135,7 +135,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to false should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead. ", @@ -166,7 +166,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.enableTsDiagnostics" set to true should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.enableTsDiagnostics\\" is deprecated, use \\"[jest-config].globals.ts-jest.diagnostics\\" instead. ", @@ -195,7 +195,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to false should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead. ", @@ -222,7 +222,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.skipBabel" set to true should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.skipBabel\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead. ", @@ -251,7 +251,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.tsConfigFile" set to 'tsconfig.build.json' should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.tsConfigFile\\" is deprecated, use \\"[jest-config].globals.ts-jest.tsConfig\\" instead. ", @@ -280,7 +280,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to false should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to false should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.typeCheck\\" is deprecated, use \\"[jest-config].globals.ts-jest.isolatedModules\\" instead. ", @@ -309,7 +309,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to true should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.typeCheck" set to true should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.typeCheck\\" is deprecated, use \\"[jest-config].globals.ts-jest.isolatedModules\\" instead. ", @@ -338,7 +338,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to false should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead. ↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845 @@ -368,7 +368,7 @@ Object { } `; -exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to true should wran the user 1`] = ` +exports[`backportJestConfig with "globals.ts-jest.useBabelrc" set to true should warn the user 1`] = ` Array [ "[level:40] \\"[jest-config].globals.ts-jest.useBabelrc\\" is deprecated, use \\"[jest-config].globals.ts-jest.babelConfig\\" instead. ↳ See \`babel-jest\` related issue: https://github.com/facebook/jest/issues/3845 diff --git a/src/util/backports.spec.ts b/src/util/backports.spec.ts index 813b38d732..c54f175a4c 100644 --- a/src/util/backports.spec.ts +++ b/src/util/backports.spec.ts @@ -20,7 +20,7 @@ describe('backportJestConfig', () => { set(original, oldPath, val) }) describe(`with "${oldPath}" set to ${inspect(val)}`, () => { - it('should wran the user', () => { + it('should warn the user', () => { backportJestConfig(logger, original) expect(logTarget.lines.warn).toMatchSnapshot() }) // should warn the user diff --git a/src/util/version-checkers.spec.ts b/src/util/version-checkers.spec.ts index 4675455669..9087731604 100644 --- a/src/util/version-checkers.spec.ts +++ b/src/util/version-checkers.spec.ts @@ -42,16 +42,29 @@ function describeChecker( expect(warnings).toHaveLength(1) expect(warnings[0].message).toMatch(testVersion ? 'has not been tested with ts-jest' : 'is not installed') }) + it('should log only once with warn()', () => { checker.warn() checker.warn() expect(logTarget.messages.warn).toHaveLength(1) }) + it('should throw with raise()', () => { expect(checker.raise).toThrow() // adds another time as it should throw all the time even if already called expect(checker.raise).toThrow() }) + + it('should not log or throw when TS_JEST_DISABLE_VER_CHECKER is set in process.env', () => { + process.env.TS_JEST_DISABLE_VER_CHECKER = 'true' + + checker.warn() + + expect(logTarget.messages.warn).toHaveLength(0) + expect(checker.raise).not.toThrow() + + delete process.env.TS_JEST_DISABLE_VER_CHECKER + }) }) // describe unsupported version }) // unsupported versions loop @@ -60,10 +73,12 @@ function describeChecker( beforeEach(() => { pv.getPackageVersion.mockImplementation((name) => (name === moduleName ? testVersion : undefined)) }) + it('should not log with warn()', () => { checker.warn() expect(logTarget.messages.warn).toHaveLength(0) }) + it('should not throw with raise()', () => { expect(checker.raise).not.toThrow() }) diff --git a/src/util/version-checkers.ts b/src/util/version-checkers.ts index 9c84dce57b..a275746d39 100644 --- a/src/util/version-checkers.ts +++ b/src/util/version-checkers.ts @@ -47,31 +47,34 @@ function checkVersion( expectedRange: string, action: CheckVersionAction | undefined = 'warn', ): boolean | never { - const version = getPackageVersion(name) - const success = !!version && satisfies(version, expectedRange) + let success = true + if (!('TS_JEST_DISABLE_VER_CHECKER' in process.env)) { + const version = getPackageVersion(name) + success = !!version && satisfies(version, expectedRange) - logger.debug( - { - actualVersion: version, - expectedVersion: expectedRange, - }, - 'checking version of %s: %s', - name, - success ? 'OK' : 'NOT OK', - ) + logger.debug( + { + actualVersion: version, + expectedVersion: expectedRange, + }, + 'checking version of %s: %s', + name, + success ? 'OK' : 'NOT OK', + ) - if (!action || success) return success + if (!action || success) return success - const message = interpolate(version ? Errors.UntestedDependencyVersion : Errors.MissingDependency, { - module: name, - actualVersion: version || '??', - expectedVersion: rangeToHumanString(expectedRange), - }) - if (action === 'warn') { - logger.warn(message) - } else if (action === 'throw') { - logger.fatal(message) - throw new RangeError(message) + const message = interpolate(version ? Errors.UntestedDependencyVersion : Errors.MissingDependency, { + module: name, + actualVersion: version || '??', + expectedVersion: rangeToHumanString(expectedRange), + }) + if (action === 'warn') { + logger.warn(message) + } else if (action === 'throw') { + logger.fatal(message) + throw new RangeError(message) + } } return success