-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vitest fails to load typescript worker code in node (but does so correctly in browser mode) #5757
Comments
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
The tsx issue suggests one workaround privatenumber/tsx#354 (comment) and that would probably work. |
The equivalent cannot work just fine in the browser mode because the browser mode doesn't support If you are testing web worker, the recommendation is to use @vitest/web-worker package. |
With equivalent code I meant code implemented in terms of web worker, see |
For anyone landing here looking for a solution: I didn't get it to work. I tried with the following lines
Both |
Oh right, 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 import { Worker } from "node:worker_threads";
class TsWorker extends Worker {
constructor(filename, options = {}) {
options.workerData ??= {};
options.workerData.__ts_worker_filename = filename.toString();
super(new URL("./worker.mjs", import.meta.url), options);
}
}
const worker = new TsWorker(new URL("./your-actual-worker.ts", import.meta.url)); // worker.mjs
import { tsImport } from "tsx/esm/api";
import { workerData } from "node:worker_threads";
tsImport(workerData.__ts_worker_filename, import.meta.url); |
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! |
If you don't want the stain of a non-typescript file on your filesystem (or just want to avoid the extra file) import { type WorkerOptions, Worker } from "node:worker_threads";
const worker = /* JavaScript */ `
import { createRequire } from "node:module";
import { workerData } from "node:worker_threads";
const filename = "${import.meta.url}";
const require = createRequire(filename);
const { tsImport } = require("tsx/esm/api");
tsImport(workerData.__ts_worker_filename, filename);
`;
export class TsWorker extends Worker {
constructor(filename: string | URL, options: WorkerOptions = {}) {
options.workerData ??= {};
options.workerData.__ts_worker_filename = filename.toString();
super(new URL(`data:text/javascript,${worker}`), options);
}
} |
Describe the bug
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
(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
?System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: