vitest-dev / vitest-dev/vitest

`vi.mock` doesn't seem to work inside a sandbox directory

Open
#7,172 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
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

issue.zip

  1. Unzip the issue.zip inside a directory
  2. cd into the root of the directory
  3. run npm i
  4. run npx vitest. See that it works.
  5. run cd .stryker-tmp/sandbox-aGW0K0 (this is a copy of the project, but instrumented with mutations)
  6. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.