jest.mock does not mock an ES module without Babel
<!-- Love Jest? Please consider supporting our collective: 👉 https://opencollective.com/jest/donate --> ## 🐛 Bug Report <!-- A clear and concise description of what the bug is. --> 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: ```js // 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 <!-- A clear and concise description of what you expected to happen. --> `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 (hig
Posted by aldeed on May 11, 2020, 10:16 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
jest.mock does not mock an ES module without Babel
<!-- Love Jest? Please consider supporting our collective: 👉 https://opencollective.com/jest/donate --> ## 🐛 Bug Report <!-- A clear and concise description of what the bug is. --> 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: ```js // 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 <!-- A clear and concise description of what you expected to happen. --> `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 (hig
Comments (0)
No comments yet. Start the discussion.