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

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

Open
6 tasks done
nicojs opened this issue Jan 4, 2025 · 2 comments
Open
6 tasks done

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

nicojs opened this issue Jan 4, 2025 · 2 comments

Comments

@nicojs
Copy link
Contributor

nicojs commented Jan 4, 2025

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

@AriPerkkio
Copy link
Member

Could you convert the .zip file into a Github repository or https://vitest.new project?

@nicojs
Copy link
Contributor Author

nicojs commented Jan 5, 2025

Sure thing, see https://github.com/nicojs/vitest-issue-7172

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants