Skip to content

Commit

Permalink
Jest.mergePreset: Fix compatibility with NodeNext (#1481)
Browse files Browse the repository at this point in the history
* Fix `isolatedModules` tsconfig override

* Try something

* Add a changeset

* Placate snapshot

* pkg: Exclude `jest/*.test.ts` files
  • Loading branch information
72636c authored Mar 19, 2024
1 parent 15842f1 commit 3bffa47
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .changeset/hip-ears-punch.md
Original file line number Diff line number Diff line change
@@ -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'.
```
5 changes: 5 additions & 0 deletions .changeset/slow-moons-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

pkg: Exclude `jest/*.test.ts` files
33 changes: 28 additions & 5 deletions jest/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { defaults } = require('ts-jest/presets');
const { ModuleResolutionKind } = require('typescript');

const { tryParseTsConfig } = require('./tsConfig');

Expand All @@ -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]) => {
Expand All @@ -24,7 +48,8 @@ module.exports.transform = Object.fromEntries(
? [
TS_JEST_PATH,
{
isolatedModules: maybeTsConfig?.options.isolatedModules ?? true,
...tsconfig,
isolatedModules,
},
]
: value,
Expand All @@ -38,10 +63,8 @@ module.exports.transform = Object.fromEntries(
TS_JEST_PATH,
{
...value[1],
isolatedModules:
value[1]?.isolatedModules ??
maybeTsConfig?.isolatedModules ??
true,
...tsconfig,
isolatedModules,
},
]
: value,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"files": [
"config/**/*",
"jest/**/*",
"jest/**/*.js",
"lib*/**/*.d.ts",
"lib*/**/*.js",
"lib*/**/*.js.map",
Expand Down

0 comments on commit 3bffa47

Please sign in to comment.