cloudflare / cloudflare/workers-sdk
`vi.mock()` not working with `cloudflare:test` setup file imports
- Dominant language
- TypeScript
- Stars
- 4.5k
- Forks
- 1.5k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 183
Description
### What versions & operating system are you using?
System:
OS: macOS 15.5
CPU: (8) arm64 Apple M2
Memory: 178.98 MB / 24.00 GB
Shell: 4.0.2 - /opt/homebrew/bin/fish
Binaries:
Node: 22.17.1 - ~/.local/state/fnm_multishells/13811_1754254652214/bin/node
npm: 10.9.2 - ~/.local/state/fnm_multishells/13811_1754254652214/bin/npm
bun: 1.2.19 - ~/.bun/bin/bun
npmPackages:
@cloudflare/vitest-pool-workers: ^0.8.59 => 0.8.59
### Please provide a link to a minimal reproduction
https://github.com/jimmycallin/cloudflare-workers-d1-vitest-mock-issue
### Describe the Bug
When using Vitest with Cloudflare Workers and importing modules from cloudflare:test in a setup file, `vi.mock()`calls are not being applied.
I'm not entirely sure if this is a vitest or a wrangler issue, but I spent quite a few hours debugging this issue, so I thought I could at least post it here for sake of posterity.
Reproduction:
- Set up a vitest config with a setup file that imports from cloudflare:test (e.g., applyD1Migrations)
- Mock a module using `vi.mock()` in a test file
- Run tests - the mock won't be applied
Expected: Mocks should work regardless of setup file imports.
Actual: Mocks are ignored when setup file contains cloudflare:test imports.
Workaround: Call `vi.resetModules()` before setting up the mock.
### Please provide any relevant error logs
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.