vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Mock modules

Open antfu opened this issue 4 years ago • 7 comments

antfu avatar Dec 13 '21 07:12 antfu

Jest supports different kind of module mocking:

  1. automocking config setting - if you have a file inside __mocks__ folder, it will automatically be mocked in tests (you can call jest.unmock('moduleName') to unmock it)
  2. 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...
  3. 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 with mock...). i prefer the 4th alternative
  4. using spyOn on 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

sheremet-va avatar Dec 13 '21 08:12 sheremet-va

And we can use spyOn from tinyspy to do just that:)

sheremet-va avatar Dec 13 '21 08:12 sheremet-va

I'll try to make at least the first two options to work.

sheremet-va avatar Dec 13 '21 13:12 sheremet-va

Done 🎆

sheremet-va avatar Dec 29 '21 11:12 sheremet-va

Jest supports different kind of module mocking:

  1. automocking config setting - if you have a file inside __mocks__ folder, it will automatically be mocked in tests (you can call jest.unmock('moduleName') to unmock it)
  2. 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...
  3. 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 with mock...). i prefer the 4th alternative
  4. using spyOn on 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

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?

shawn-m-tech avatar Nov 07 '22 18:11 shawn-m-tech

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.

agarleanu avatar Jan 04 '23 10:01 agarleanu

On the latest version, this seems not to work for me either.

Please, create an issue with minimal reproduction. This issue is resolved.

sheremet-va avatar Jan 04 '23 11:01 sheremet-va