jest.mock does not mock an ES module without Babel
See original GitHub issueš Bug Report
In an ES module Node project, with no Babel, jest.mock
works when the mocked module is a node_modules
package that exports CommonJS, but it isnāt working for me mocking an ES module exported from a file in the same project.
(Itās possible that an NPM package that only exports ES modules has the same issue. I didnāt try that case.)
To Reproduce
Steps to reproduce the behavior:
Click Run in the repl, or hereās a simple example:
// main.js
import secondary from "./secondary.js";
export default function main() {
return secondary();
}
// secondary.js
export default function secondary() {
return true;
}
// test.js
import { jest } from "@jest/globals";
jest.mock("./secondary.js");
let main;
let secondary;
beforeAll(async () => {
({ default: main } = await import("./main.js"));
({ default: secondary } = await import("./secondary.js"));
});
test("works", () => {
secondary.mockReturnValueOnce(false); // TypeError: Cannot read property 'mockReturnValueOnce' of undefined
expect(main()).toBe(false);
});
Expected behavior
jest.mock(filename)
should mock the exports from filename
when the test file and the Node project are both ES modules (type: "module"
)
Link to repl or repo (highly encouraged)
https://repl.it/repls/VerifiableOfficialZettabyte
envinfo
System:
OS: macOS 10.15.4
CPU: (4) x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
Binaries:
Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.14.4 - ~/DevProjects/reaction/api-utils/node_modules/.bin/npm
npmPackages:
jest: ^26.0.1 => 26.0.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:142
- Comments:66 (8 by maintainers)
Top Results From Across the Web
node + ts-jest + ESM, jest.mock doing nothing - Stack Overflow
Basically, the issue is that Jest can't hoist the mock before all other imports as it does using babel (https://github.com/facebook/jest/issuesĀ ...
Read more >Mocking ES and CommonJS modules with jest.mock() - Medium
You can mock both externally required/imported code, or code passed in through dependency injection. Mocking code passed in through dependencyĀ ...
Read more >ECMAScript Modules - Jest
mock calls that happens in CJS won't work for ESM. To mock modules in ESM, you need to use require or dynamic import()...
Read more >solving the jest.mock()+esmodules hoisting problem
A proposal to use jest.mock() with esmodules without babel shenanigans. ... The value x is imported before the module is mocked, yet behaves...
Read more >How to mock a dependency in a Node.js, and why you should ...
So, I hope you know that unit tests must test their targets in isolation. Target must being isolated, as long they just should...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
I respectfully disagree with this being labeled a feature request. Itās entirely blocking of any effort to move to Jest native ES module support if any files have mocks in them, and there is no workaround that I know of (other than to continue using CommonJS through Babel, which means that ES module support is broken, hence bug).
As a status update, Iāve opened up a PR here: #10976