Skip to content

SpyOn a module function imported by a web worker stops working in 2.1.0-beta.7/2.1.0/2.1.1 #6566

@xuhdev

Description

@xuhdev

Describe the bug

When a web worker imports a function from another module, in 2.0.5, spyOn can mock the function used by the web worker. This stops working starting from 2.1.0-beta.7.

The following mocking used to be working, given that util.js is imported by the worker script:

Reproduction

Full source code at https://github.com/xuhdev/vitest-web-worker-mock-bug-demo

With vitest from 2.0.5 to 2.1.0-beta.6, running test prints "I'm in test". With vitest from 2.1.0-beta.7 to 2.1.1, running test prints "I'm the production worker.".

index.test.ts:

import { vi } from "vitest";
import * as util from "./util.js";

vi.mock("./util.js", async (importOriginal) => {
  const mod = await importOriginal();
  return {
    ...mod,
  };
});

test("main", async () => {
  vi.spyOn(util, "getWorkerMessage").mockReturnValue("I'm in test");
  const worker = new Worker(new URL("./worker.js", import.meta.url));
  const promise = new Promise((resolve) => {
    worker.onmessage = (e) => {
      // Prints "I'm the production worker." in 2.1.0-beta.7 and beyond, "I'm in test" in 2.0.5 and 2.1.0-beta.6.
      console.error(e.data);
      resolve();
    };
  });
  worker.postMessage(["get message"]);

  await promise;
});

util.js:

export function getWorkerMessage() {
  return "I'm the production worker.";
}

worker.js:

import { getWorkerMessage } from "./util.js";

self.onmessage = () => {
  postMessage(getWorkerMessage());
};

System Info

System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (12) x64 Intel(R) Xeon(R) E-2136 CPU @ 3.30GHz
    Memory: 54.13 GB / 62.51 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
    npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
    pnpm: 9.5.0 - ~/.nvm/versions/node/v22.9.0/bin/pnpm
  Browsers:
    Chromium: 129.0.6668.58
  npmPackages:
    @vitest/web-worker: 2.1.1 => 2.1.1 
    vitest: 2.1.1 => 2.1.1


### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions