[CRA v5] Jest + React-markdown = Must use import to load ES Module
See original GitHub issueDescribe the bug
Running a CRA test rendering react-markdown
leads to an error message “Must use import to load ES Module:”
A (somewhat) related issue in https://github.com/remarkjs/react-markdown/issues/635 seems to redirect to https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c, which in turn states “Create React App doesn’t yet fully support ESM. I would recommend opening an issue on their repo with the problem you have encountered. One known issue is #10933.”
/cc @ChristianMurphy
Did you try recovering your dependencies?
Yes
Which terms did you search for in User Guide?
esm
Environment
Windows
Steps to reproduce
Full repro case is available at
- Clone https://github.com/nulltoken/repro-react-markdown-cra-esm
- run
yarn test
Output of yarn test
can be reviewed at https://github.com/nulltoken/repro-react-markdown-cra-esm/runs/4841320780?check_suite_focus=true
Expected behavior
I’d very much like the test not to fail because of an import issue 😕
Actual behavior
Reproducible demo
(Paste the link to an example project and exact instructions to reproduce the issue.)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:11
Top GitHub Comments
Not sure if this helps but I managed to get my react-markdown tests to run with CRA by modifying the test command
@jcgentr and @esetnik answers didn’t work for me. However, they pointed me in the correct direction. Especially because those solutions work for projects created with
create-react-app
. I needed to edit the regex pattern to the following.Basically, I just added these patterns:
rehype-*
hast-util-.*
mdast-util-gfm
web-namespaces
zwitch
hast-to-hyperscript
html-void-elements
gemoji
If running the tests still fails for you because
Jest encountered an unexpected token
, just try to add the folder, where the error occurred, from thenode_module
folder. Unfortunately, sometimes the error message does not directly point to the correct package, which should be transformed. For example, I always got an error message which pointed to themdast-util-gfm-autolink-literal
package, however, the error actually happened becausemdast-util-gfm
was not transformed. Just try to google the error. 😃