-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Describe the bug
In a test file, I mock a virtual module, e.g. using vi.mock() and try to import the original module using either vi.importActual() or the importOriginal() helper.
vi.mock("virtual:test", async (importOriginal) => {
const mod = await vi.importActual<"virtual:test">("virtual:test");
console.log("🚨 mod with vi.importActual:", mod);
const actual = await importOriginal();
console.log("🚨 actual with importOriginal:", actual);
// Not doing anything here for the sake of the repro, just returning the original module.
return mod;
});The behavior seems to be different between Vitest 3 and Vitest 4:
Vitest 3: everything seems to work as expected, I can import the original module and get the expected value.
Vitest 4: both vi.importActual() and the importOriginal() helper return what seems to be an empty module (e.g. when logging the result, I get [Object: null prototype] [Module] {})
Reproduction
I've setup a minimal repro available here: https://github.com/HiDeoo/vitest-4-mock-virtual-module-importactual-repro
- Clone the repository
- Run pnpm install
- Run pnpm test
Some tests that are checking the result of calling a function using the virtual module will fail.
A few extra notes:
- Running
pnpm add vitest@3andpnpm testwill make the test pass - I've carefully read the Vitest 4 migration guide, and specifically the section about "Changes to Mocking", but unless I'm missing something, I don't think any of the listed changes explain this behavior.
System Info
System:
OS: macOS 26.3
CPU: (8) arm64 Apple M1
Memory: 96.19 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.14.0 - /Users/hideoo/.local/state/fnm_multishells/42034_1772463011409/bin/node
npm: 10.9.2 - /Users/hideoo/.local/state/fnm_multishells/42034_1772463011409/bin/npm
pnpm: 9.15.9 - /Users/hideoo/.local/state/fnm_multishells/42034_1772463011409/bin/pnpm
bun: 1.3.10 - /opt/homebrew/bin/bun
Browsers:
Chrome: 145.0.7632.117
Firefox: 148.0
Safari: 26.3
npmPackages:
vitest: 4.1.0-beta.5 => 4.1.0-beta.5Used Package Manager
pnpm
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.