How to handle hoisted mocks
See original GitHub issueLet’s take the following code snippet:
import { mockDeep } from "jest-mock-extended";
// ... other imports
jest.mock("./someObject", () => ({
someObject: mockDeep<InstanceType<SomeClass>>(),
}));
This would fail because jest hoists jest.mock
above all the import
s and so mockDeep
would be undefined.
How do I solve this problem? And would it be worthwhile adding documentation on how to do this?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5
Top Results From Across the Web
solving the jest.mock()+esmodules hoisting problem
mock () call to above the import statements. So the module first gets mocked, then gets imported.
Read more >Manual Mocks - Jest
Manual mocks are used to stub out functionality with mock data. ... jest.mock calls cannot be hoisted to the top of the module...
Read more >How to hoist a jest dependency mock over actual dependency?
You'll need to move your mock outside of the test and make sure your factory function doesn't have any external dependencies. Something like ......
Read more >ts-jest does not hoist variables beginning with mock #1088
Issue : Jest docs describe that all variables that begin with mock are hoisted to the top of the file, above any jest.mock...
Read more >Mocking ES and CommonJS modules with jest.mock() - Medium
Since calls to jest. mock() are hoisted to the top of the file, it's not possible to first define a variable and then...
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
Probably worth adding to docs… but you can do this:
@toshi38 this is absolutely worth adding to the docs, this is a scenario that we would encounter quite frequently.
Closing this issue as resolved, but yes definitely worth adding to the docs!