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

Skip to content

vi.mock importOriginal is empty when vi.resetModules is used (since 4.1.0) #9888

Description

@Nico142

Describe the bug

After updating Vitest from 4.0.18 to 4.1.0, importOriginal from vi.mock is empty when using resetModules and isolate: false.

I run my tests with maxWorkers: 1, isolate: false, and vi.resetModules(); in a beforeEach. When using importOriginal from vi.mock in setupTests.js, the first test suite runs successfully. However, the second test throws this error:

No "connect" export is defined on the "react-redux" mock. Did you forget to return it from "vi.mock"?
If you need to partially mock a module, you can use "importOriginal" helper inside:

When logging original, the first test suite logs all exports from react-redux, but the second suite logs this instead:

[Object: null prototype] [Module] {} 

Reproduction

// vitest.config.js

export default defineConfig({
  test: {
    setupFiles: ['./setupTests.js'],
    maxWorkers: 1,
    isolate: false,
  },
})
// setupTests.js

vi.mock('react-redux', async (importOriginal) => {
  const original = await importOriginal();

  return {
    ...original,
    useSelector: (callback) => callback(),
    useDispatch: () => (callback) => callback(),
  };
});

beforeEach(() => {
  vi.resetModules();
});

Here is a StackBlitz example, with Lodash instead of react-redux:
https://stackblitz.com/edit/vitest-dev-vitest-z5dhbqpm?file=package.json&view=editor

System Info

- Vite 7
- Vitest 4.1.0

Used Package Manager

npm

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