Skip to content
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

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

Closed
xuhdev opened this issue Sep 25, 2024 · 0 comments · Fixed by #6623
Closed
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@xuhdev
Copy link
Contributor

xuhdev commented Sep 25, 2024

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.
@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Oct 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants