Comments (13)
I'm pretty sure this is what I'm running into as well, for a slightly less minimal example if it's useful then have a look at mscharley/dot#63
Seems to only be an issue with TS ~5.2, 5.1 is unaffected. It's similarly happening across Node 16, 18, and 20 for me as well.
I'd also be interested to know why it seems like ts-jest is messing with moduleResolution
and especially module
settings - it can trigger very different behaviour in projects if it gets toggled between different values. In my project I have both module
and moduleResolution
set explicitly to node16
but I managed to get the same error out of Jest:
● Test suite failed to run
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
from ts-jest.
Borrowing from #4198 I tried using a special tsconfig for just jest:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node10"
}
}
This is working for me, even for node imports which wouldn't normally work. This is very much a workaround though. This feels like a combination of two issues:
- Per the comments in #4198 ts-jest is modifying the
module
compilation option used to compile typescript with when it is in ESM mode but isn't modifyingmoduleResolution
as well which is now an error in TS >= 5.2 if you have both explicitly specified in your tsconfig. - This compilation error isn't being handled properly somewhere inside either Jest or ts-jest and the compilation error is leading to a hang condition (this issue).
from ts-jest.
Yep, I'm getting the hang with no changes other than the typescript
package version update, eg: https://github.com/mscharley/ioc-deco/actions/runs/5994385300/job/16255936781?pr=63
I got the error message by disabling NODE_OPTIONS=--experimental-vm-modules
, similar to how you did modifying some of Jest's options.
from ts-jest.
Error message says to ensure outDir
Which unfortunately makes no sense either. For esm javascript they suggest to use an empty transform but obviously I can't do that with Typescript.
from ts-jest.
But are you getting a hang as well? I would have been happy about an error message tbh. Hang is way harder to deal with, and has some dire implications in a CI environment eating away server time...
from ts-jest.
I don't have a minimal reproduction repo but I'm running into the same thing with typescript 5.2.2.
Jest hangs.
If I remove NODE_OPTIONS=--experimental-vm-modules
I get:
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
Even though I already have:
"module": "NodeNext",
"moduleResolution": "NodeNext"
in my tsconfig.json
.
Tried with Node 18 and 20.
from ts-jest.
@mscharley excellent workaround, thanks so much for sharing!
from ts-jest.
I dug around a bit in the repro provided in the link above.
In that case, we hit this throw
statement:
ts-jest/src/legacy/compiler/ts-compiler.ts
Line 213 in 9f1439a
Called from here:
ts-jest/src/legacy/ts-jest-transformer.ts
Line 197 in 9f1439a
The problem is that it throws within a new Promise
which is not properly propagated.
process.on('unhandledRejection', err => {
console.error('unhandled rejection', err);
})
new Promise(async (resolve, reject) => {
throw new Error('Go boom');
}).then(() => {
console.log('resolved');
}).catch(err => {
console.error('caught', err);
});
Running this will trigger the unhandled rejection instead of bubbling up the error.
Wrapping in try-catch
and calling reject
would fix it, but I don't understand why the new Promise
is there at all - removing that wrapper and just using await
within processAsync
normally would also resolve the issue.
This diff correctly bubbles up the error:
diff --git i/src/legacy/ts-jest-transformer.ts w/src/legacy/ts-jest-transformer.ts
index b4dbdc2a2..b0c3b5366 100644
--- i/src/legacy/ts-jest-transformer.ts
+++ w/src/legacy/ts-jest-transformer.ts
@@ -189,7 +189,6 @@ export class TsJestTransformer implements SyncTransformer {
): Promise<TransformedSource> {
this._logger.debug({ fileName: sourcePath, transformOptions }, 'processing', sourcePath)
- return new Promise(async (resolve, reject) => {
const configs = this._configsFor(transformOptions)
const shouldStringifyContent = configs.shouldStringifyContent(sourcePath)
const babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer
@@ -199,7 +198,7 @@ export class TsJestTransformer implements SyncTransformer {
code: processWithTsResult.code,
}
if (processWithTsResult.diagnostics?.length) {
- reject(configs.createTsError(processWithTsResult.diagnostics))
+ throw configs.createTsError(processWithTsResult.diagnostics)
}
if (babelJest) {
this._logger.debug({ fileName: sourcePath }, 'calling babel-jest processor')
@@ -212,8 +211,7 @@ export class TsJestTransformer implements SyncTransformer {
}
result = this.runTsJestHook(sourcePath, sourceText, transformOptions, result)
- resolve(result)
- })
+ return result
}
private processWithTs(
(Also, I don't know why async-await
is transpiled)
That said, Jest should have some sort of unhandled rejection
listener somewhere that picks this up - unsure why that doesn't happen...
from ts-jest.
Opened #4226 fixing the error being gobbled up (which is what this issue is about - not what error is being thrown)
from ts-jest.
@SimenB regarding the error itself did I misconfigure something or is it yet another issue?
from ts-jest.
Dunno, I haven't used those options. Error message says to ensure outDir
from ts-jest.
I managed to get it working: https://github.com/darkbasic/jest-esm-repro
Unfortunately we still have problems with errors not bubbling up, in fact the culprit was "noEmitOnError": true
. Removing it fixed jest but still no clue which errors are supposed to happen because yarn compile
works fine with the same tsconfig.
from ts-jest.
Even worse I've just noticed that snapshots don't work at all: jestjs/jest#14723
from ts-jest.
Related Issues (20)
- [Bug]: TS 5.2 error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16' HOT 10
- [Bug]: ES6 still doesn't work HOT 1
- [Feature]: Add ability to pass config to presets
- [Bug]: importing `got` v13 package causes Jest to fail to parse the file
- [Bug]: error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'. HOT 1
- [Bug]: TS 5.2 - TypeError: toSorted is not a function HOT 2
- [Bug]: Cannot find module bundle-text (Parcel) HOT 1
- [Bug]: Divergent branch coverage in JS and TS reports HOT 1
- [Bug]: Mixed ESM/CJS Typescript files in node_modules HOT 1
- [Bug]: Seems it disrepects "@ts-nocheck"
- [Bug]: Failed to collect coverage from. | 'rootDir' is expected to contain all source files. HOT 2
- [Bug]: Wrong line number at error stack of global setup when using typescript
- [Bug]: Inconsistent hybrid module resolution (index.d.mts vs. index.d.cts) between ts-jest and tsc HOT 3
- [Bug]: Top level await expressions are only .... HOT 6
- [Bug]: Cannot find module '@jest/globals' when cache: false HOT 1
- [Bug]: TS 5.2+ fails if using `nodenext` module resolution and `noEmitOnError` is `true` HOT 2
- [Bug]: TsJestTransformerOptions throw type error
- [Bug]: ESM syntax is not allowed in a CommonJS HOT 1
- [Bug]: Produced source maps seem to have mismatch with file paths
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-jest.