I am working on a Next.js project using TypeScript and for testing I use Jest and React Testing Lib. However, I encounter a SyntaxError: Cannot use import statement outside a module for components where I import rehype-raw.
As far as I understand this, Jest does not support ES6 so node_modules may need to be transformed. This can be configured using transformIgnorePatterns
. For example if rehype-raw
is causing this error using "transformIgnorePatterns": ["node_modules/(?!rehype-raw)/"]
should allow transformation of the rehype-raw
but no other module. And thus solve this error.
However, this does not work for me. But idk why and how I can solve this. No suggested solution I have found could solve this problem. I have attached my error output, jest.config.js and babel.rc file below.
Error output
FAIL test/pages/companies/[id].test.tsx
● Test suite failed to run
Jest encountered an unexpected token
[...]
Details:
/path/frontend-job/node_modules/rehype-raw/index.js:7
import {raw} from 'hast-util-raw'
^^^^^^
SyntaxError: Cannot use import statement outside a module
3 | import Image from 'next/image';
4 | import { Remark } from 'react-remark';
> 5 | import rehypeRaw from 'rehype-raw';
| ^
6 | import rehypeSanitize from 'rehype-sanitize';
7 | import { logError } from '@utils/logger';
8 |
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (components/markdown/JobMarkdown.tsx:5:1)
jest.config.js
const { resolve } = require('path');
module.exports = {
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
setupFiles: ['<rootDir>/test/setup.js'],
testPathIgnorePatterns: ['<rootDir>[/\\](node_modules|.next)[/\\]'],
transform: {
'^.+\.(ts|tsx)$': 'babel-jest',
},
transformIgnorePatterns: [
'node_modules/(?!rehype-raw)/',
],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
moduleNameMapper: {
// Force mocks: https://github.com/facebook/jest/issues/4262
'@api/axios': '<rootDir>/test/__mocks__/axios.js',
// Normal module aliases
'\.(css|less|sass|scss)$': 'identity-obj-proxy',
'\.(gif|ttf|eot|svg|png)$': '<rootDir>/test/__mocks__/fileMock.js',
'^@test/(.*)$': resolve(__dirname, './test/$1'),
'^@test/faker/(.*)$': resolve(__dirname, './test/faker/$1'),
'^@components/(.*)$': resolve(__dirname, './components/$1'),
'^@pages/(.*)$': resolve(__dirname, './pages/$1'),
'^@utils/(.*)$': resolve(__dirname, './utils/$1'),
'^@api/(.*)$': resolve(__dirname, './api/$1'),
'^@store/(.*)$': resolve(__dirname, './store/$1'),
},
testEnvironment: 'jsdom',
};
babel.rc
{
"presets": ["next/babel"]
}
4
Answers
Did you already use type:"module" in package.json?
Next has already out-of-the-box support for Jest, I’d suggest you follow the steps provided in the docs.
Jest does not support ECMAScript Modules which is what
hast-util-raw
uses. Second problem;transformIgnorePatterns
did not work too so below is the fix for myself with a Next.JS setup.1. Delete babel.rc
Delete your
babel.rc
. You can just use the below changes.2. Add moduleNameMapper
This fixes a different error where
parse5
can not be found. Required byhast-util-raw
3. Add transformIgnorePatterns
Add
transformIgnorePatterns
however right at the end. As I said, this did not work adding directly into the config for som reason. I also had to add every package used byhast-util-raw
. I am sure there is a better way to do this though 🙂My full config if anyone is interested…
What you need to understand is Next.js will tell Jest to not transform packages under
/node_modules
except those defined in their config file (thetranspilePackages
property).It’s a bit of a mess to add properly transform exclusions over this, but https://github.com/vercel/next.js/discussions/31152#discussioncomment-1697047 put me on the right track.
If you don’t want to manually manage the array since it depends on
transpilePackages
on the Next.js side, I reused their logic at https://github.com/vercel/next.js/blob/435eca3fc5b5dd23cad6cff43632bdcc777727d9/packages/next/src/build/jest/jest.ts#L156-L173 to transform both those fromtranspilePackages
but also the ones needed for Jest to run properly.Here the diff of my workaround:
https://github.com/inclusion-numerique/mediature/commit/cfe3ad85ab13d3f38d863afb8ee24b6995ae4e00
I’m sorry to not put direct code on this post, it would make things unclear.