From 3bffa47625a54916fbc543539bde3c7507142fe5 Mon Sep 17 00:00:00 2001 From: Ryan Ling Date: Wed, 20 Mar 2024 09:11:38 +1100 Subject: [PATCH] Jest.mergePreset: Fix compatibility with `NodeNext` (#1481) * Fix `isolatedModules` tsconfig override * Try something * Add a changeset * Placate snapshot * pkg: Exclude `jest/*.test.ts` files --- .changeset/hip-ears-punch.md | 20 ++++++++++++++++++++ .changeset/slow-moons-matter.md | 5 +++++ jest/transform.js | 33 ++++++++++++++++++++++++++++----- package.json | 2 +- 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 .changeset/hip-ears-punch.md create mode 100644 .changeset/slow-moons-matter.md diff --git a/.changeset/hip-ears-punch.md b/.changeset/hip-ears-punch.md new file mode 100644 index 000000000..bc4033379 --- /dev/null +++ b/.changeset/hip-ears-punch.md @@ -0,0 +1,20 @@ +--- +'skuba': patch +--- + +Jest.mergePreset: Fudge `Node16` and `NodeNext` module resolutions + +This works around a `ts-jest` issue where test cases fail to run if your `moduleResolution` is set to a modern mode: + +```json +{ + "compilerOptions": { + "moduleResolution": "Node16 | NodeNext" + } +} +``` + +```console +error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'. +error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'. +``` diff --git a/.changeset/slow-moons-matter.md b/.changeset/slow-moons-matter.md new file mode 100644 index 000000000..77d9b4027 --- /dev/null +++ b/.changeset/slow-moons-matter.md @@ -0,0 +1,5 @@ +--- +'skuba': patch +--- + +pkg: Exclude `jest/*.test.ts` files diff --git a/jest/transform.js b/jest/transform.js index e94ac5dfe..c3afe6858 100644 --- a/jest/transform.js +++ b/jest/transform.js @@ -1,4 +1,5 @@ const { defaults } = require('ts-jest/presets'); +const { ModuleResolutionKind } = require('typescript'); const { tryParseTsConfig } = require('./tsConfig'); @@ -14,6 +15,29 @@ const TS_JEST_PATH = require.resolve(TS_JEST_NAME); const maybeTsConfig = tryParseTsConfig(); +const isolatedModules = maybeTsConfig?.options.isolatedModules ?? true; + +const BROKEN_MODULE_RESOLUTIONS = new Set([ + ModuleResolutionKind.Node16, + ModuleResolutionKind.NodeNext, +]); + +/** + * Passing through `Node16` or `NodeNext` seems to break `ts-jest`. + * + * ``` + * error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'. + * error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'. + * ``` + * + * https://github.com/kulshekhar/ts-jest/issues/4198 + */ +const tsconfig = BROKEN_MODULE_RESOLUTIONS.has( + maybeTsConfig?.options.moduleResolution, +) + ? { tsconfig: { moduleResolution: 'Node' } } + : undefined; + // Rewrite `ts-jest` transformations using our resolved `TS_JEST_PATH`. module.exports.transform = Object.fromEntries( Object.entries(defaults.transform).map(([key, value]) => { @@ -24,7 +48,8 @@ module.exports.transform = Object.fromEntries( ? [ TS_JEST_PATH, { - isolatedModules: maybeTsConfig?.options.isolatedModules ?? true, + ...tsconfig, + isolatedModules, }, ] : value, @@ -38,10 +63,8 @@ module.exports.transform = Object.fromEntries( TS_JEST_PATH, { ...value[1], - isolatedModules: - value[1]?.isolatedModules ?? - maybeTsConfig?.isolatedModules ?? - true, + ...tsconfig, + isolatedModules, }, ] : value, diff --git a/package.json b/package.json index eec0b8bd6..920620551 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "files": [ "config/**/*", - "jest/**/*", + "jest/**/*.js", "lib*/**/*.d.ts", "lib*/**/*.js", "lib*/**/*.js.map",