Giter VIP home page Giter VIP logo

Comments (6)

hi-ogawa avatar hi-ogawa commented on July 17, 2024 2

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.

hi-ogawa avatar hi-ogawa commented on July 17, 2024 1

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.

sheremet-va avatar sheremet-va commented on July 17, 2024

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.

andreashuber69 avatar andreashuber69 commented on July 17, 2024

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.

andreashuber69 avatar andreashuber69 commented on July 17, 2024

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.

andreashuber69 avatar andreashuber69 commented on July 17, 2024

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)

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.