Coder Social home page Coder Social logo

Comments (13)

mscharley avatar mscharley commented on June 2, 2024 4

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.

mscharley avatar mscharley commented on June 2, 2024 4

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:

  1. 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 modifying moduleResolution as well which is now an error in TS >= 5.2 if you have both explicitly specified in your tsconfig.
  2. 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.

mscharley avatar mscharley commented on June 2, 2024 3

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.

darkbasic avatar darkbasic commented on June 2, 2024 1

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.

BenceSzalai avatar BenceSzalai commented on June 2, 2024

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.

ValentinGurkov avatar ValentinGurkov commented on June 2, 2024

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.

noahm avatar noahm commented on June 2, 2024

@mscharley excellent workaround, thanks so much for sharing!

from ts-jest.

SimenB avatar SimenB commented on June 2, 2024

I dug around a bit in the repro provided in the link above.

In that case, we hit this throw statement:

throw new Error(interpolate(Errors.CannotProcessFile, { file: fileName }))

Called from here:

const processWithTsResult = this.processWithTs(sourceText, sourcePath, transformOptions)

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(
image

(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.

SimenB avatar SimenB commented on June 2, 2024

Opened #4226 fixing the error being gobbled up (which is what this issue is about - not what error is being thrown)

from ts-jest.

darkbasic avatar darkbasic commented on June 2, 2024

@SimenB regarding the error itself did I misconfigure something or is it yet another issue?

from ts-jest.

SimenB avatar SimenB commented on June 2, 2024

Dunno, I haven't used those options. Error message says to ensure outDir

from ts-jest.

darkbasic avatar darkbasic commented on June 2, 2024

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.

darkbasic avatar darkbasic commented on June 2, 2024

Even worse I've just noticed that snapshots don't work at all: jestjs/jest#14723

from ts-jest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.