Skip to content

Commit

Permalink
feat: allow opt-out version warning message by environment variable `…
Browse files Browse the repository at this point in the history
…TS_JEST_DISABLE_VER_CHECKER` (#1821)

Close #1774
  • Loading branch information
ahnpnl authored Jul 21, 2020
1 parent d8367db commit e6b42fc
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 45 deletions.
28 changes: 25 additions & 3 deletions docs/user/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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

Expand Down Expand Up @@ -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')} */
Expand Down Expand Up @@ -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.

Expand Down
20 changes: 19 additions & 1 deletion e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]===================================================================
Expand All @@ -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.
================================================================================
`;
26 changes: 21 additions & 5 deletions e2e/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
})
Expand Down
26 changes: 13 additions & 13 deletions src/util/__snapshots__/backports.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
",
Expand All @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand All @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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.
",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/util/backports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/util/version-checkers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
})
Expand Down
47 changes: 25 additions & 22 deletions src/util/version-checkers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e6b42fc

Please sign in to comment.