You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
vitest fails to load typescript worker code in node (but does so correctly in browser mode) #5757
As already asked here, I'm looking for a way to execute tests in node while using node:worker_threads. Equivalent code works just fine in browser mode.
Reproduction
git clone --branch original https://github.com/andreashuber69/vitest-repro.git
cd vitest-repro
npm install
(EDIT: modified to check out original code, to check out the workaround discussed below please use git clone https://github.com/andreashuber69/vitest-repro.git)
The following commands work as expected:
npm run test-browser
npm run test-node-js
The following doesn't work:
npm run test-node-ts
Apparently under node, test code isn't compiled to js first (as in browser mode), which is why the above produces the following error:
Error: Cannot find module '.../src/nodeWorker.js'
Attempts at importing tsx (see commented out code in ./src/nodeWorker.spec.ts) have failed with the following error:
TypeError: Unknown file extension ".ts" for .../src/nodeWorker.ts
(presented as a solution in this issue but apparently doesn't work any longer)
How can I use vitest with typescript code that uses node:worker_threads?
Browser mode is quite different and it just happens that Vite's web worker support makes it work, but I don't think the same technique naturally transfers to node worker threads. It's unlikely to have some magical feature on Vitest out-of-the-box as noted in #3234 (comment)
Regarding tsx loader failing on Worker constructor, this seems like a well known issue/limitation of current Node loader:
Oh right, tsx probably spawns extra process in between, so Node's out-of-the-box worker communication doesn't work.
Just reading back the original Node issue nodejs/node#47747 (comment), it might be that the fix is near in Node 22, but I just double checked it still doesn't work (and it actually breaks more for some use cases).
Activity
hi-ogawa commentedon May 22, 2024
Browser mode is quite different and it just happens that Vite's web worker support makes it work, but I don't think the same technique naturally transfers to node worker threads. It's unlikely to have some magical feature on Vitest out-of-the-box as noted in #3234 (comment)
Regarding tsx loader failing on
Workerconstructor, this seems like a well known issue/limitation of current Node loader:--importAPI breaks worker threads privatenumber/tsx#354WorkeroptionexecArgvin v20 nodejs/node#47747The tsx issue suggests one workaround privatenumber/tsx#354 (comment) and that would probably work.
sheremet-va commentedon May 22, 2024
The equivalent cannot work just fine in the browser mode because the browser mode doesn't support
node:worker_threadsmodule.If you are testing web worker, the recommendation is to use @vitest/web-worker package.
andreashuber69 commentedon May 22, 2024
With equivalent code I meant code implemented in terms of web worker, see
test-browserscript.andreashuber69 commentedon May 22, 2024
For anyone landing here looking for a solution: I didn't get it to work. I tried with the following lines
Both
cli.mjsandnodeWorker.tsare found, but apparently the message posted on the main thread never makes it to the worker.hi-ogawa commentedon May 23, 2024
Oh right,
tsxprobably spawns extra process in between, so Node's out-of-the-box worker communication doesn't work.Just reading back the original Node issue nodejs/node#47747 (comment), it might be that the fix is near in Node 22, but I just double checked it still doesn't work (and it actually breaks more for some use cases).
For the workaround on Node 20, I think you can use
tsx/esm/apilike this to wrap your worker. I just tested this approach here https://github.com/hi-ogawa/reproductions/blob/main/vitest-5757-ts-worker/src/ts-worker/index.mjsandreashuber69 commentedon May 23, 2024
Indeed, that works. I've modified vitest-repro accordingly and adapted the instructions for the reproduction to check out the original code.
Thank you very much!
daniel-nagy commentedon Jun 3, 2024
If you don't want the stain of a non-typescript file on your filesystem (or just want to avoid the extra file)