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

Incompatible with Jest using ESM (Jest encountered an unexpected token) #160

Closed
FrozenKiwi opened this issue Jul 21, 2022 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@FrozenKiwi
Copy link

FrozenKiwi commented Jul 21, 2022

Versions

  • @prismicio/react: latest

Reproduction

When imported into Jest with Native Modules, it fails to compile because the package exports points to an ESM file, but there is no matching type: "modules" in package.json

https://github.com/prismicio/prismic-react/blob/master/package.json

I think webpack might figure this one out automatically, but jest is bit of a stickler for the rules.

jestjs/jest#11809 (comment)

Steps to reproduce

Too many to list. Getting jest going with ESM is a PITA

What is expected?

import succeeds

What is actually happening?

Jest encountered an unexpected token

WORKAROUND:

I'm using a custom resolver with a packageFilter similar to jestjs/jest#2702 (comment) that monkey-patches the package on-the-fly.

function mapModuleFieldToExports (pkg, pkgDir) {
  // manually force prismic/react to be a module
  if (pkgDir.includes(`@prismicio${path.sep}react`))
    pkg.type = "module";
@FrozenKiwi FrozenKiwi added the bug Something isn't working label Jul 21, 2022
@angeloashmore
Copy link
Member

Hi @FrozenKiwi, thanks for the report, and sorry for the delay in response.

When we don't get a reproduction project or steps to reproduce the issue, it takes longer for us to diagnose the issue since we need to first recreate the issue. A repro would be greatly appreciated next time. 🙂

Here's a minimal reproduction for this issue: https://github.com/angeloashmore/prismic-react-issue-160-repro


We initially had "type": "module" in this package, along with other Prismic packages, but had to roll back native ESM support due to the ecosystem not being ready. Jest was notably one of the tools that was not ESM-ready at the time we rolled back support.

You can find more details about removing "type": "module" and .mjs in this PR: #122

Due to Create React App 4's large user-base and ESM still being experimental in many tools (including Jest), I don't think we are ready to commit to full native ESM support.

Until the ecosystem catches up, I think your workaround is the best solution.

I'm going to rename the title of this issue so it is easier to find and we can reference it in the future. I am going to close the issue, however, as it is not something we will be changing soon.

We can definitely continue the conversation here as things change or if you have any suggestions on how we could support it. Thank you! 🙂

@angeloashmore angeloashmore closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2022
@angeloashmore angeloashmore changed the title package.json missing types: "module" Incompatible with Jest using ESM (Jest encountered an unexpected token) Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants