[英]React: Unexpected token, expected ","
試圖讓 jest 測試適用於 React 項目。 安裝了babel和webpack,還是報錯:
測試套件無法運行
SyntaxError: .../user.js: Unexpected token, expected ","
export function user(toEnable: Bool, code: String = "") {
^
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
包.json
{
"version": "0.1.0",
"private": true,
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"axios-mock-adapter": "^1.15.0",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.5",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-to-json": "^3.3.5",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-standard": "^4.0.0",
"husky": "^0.14.3",
"jest": "^23.6.0",
"jest-css-modules-transform": "^2.1.1",
"lint-staged": "^4.0.2",
"prettier": "^1.10.2",
"react-test-renderer": "^16.7.0",
"redux-mock-store": "^1.5.3",
"webpack": "^4.28.4",
"webpack-cli": "^3.2.1"
},
"dependencies": {
"axios": "~0.15.2",
"classnames": "~2.2.5",
"js-file-download": "^0.4.1",
"lodash": "^4.17.11",
"moment": "^2.15.2",
"node-sass-chokidar": "0.0.3",
"npm": "^6.4.1",
"npm-run-all": "^4.0.2",
"numbro": "^1.11.0",
"prop-types": "^15.5.10",
"qrcode.react": "^0.7.2",
"react": "^16.3.2",
"react-addons-css-transition-group": "^15.6.2",
"react-clipboard.js": "~1.1.3",
"react-datetime": "~2.14.0",
"react-dom": "^16.3.2",
"react-dropzone-component": "^2.0.0",
"react-gmaps": "^1.5.0",
"react-image-crop": "^2.0.8",
"react-js-pagination": "~2.0.1",
"react-loadable": "^5.4.0",
"react-modal": "~3.4.4",
"react-notifications": "^1.3.0",
"react-redux": "~4.4.5",
"react-router-dom": "^4.2.2",
"react-router-redux": "~4.0.6",
"react-scripts": "^2.1.1",
"react-scroll": "^1.7.3",
"react-tabs": "^0.8.2",
"redux": "~3.6.0",
"redux-persist": "^4.0.1",
"redux-thunk": "~2.1.0",
"source-map-explorer": "^1.6.0",
"store2": "~2.3.2",
"universal-cookie": "^2.0.8",
"validator": "~6.1.0"
},
"lint-staged": {
"src/**/*.{js,jsx}": [
"prettier --write --trailing-comma es5 --semi false --print-width 160",
"git add"
]
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>__tests__/setup/setupEnzyme.js",
"testPathIgnorePatterns": [
"<rootDir>/__tests__/setup/"
]
}
}
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
],
},
}
測試本身只是一個簡單的測試,它只是創建了組件,但由於這個參數問題,組件創建失敗了。 我環顧四周,似乎無法找到解決此問題的方法。
我確定這是 ES6 + babel 的問題。 但我不確定如何修復它,我嘗試過的所有方法都無法修復它。 其他人的問題都是“真正的”語法錯誤,而這只是一個命名參數列表。
任何想法將不勝感激。
感謝 Sulthan 和 JJJ - 在 JS 文件中混合了打字稿。 刪除所有 TS 解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.