[!warning] A Work on Module Systems
vi.mock
works only for modules that were imported with theimport
keyword. It doesn't work withrequire
.
Vitest provides vi.mock
, which allows you to mock any import that you provide a path for. It's go the following signature:
(path: string, factory?: () => unknown) => void
The factory
is a function that you provide as a substitute for whatever really resides at the file path. You'll that it's optional. Here is how it goes down:
- If you provided a
factory
function, it will use the return value of that function as the replacement for whatever module lives atpath
. - If you don't provide a factory, but you do have a
__mocks__
directory at the same location and an alternative file in that__mocks__
directory, then it will substiture that in. - Vitest will use it's automocking algorithm.
If you don't provide a factory, Vitest will employ its automocking algorithm:
- All arrays will be emptied.
- All primitives and collections will stay the same.
- All objects will be deeply cloned.
- All instances of classes and their prototypes will be deeply cloned.
[!todo] Explain the automocking algorithm You'll probably want to add some kind of visual as well as an example in code and some tests that prove your assumptions.
vi.doMock
is basically the same as vi.mock
except for the fact that it's not hoisted to the top, which means you have access to variables. The next import of that module will be mocked.
[!todo] Explain
vi.mock
hoisting Show with some concrete examples the difference betweenvi.mock
andvi.doMock
.