Mock modules
Jest supports different kind of module mocking:
- automocking config setting - if you have a file inside
__mocks__folder, it will automatically be mocked in tests (you can calljest.unmock('moduleName')to unmock it) - mocking with
jest.mock(path)syntax - it will try to find__mocks__folder and mock it, otherwise i'm not sure what it does, but it does something... - mocking with
jest.mock(path, factoryFn)- i personally dont recommend this approach, since it's behavior is unpredictable and I think we shouldn't implement it (for example, you cant reference other variables, except if they start withmock...). i prefer the 4th alternative - using
spyOnon imported object (import * as mod from 'mod') - doesnt work on esm modules in vite (does work in jest).
jest also has a function jest.requireActual(moduleName) to return actual module
I think implementing __mocks__/** is the easiest part - just see if there is a file inside, - mock it.
But 4th option is a little bit difficult. I suggest transforming all imports, how vite does to cjs modules:
// mod.ts
export const apples = 0
// -> transform to
export const __vitest__esm__ = {
apples
}
// importer.ts
import {apples} from 'mod.ts'
console.log(apples)
// -> transform to
import {__vitest__esm__} from 'mod.ts'
console.log(__vitest__esm__.apples__)
// test.ts
import * as mod from 'mod.ts'
spyOn(mod, 'apples', 'get')
// -> transform to (or smth like that)
import { __vitest__esm__ } from 'mod.ts'
spyOn(__vitest__esm__, 'apples', 'get')
That way we are stubing an object inside a module. One problem is that variables inside the modules itself wont be mocked, but this behaviour is aligned with jest
docs:
- https://jestjs.io/docs/manual-mocks
- https://jestjs.io/docs/jest-object#jestmockmodulename-factory-options
And we can use spyOn from tinyspy to do just that:)
I'll try to make at least the first two options to work.
Done 🎆
Jest supports different kind of module mocking:
- automocking config setting - if you have a file inside
__mocks__folder, it will automatically be mocked in tests (you can calljest.unmock('moduleName')to unmock it)- mocking with
jest.mock(path)syntax - it will try to find__mocks__folder and mock it, otherwise i'm not sure what it does, but it does something...- mocking with
jest.mock(path, factoryFn)- i personally dont recommend this approach, since it's behavior is unpredictable and I think we shouldn't implement it (for example, you cant reference other variables, except if they start withmock...). i prefer the 4th alternative- using
spyOnon imported object (import * as mod from 'mod') - doesnt work on esm modules in vite (does work in jest).jest also has a function
jest.requireActual(moduleName)to return actual moduleI think implementing
__mocks__/**is the easiest part - just see if there is a file inside, - mock it.But 4th option is a little bit difficult. I suggest transforming all imports, how vite does to cjs modules:
// mod.ts export const apples = 0 // -> transform to export const __vitest__esm__ = { apples } // importer.ts import {apples} from 'mod.ts' console.log(apples) // -> transform to import {__vitest__esm__} from 'mod.ts' console.log(__vitest__esm__.apples__) // test.ts import * as mod from 'mod.ts' spyOn(mod, 'apples', 'get') // -> transform to (or smth like that) import { __vitest__esm__ } from 'mod.ts' spyOn(__vitest__esm__, 'apples', 'get')That way we are stubing an object inside a module. One problem is that variables inside the modules itself wont be mocked, but this behaviour is aligned with jest
docs:
- https://jestjs.io/docs/manual-mocks
- https://jestjs.io/docs/jest-object#jestmockmodulename-factory-options
So to be clear, for the mocks to work, you just put the file in there and presto it should work?
Doesn't work for me and I upgraded to the latest version (25).
Am I missing a step?
On the latest version, this seems not to work for me either.
It does work, however, if I mock it via a factory.
vi.mock('zustand', async () => vi.importActual('/__mocks__/zustand'));
But I have a feeling that it shouldn't really be used like this.
On the latest version, this seems not to work for me either.
Please, create an issue with minimal reproduction. This issue is resolved.