You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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*asutilfrom"./util.js";vi.mock("./util.js",async(importOriginal)=>{constmod=awaitimportOriginal();return{
...mod,};});test("main",async()=>{vi.spyOn(util,"getWorkerMessage").mockReturnValue("I'm in test");constworker=newWorker(newURL("./worker.js",import.meta.url));constpromise=newPromise((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"]);awaitpromise;});
util.js:
exportfunctiongetWorkerMessage(){return"I'm the production worker.";}
### 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.
The text was updated successfully, but these errors were encountered:
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 from2.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:
util.js:
worker.js:
System Info
The text was updated successfully, but these errors were encountered: