尝试使用babel 7运行Jest测试时出现以下错误
从'react'导入React;
^^^^^^
SyntaxError:意外的令牌导入
我的配置如下-
jest.config.json
{
"rootDir": "../../",
"testPathIgnorePatterns": [
"/node_modules/",
"/test/e2e"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|scss)$": "identity-obj-proxy"
},
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.jsx$": "babel-jest"
}
}
.babelrc (我认为接下来的6个是@ babel / env和@ babel / react!)
{
"presets": ["next/babel"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
package.json
"devDependencies": {
"@babel/cli": "^7.0.0-rc.1",
"@babel/core": "^7.0.0-rc.1",
"@babel/plugin-proposal-class-properties": "^7.0.0-rc.1",
"@babel/preset-env": "^7.0.0-rc.1",
"@babel/preset-react": "^7.0.0-rc.1",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.2",
"babel-jest": "^23.4.2",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jest": "^21.17.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"husky": "^0.14.3",
"identity-obj-proxy": "^3.0.0",
"jest": "^23.4.0",
"puppeteer": "^1.6.0",
"react-test-renderer": "^16.4.1",
"redux-mock-store": "^1.5.3",
"regenerator-runtime": "^0.12.1"
}
我尝试了很多配置设置,但似乎没有任何作用。
谢谢
答案 0 :(得分:0)
由于babel停止发布第2阶段的预设。有一些beta版本可以用于babel核心和预设。如果您不执行代码拆分,则tou可以排除动态导入。试试这些版本并更新您的devDependencies,让我知道问题是否仍然存在:
package.json
"devDependencies": {
"@babel/core": "7.0.0-beta.54",
"@babel/plugin-proposal-class-properties": "7.0.0-beta.54",
"@babel/plugin-proposal-decorators": "7.0.0-beta.54",
"@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.54",
"@babel/plugin-proposal-function-sent": "7.0.0-beta.54",
"@babel/plugin-proposal-json-strings": "7.0.0-beta.54",
"@babel/plugin-proposal-numeric-separator": "7.0.0-beta.54",
"@babel/plugin-proposal-throw-expressions": "7.0.0-beta.54",
"@babel/plugin-syntax-dynamic-import": "7.0.0-beta.54",
"@babel/plugin-syntax-import-meta": "7.0.0-beta.54",
"@babel/polyfill": "^7.0.0-beta.55",
"@babel/preset-env": "^7.0.0-beta.55",
"@babel/preset-react": "^7.0.0-beta.55",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.3",
"babel-jest": "^23.0.1",
"babel-loader": "^8.0.0-beta.0",
}
.bablerc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-json-strings",
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions"
]
}