Skip to content

vi.mock importOriginal causes ERR_MODULE_NOT_FOUND with &v=<hash> suffix appended to path in 4.1.0 (regression from 4.0.x) #9887

@csvan

Description

@csvan

Bug Report

Vitest version: 4.1.0
Regression from: 4.0.18 (works correctly)
Environment: Node 22, happy-dom

Description

In Vitest 4.1.0, calling vi.mock with importOriginal (or vi.importActual) on a package listed in test.server.deps.inline causes ERR_MODULE_NOT_FOUND. The module evaluator appends &v=<hash> directly to the resolved file path, producing a path that does not exist on disk.

The same error occurs when the package is in test.deps.optimizer.client.include — the pre-bundled chunk path also gets the suffix appended.

This is a regression — identical configuration works correctly on 4.0.18.

Minimal reproduction

// vitest.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    environment: 'happy-dom',
    server: {
      deps: {
        inline: ['react-redux'],
      },
    },
  },
});
// some.test.ts
vi.mock('react-redux', async (importOriginal) => ({
  ...(await importOriginal()),
  useSelector: vi.fn(),
}));

test('example', () => {});

Error output

Error: [vitest] There was an error when mocking a module.
Caused by: Error: Cannot find module '/node_modules/react-redux/dist/react-redux.mjs&v=c1c7e391'
  { code: 'ERR_MODULE_NOT_FOUND' }

The &v=<hash> suffix is appended to the literal resolved file path by the module evaluator, making it unresolvable on disk.

When the package is instead added to test.deps.optimizer.client.include, the pre-bundled chunk gets the same treatment:

Error: Cannot find module '/node_modules/.vite/vitest/.../deps/react-redux.js&v=8f02ed9a'
  { code: 'ERR_MODULE_NOT_FOUND' }

Expected behavior

importOriginal / vi.importActual should resolve the module correctly regardless of whether it is listed in server.deps.inline or optimizer.client.include, as it did in 4.0.x.

Workaround

Pinning to ~4.0.0 via overrides in package.json resolves the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions