Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .cjs and .mjs files support to test runner #8768

Merged
merged 1 commit into from
Apr 4, 2020
Merged

Add .cjs and .mjs files support to test runner #8768

merged 1 commit into from
Apr 4, 2020

Conversation

ai
Copy link
Contributor

@ai ai commented Apr 2, 2020

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: ai/nanoid#205

The Way to Reproduce the Problem

  1. Create an empty CRA project
  2. npm i nanoid
  3. 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: … }

@ianschmitz
Copy link
Contributor

Great, thanks @ai!

@ai
Copy link
Contributor Author

ai commented Apr 8, 2020

@ianschmitz can I wait for sooner release in patch version?

@lock lock bot locked and limited conversation to collaborators Apr 15, 2020
@ianschmitz ianschmitz modified the milestones: 3.5, 4.0 May 3, 2020
@mrmckeb
Copy link
Contributor

mrmckeb commented Jul 28, 2020

Thanks for this @ai, I also hit this problem today and was about to solve it when I saw you had.

We're working hard on getting v4.0 out ASAP. We've been held up by some ESLint issues, but I think those are 99% solved now... so this will come very soon.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants