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]: CJS support for Jest

See original GitHub issue

Would you like to work on a fix?

  • Check this if you would like to implement a PR, we are more than happy to help you go through the process.

Current and expected behavior

Jest tests that render a tree containing a chart appear to fail after v5.x.x.

Cannot find module 'react-chartjs-2' from 'src/components/LineChart/index.tsx'

I tested this using Create React App with the default Jest configuration. I think this is caused by the removal of the CJS build. I don’t think Jest supports ESM-only modules out of the box. From what I can see, I don’t think people who use CRA can use the new version of this library without ejecting and configuring Jest themselves.

Please can you let me know if this is an expected consequence or if I’m missing something?

Thanks 🙂

Reproduction

https://github.com/louis-young/chartjs-cjs-jest-issue

chart.js version

v5.0.1

react-chartjs-2 version

v.4.0.0

Possible solution

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9

github_iconTop GitHub Comments

3reactions
leonardobritocommented, Dec 9, 2022

My workaround was create a datum file on mocks folder and do a mapper for this file on jest config:

moduleNameMapper: {
  'react-chartjs-2': '<rootDir>/src/tests/mocks/datum/react-chartjs-2.js',
},

At the file (<rootDir>/src/tests/mocks/datum/react-chartjs-2.js) we need to export the used components when needed:

function Doughnut({ children, ...rest }) {
  return <div {...rest}>{children}</div>
}

function Line({ children, ...rest }) {
  return <div {...rest}>{children}</div>
}

export {
  Doughnut,
  Line,
}
2reactions
dangreencommented, Nov 22, 2022

@LouisYoungGMSL

not build for CJS too

Chart.js is ESM only. We can’t import ESM in CommonJS module. So react-chartjs-2 can’t have CommonJS bundle.

I don’t think people who use CRA can use the new version of this library without ejecting and configuring Jest themselves.

Yes. CRA at all is poorly supported and has rare updates.

If you will eject react-scripts you should follow this doc to enable ESM support in jest. Or, I can recommend you migrate to vitest. Vitest has ESM support out of the box and has almost the same API as jest. My example of migration: https://github.com/reactchartjs/react-chartjs-2/commit/7f3ec96101d21e43cae8cbfe5e09a46a17cff1ef

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug#1019355: jest: please provide real (not virtual) package node ...
Package: jest. Version: 28.1.3~ds+~cs70.48.29-1. Severity: important nodejs also depends on cjs-modu...@1.2.2. Both packages could benefit from a separate ...
Read more >
ECMAScript Modules - Jest
Jest ships with experimental support for ECMAScript Modules (ESM). The implementation may have bugs and lack features. For the latest status ...
Read more >
jest fails with error: Cannot find module 'jose-node-cjs-runtime ...
I am unable to figure out why this error is caused. Please help me in finding the solution. I was using Node.js version...
Read more >
babel-jest - Awesome JS
[jest-runtime] Support WebAssembly (Wasm) imports in ESM modules (#13505) ... [jest-mock] Treat cjs modules as objects so they can be mocked (#13513) ...
Read more >
jest | Yarn - Package Manager
Jest · ‍ Developer Ready: Complete and ready to set-up JavaScript testing solution. Works out of the box for any React...
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