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.

How to handle hoisted mocks

See original GitHub issue

Let’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 imports 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
toshi38commented, Aug 3, 2022

Probably worth adding to docs… but you can do this:

import { mockDeep } from "jest-mock-extended";
// ... other imports

jest.mock("./someObject", () => {
    const nonHoistedMockDeep: typeof mockDeep = jest.requireActual("jest-mock-extended").mockDeep;
    return {
      someObject: nonHoistedMockDeep<SomeClass>(),
    };
});
0reactions
moltenicecommented, Nov 11, 2022

@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!

Read more comments on GitHub >

github_iconTop 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 >

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