Created by: ai
Node.js added new .cjs and .mjs file extensions for dual ESM/CJS packages.
This PR adds .cjs and .mjs files from node_modules support to test runner.
Context
I have popular dual CJS/ESM package.
"type": "module",
"main": "index.cjs",
"module": "index.js",
"browser": {
"./index.js": "./index.browser.js"
},
"exports": {
".": {
"require": "./index.cjs",
"import": "./index.js",
"browser": "./index.browser.js"
},
Everythings works in Create React App npm start:
import { nanoid } from 'nanoid'
But Jest can’t load my library during npm test:
TypeError: (0 , _nanoid.nanoid) is not a function
Here is issue in my repo: https://github.com/ai/nanoid/issues/205
The Way to Reproduce the Problem
- Create an empty CRA project
npm i nanoid- Create test:
import * as nanoid from 'nanoid' console.log(nanoid)
Right now output will be:
{ default: 'index.cjs' }
With this pull request the result will be correct:
{ nanoid: [Function nanoid], urlAlphabet: …, customAlphabet: …, customRandom: … }