React:意外的令牌,预期为“,”

时间:2019-01-11 18:37:09

标签: reactjs babel

试图获得最好的测试以用于React项目。安装了babel和webpack,仍然出现此错误:

测试套件无法运行

SyntaxError: .../user.js: Unexpected token, expected "," 

export function user(toEnable: Bool, code: String = "") {
                             ^

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

package.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的问题。但是我不确定如何修复它,我尝试过的所有方法都无法修复它。每个人的问题都是“实际”语法错误,而这只是一个命名参数列表。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

感谢Sulthan和JJJ-JS文件中混有打字稿。删除所有TS即可解决问题。