You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the release of vitest@0.31.1, there was a change of behavior with the combination of vi.mock and the restoreMocks configuration option that I do not think was intended. The following will (hopefully) help to explain what is shown in the reproduction (all of the snippets below are taken from the reproduction).
In my codebase, we have the restoreMocks option enabled and use a pattern like this (contrived) example:
import{describe,expect,it,vi}from"vitest";import{double}from"./double";// the module containing logic I want to testimport{multiply}from"./multiply";// a separate module that my module-under-test uses, and that i want to mock in this testvi.mock("./multiply");describe("double - mocked mock",()=>{it("should double 4",()=>{constmockMultiply=vi.fn();vi.mocked(multiply).mockImplementation(mockMultiply).mockReturnValue(12);expect(double(4)).toEqual(8);});});
With restoreMocks: true in the configuration, versions prior to 0.31.1 worked as expected, and this test would fail. After installing 0.31.1, the mockImplementation on multiply is seemingly no longer being used, and the test passes. If I change restoreMocks to false, the test starts failing with 0.31.1 as well.
The reason I think this is unintended behavior is because providing a factory to the vi.mock call results in (expected) test failures under both 0.31.1 and 0.31.0, with both restoreMocks: true and restoreMocks: false
Describe the bug
With the release of vitest@0.31.1, there was a change of behavior with the combination of
vi.mock
and therestoreMocks
configuration option that I do not think was intended. The following will (hopefully) help to explain what is shown in the reproduction (all of the snippets below are taken from the reproduction).In my codebase, we have the
restoreMocks
option enabled and use a pattern like this (contrived) example:With
restoreMocks: true
in the configuration, versions prior to 0.31.1 worked as expected, and this test would fail. After installing 0.31.1, themockImplementation
onmultiply
is seemingly no longer being used, and the test passes. If I changerestoreMocks
tofalse
, the test starts failing with 0.31.1 as well.The reason I think this is unintended behavior is because providing a factory to the
vi.mock
call results in (expected) test failures under both 0.31.1 and 0.31.0, with bothrestoreMocks: true
andrestoreMocks: false
Using my pattern with the directly imported module also fails tests as expected under both versions and with both settings of
restoreMocks
.My expectation is that my original pattern should continue to work in 0.31.1 as it did in prior versions with
restoreMocks: true
.Reproduction
https://stackblitz.com/edit/node-pvnkfw
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: