react-markdown: Error importing component in Jest tests with TypeScript
Initial checklist
- I read the support docs
- I read the contributing guide
- I agree to follow the code of conduct
- I searched issues and couldn’t find anything (or linked relevant results below)
Affected packages and versions
react-markdown@npm:7.0.0 [bad77] (via npm:^7.0.0 [bad77])
Link to runnable example
https://github.com/tubbo/react-markdown-repro
Steps to reproduce
Install react-markdown
on a TypeScript project and try to use it in a test. I used CRA in my example but that isn’t strictly necessary, as I came across the issue on an Electron project. Trying to configure transformIgnorePatterns
to whitelist this module also proves impossible, as you have to also whitelist every dependency of the module which is a nightmare.
Expected behavior
Test should run without having trouble importing the file
Actual behavior
FAIL src/community/community.test.tsx
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/[...]/node_modules/react-markdown/index.js:5
import { ReactMarkdown } from './lib/react-markdown.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import ReactMarkdown from 'react-markdown'
Runtime
Node v14
Package manager
yarn v2
OS
macOS
Build and bundle tools
Webpack, Other (please specify in steps to reproduce)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 21 (1 by maintainers)
Commits related to this issue
- fixed import error with react-markdown and antd css see: https://app.circleci.com/pipelines/github/ademidun/atila-client-web-app/1769/workflows/89ba03df-a4bd-4adc-a12e-700e0c28b699/jobs/3338 https:/... — committed to ademidun/atila-client-web-app by ademidun 2 years ago
- mock react-markdown in application detail SyntaxError: Unexpected token 'export' https://app.circleci.com/pipelines/github/ademidun/atila-client-web-app/1772/workflows/ff5de33e-07e3-416b-af65-d01c488... — committed to ademidun/atila-client-web-app by ademidun 2 years ago
- Fix tests We need to mock reac-markdown and react-leaflet since jest does not fully support ESM syntax yet. See https://github.com/remarkjs/react-markdown/issues/635#issuecomment-956158474. — committed to dracor-org/einakter by cmil 2 years ago
(For folks who are googling after installing and cannot run jest) Since Jest has not fully supported ESM yet. So to make
react-markdown
work in jest, other than opt-in for ESM in jest, you guys can use one of following options (solutions tested with Create React App 4.0.3):react-markdown
You lose the ability to actually test the
react-markdown
package but the troublesome is now gone.react-markdown
version 6Since version 6 shipped with CommonJS build so it should be no problem working with Jest. This is easiest solution but not recommended. You can upgrade to newer version when jest fully support ESM or
react-markdown
bundles CommonJS (not likely to happen).babel-jest
(solution for CRA 4.0.3) (credit to https://github.com/nrwl/nx/issues/812#issuecomment-429488470)Update Aug 19, 2022: Add dependency “trim-lines” Update Nov 1, 2021: Add more dependencies, thanks to @lamai6
I ended up using
jest.mock
in mysetupTests.js
to mock out bothreact-select
andremark-gfm
:I’ve joined the answers from @mvh25 and @mikegoatly, and works like a charm.
__test__/__mocks__/react-markdown.js
jest.config.js
file add:Option 3 descibed by nvh95 above does not work, probably due to bug facebook/jest#11067 . So unless you want to downgrade to react-markdown 6, the only option is to mock react-markdown during testing. nvh95 described how above, but in addition to that I also had to add
Doesn’t seem to work anymore with Next.js 13 for me
Option 3 from this #635 comment still works but needs an update, as other dependencies have been added since the comment.
Solution
Environment
For whom is looking for a solution that works with Nextjs, notice
transformIgnorePatterns
doesn’t work the same way in NextJS due to hownext/jest
is implemented. More details here. This solution worked for me.@fbruckhoff Here is a list that worked for me with the latest
react-markdown
version