[Bug]: CJS support for Jest
See original GitHub issueWould 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:
- Created 10 months ago
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
My workaround was create a datum
file
on mocks folder and do a mapper for thisfile
on jest config:At the file (
<rootDir>/src/tests/mocks/datum/react-chartjs-2.js
) we need to export the used components when needed:@LouisYoungGMSL
Chart.js is ESM only. We can’t import ESM in CommonJS module. So react-chartjs-2 can’t have CommonJS bundle.
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