Comments (6)
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).
For the workaround on Node 20, I think you can use tsx/esm/api
like 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.mjs
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);
from vitest.
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:
The tsx issue suggests one workaround privatenumber/tsx#354 (comment) and that would probably work.
from vitest.
The equivalent cannot work just fine in the browser mode because the browser mode doesn't support node:worker_threads
module.
If you are testing web worker, the recommendation is to use @vitest/web-worker package.
from vitest.
The equivalent cannot work just fine in the browser mode because the browser mode doesn't support node:worker_threads module.
With equivalent code I meant code implemented in terms of web worker, see test-browser
script.
from vitest.
For anyone landing here looking for a solution: I didn't get it to work. I tried with the following lines
const tsx = new URL("../node_modules/tsx/dist/cli.mjs", import.meta.url);
const worker = new Worker(tsx, { env: SHARE_ENV, argv: ["./src/nodeWorker.ts"] });
Both cli.mjs
and nodeWorker.ts
are found, but apparently the message posted on the main thread never makes it to the worker.
from vitest.
For the workaround on Node 20, I think you can use
tsx/esm/api
like this to wrap your worker.
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!
from vitest.
Related Issues (20)
- vitest config with react plugin typescript error: "No overload matches this call" HOT 1
- Ability to "unset" an environment variable with stubEnv (or a new function) HOT 3
- Range.compareBoundaryPoints should throw error HOT 4
- Browser mode: 'X-Frame-Options': 'DENY' breaks tests HOT 2
- Add init command for browser tests HOT 3
- The requested module 'vite' does not provide an export named 'parseAstAsync' HOT 2
- Re-implement parameterized test names (#2405)
- Support testing the unhandled promise rejection
- Coverage Testing | Min & Max Threads = Null | Range Error HOT 1
- Windows drive casing causes issues with mocking
- Support for the experimental syntax 'decorators' isn't currently enabled
- Failed to update coverage thresholds. Configuration file is too complex. HOT 3
- Expose CDP in the browser mode
- Vitest hangs infinitely importing file with exported class Timeline. HOT 3
- Vitest cannot transpile with TypeScript `moduleResolution` set to `node16` HOT 2
- Browser: refreshing the page brakes `vitest` BroadcastChannel
- Mocking cyclic dependencies with `importOriginal` doesn't work in the browser
- docs: oder instead of order
- Vitest does not work when running from any CLI (Cannot find module @rollup) HOT 2
- vitest runner defaultSuite always empty HOT 1
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 vitest.