Thanks to visit codestin.com
Credit goes to github.com

Skip to content

vi.importActual() issue with virtual modules in Vitest 4 #9771

Description

@HiDeoo

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

  1. Clone the repository
  2. Run pnpm install
  3. 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@3 and pnpm test will 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.5

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

Labels

p3-minor-bugAn edge case that only affects very specific usage (priority)

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions