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.

[Bug]: Mock ESM module not working

See original GitHub issue

Version

27.3.1

Steps to reproduce

In my angular project I am using the library swiper. Swiper migrated to esm with version 7. My Jest is actually configured to use CommonJS and not esm, because the esm support seems not really stabel and its not recommended to use experimental features in production mode like node vm-module.

In my angular component I am importing the swiper module like:

import { Swiper } from ‘swiper’;

If I am mocking the swiper module directly within the spec file like

jest.mock(‘swiper’, () => ({ __esModule: true, Swiper: jest.fn().mockImplementation(() => new MockSwiper()), Navigation: jest.fn(), Mousewheel: jest.fn(), Keyboard: jest.fn(), Autoplay: jest.fn(), Pagination: jest.fn(), default: jest.fn().mockImplementation(() => new MockSwiper()) }));

all is working fine. But I would like to mock the swiper module more global at the mocks folder. I am doing this like

const Swiper = jest.fn().mockImplementation(() => new MockSwiper()); export default Swiper; export { Swiper };

with the preset option:

moduleNameMapper: { swiper: ‘<rootDir>/…/…/mocks/swiper.js’, },

This generate the error:

Cannot read property ‘ɵcmp’ of undefined TypeError: Cannot read property ‘ɵcmp’ of undefined

I followed the Jest documentation. I think this could be a bug or I am doing somthing wrong?

Expected behavior

Esm modules could be mocked.

Actual behavior

Esm modules could be mocked.

Additional context

The following error appears:

Cannot read property ‘ɵcmp’ of undefined TypeError: Cannot read property ‘ɵcmp’ of undefined

Environment

Node: 14.17.0
Angular: 12.2.12
NX: 13.1.3

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anschmcommented, Nov 6, 2021

Yes, this solves the problem. Thanks.

0reactions
savifoncommented, Sep 20, 2022

It solved it, but it doesn’t render Swiper in the test, so you can’t do the internal tests.

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 >
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 >
supporting ES modules in the Testdouble.js mocking library
Having ES modules (ESM) in Node.js means that you can now write: import fs from 'fs' ... _load won't work because it just...
Read more >
Migrating an NPM package to use ES Modules - Medium
But the JS working group — TC39 — had been busy defining an official ... Currently Jest does not have a way to...
Read more >
jest mock cannot find module - You.com | The AI Search ...
Module mocking in ESM . Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens...
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