-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
vi.mock importOriginal is empty when vi.resetModules is used (since 4.1.0) #9888
Copy link
Copy link
Closed
Copy link
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Description
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.0Used Package Manager
npm
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)