question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request: jest.mock - require factory function

See original GitHub issue

I loved the auto-mocking feature of jest.mock when learning Jest, but now with an enterprise-sized project, it can cause unexpected problems when modules have side effects on import.

Example where jest.mock automocking can cause problems:

// ./get-config.ts - file to be mocked
import mem from 'mem';

// mem is being executed on import
export const foo = mem(() => ({});
// ./server.test.ts - test file

jest.mock('mem');
jest.mock('./get-config'); // ERROR HERE because the mock mem is not returning a function.

Rule fail

jest.mock('mem'); // no factory function
jest.mock('./get-config'); // no factory function

Rule pass

jest.mock('mem', () => jest.fn());
jest.mock('mem', () => (fn: Function) => fn);
jest.mock('mem', () => jest.fn((fn: Function) => fn));
jest.mock('./get-config', () => ({ foo: jest.fn() }));
jest.mock('./get-config', () => jest.fn());

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Oct 18, 2021

could you provide more detail on what problem this prevents?

Since I just noticed this never got an answer: jest.mock without a factory will load the module provided in order to inspect its exports and create a stub of it (e.g. functions, numbers etc., i.e. call jest.createMockFromModule under the hood). So if the file you mock (or anything it depends on) have side effects during loading, they will execute. Also, it’s slower (and gets slower the bigger the dependency graph is) than just providing a function for Jest to call.

0reactions
G-Rathcommented, Oct 16, 2021

I think let’s go with recommending no-restricted-syntax for this for now, given the lack of activity and I’ve not seen any "+1"ings on this issue or in others/comments.

I’m happy to add a new rule if a few more people request this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest.mock factory doesn't work inside a test #2582 - GitHub
So jest.mock is being hoisted to the function scope, that's why it won't work with require s (and definitely not import s ...
Read more >
How to mock a module that exports a factory function with Jest
You need to mock your module so that the constructor function should return the object you would like to mock, wrapped around a...
Read more >
The Jest Object
The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by...
Read more >
Mock only one function from module but leave rest with ...
The highlight of this answer is jest.requireActual(), this is a very useful utility that says to jest that "Hey keep every original ...
Read more >
Mocking a JavaScript Class with Jest, two ways to make it easier
A guide on how to mock a JavaScript class using Jest, comparing dependency ... A module factory is a function that returns the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found