Reputation: 81
I have a project with NX structure(apps + libs). And I am writing tests for react + typescript lib. I faced the issue when I try to use suneditor + suneditor-react:
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\suneditor\src\plugins\index.js:4
import blockquote from './command/blockquote';
I found the files are not transformed when they are in node_modules and made this change to jest config:
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!suneditor|suneditor-react)"
]
After this I am continuously getting the error for all tests:
● Test suite failed to run
Cannot find module 'babel-preset-es2015'
at resolveStandardizedName (../../node_modules/@babel/core/lib/config/files/plugins.js:100:7)
at resolvePreset (../../node_modules/@babel/core/lib/config/files/plugins.js:48:10)
at loadPreset (../../node_modules/@babel/core/lib/config/files/plugins.js:67:20)
at createDescriptor (../../node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
at ../../node_modules/@babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (../../node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (../../node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
Jest config:
module.exports = {
displayName: 'component',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/pbc-client-journal',
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!suneditor|suneditor-react)"
]
};
And jest.preset.js:
const nxPreset = require('@nrwl/jest/preset');
module.exports = { ...nxPreset };
.babelrc:
{
"presets": [
[
"@nrwl/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
]
}
Could someone please help me with this issue?
Upvotes: 8
Views: 4826
Reputation: 183
You need to switch to babel.config.js
in your lib to be able to transform files from node_modules
. Here is what worked in my project:
babel.config.js
module.exports = {
presets: [
[
"@nrwl/react/babel",
{
runtime: "automatic",
useBuiltIns: "usage"
}
]
],
plugins: []
}
jest.config.js
module.exports = {
displayName: 'mylib',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': ['babel-jest', { cwd: __dirname }],
},
transformIgnorePatterns: [
"node_modules/(?!(@asyncapi)/)"
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/mylib',
};
Upvotes: 3