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.

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:open
  • Created 3 years ago
  • Reactions:142
  • Comments:66 (8 by maintainers)

github_iconTop GitHub Comments

93reactions
aldeedcommented, May 22, 2020

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).

28reactions
SimenBcommented, Dec 23, 2020

As a status update, Iā€™ve opened up a PR here: #10976

Read more comments on GitHub >

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

github_iconTop Related Medium Post

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