尝试运行测试时,Jest 会抛出遇到的意外 token 错误。我正在使用 ky http,这显然会导致问题并且不会被转译。
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
Z:\sepview.web\sepview-react\node_modules\ky\index.js:277
export default createInstance();
^^^^^^
SyntaxError: Unexpected token export
1 | import * as React from 'react';
> 2 | import ky from 'ky';
| ^
3 |
4 | class HttpService extends React.Component {
5 |
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/components/HttpService.tsx:2:1)
Test Suites: 1 failed, 1 total
这是我的 package.json
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.7",
"@fortawesome/free-solid-svg-icons": "^5.4.2",
"@fortawesome/react-fontawesome": "^0.1.3",
"@types/d3": "^5.0.1",
"bootstrap": "^4.1.3",
"bootswatch": "^4.1.3",
"d3": "^5.7.0",
"d3-array": "^2.0.2",
"jquery": "^3.3.1",
"ky": "^0.5.1",
"moment": "^2.22.2",
"node-sass": "^4.9.4",
"popper.js": "^1.14.4",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"typescript": "^3.1.6"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"@types/enzyme": "^3.1.14",
"@types/enzyme-adapter-react-16": "^1.0.3",
"@types/jest": "^23.3.9",
"@types/node": "^10.12.2",
"@types/react": "^16.4.18",
"@types/react-dom": "^16.0.9",
"@types/react-router": "^4.4.0",
"@types/react-router-dom": "^4.3.1",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0"
},
"proxy": "http://localhost:43947"
}
将transform或transformIgnorePatterns添加到package.json时,如下所示:
"jest": {
"transform": {
"^.+\\.js$": "babel-jest"
},
"transformIgnorePatterns": [
"/node_modules/(?!(@ky)/).*/"
]
},
我收到此错误:
Out of the box, Create React App only supports overriding these Jest options:
• collectCoverageFrom
• coverageReporters
• coverageThreshold
• globalSetup
• globalTeardown
• resetMocks
• resetModules
• snapshotSerializers
• watchPathIgnorePatterns.
These options in your package.json Jest configuration are not currently supported by Create React App:
• transform
• transformIgnorePatterns
If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.
我还有一个 babel.config.js,它是空的 atm:
module.exports = function () {
const presets = [];
const plugins = [];
return {
presets,
plugins
};
}
无法选择弹出。我尝试解决这个问题超过一周。非常感谢任何帮助。
最佳答案
我在使用 material-ui-next-pickers
时遇到了类似的问题,它在 dist/中使用 es6 导入
这是因为 material-ui-next-pickers
在 Jest 中运行之前没有被转译。 create-react-app
排除 ./node_modules
中的所有内容。
我们可以通过更改 transformIgnorePatterns
来解决此问题但如问题中所述,create-react-app
不支持它。
但是,我们仍然可以将其作为命令参数传递给 react-script test --transformIgnorePatterns\"node_modules/(?!my-module)/\"
示例
"scripts": {
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!material-ui-next-pickers)/\" --env=jsdom",
...
}
关于reactjs - 未转译节点模块的 Jest 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53554984/