feature request: jest.mock - require factory function
See original GitHub issueI 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. calljest.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.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.