我有一个与NX结构(应用+库)的项目。我正在为react + typescript lib编写测试。当我尝试使用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';我发现这些文件在node_modules中没有转换,并对jest配置进行了以下更改:
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!suneditor|suneditor-react)"
]在此之后,我不断得到所有测试的错误:
● 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配置:
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)"
]
};和jest.preset.js:
const nxPreset = require('@nrwl/jest/preset');
module.exports = { ...nxPreset };.babelrc:
{
"presets": [
[
"@nrwl/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
]
}有人能帮我解决这个问题吗?
发布于 2021-09-29 09:16:38
为了能够从node_modules转换文件,您需要在库中切换到babel.config.js。以下是在我的项目中有效的方法:
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',
};https://stackoverflow.com/questions/68752772
复制相似问题