vitest-dev / vitest-dev/vitest
`vi.mock` doesn't seem to work inside a sandbox directory
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 94
Description
Describe the bug
StrykerJS creates a sandbox directory for your project by default. Inside that sandbox directory, it copies all project files while mutating your production code. However when running vi.mock it doesn't seem to work because of this sandboxing mechanism. I've reproduced it using just vitest (without StrykerJS).
Note: I'm sure the "sandboxing" is the issue here. StrykerJS also supports --inPlace mode, that disables sandboxing and I can confirm that Stryker works as expected in that case.
Reproduction
- Unzip the issue.zip inside a directory
- cd into the root of the directory
- run
npm i - run
npx vitest. See that it works. - run
cd .stryker-tmp/sandbox-aGW0K0(this is a copy of the project, but instrumented with mutations) - again, use
npx vitest. You can now see the error:TypeError: [Function Discount] is not a spy or a call to a spy!
The console outputs includes:
Error: [MSW] Failed to register a Service Worker for scope ('http://localhost:63315/') with script ('http://localhost:63315/mockServiceWorker.js'): Service Worker script does not exist at the given path.
Did you forget to run "npx msw init <PUBLIC_DIR>"?
Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/init
System Info
System:
OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
Memory: 5.79 GB / 15.49 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
npm: 11.0.0 - ~/.nvm/versions/node/v22.9.0/bin/npm
pnpm: 9.15.1 - ~/.nvm/versions/node/v22.9.0/bin/pnpm
Browsers:
Chrome: 131.0.6778.139
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the attached issue.zip reproduction: install dependencies, run npx vitest from the project root, then repeat from the .stryker-tmp/sandbox-aGW0K0 copy. Compare the two runs and investigate why vi.mock fails in the sandbox; done means the copied project runs without the spy error and the reported MSW failure is addressed or understood.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript, vite
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100