代码之家  ›  专栏  ›  技术社区  ›  Jason G

反应:意外标记,应为“,”

  •  -3
  • Jason G  · 技术社区  · 6 年前

    试图让jest测试为React项目工作。已安装babel和webpack,但仍出现以下错误:

    测试套件无法运行

    SyntaxError: .../user.js: Unexpected token, expected "," 
    
    export function user(toEnable: Bool, code: String = "") {
                                 ^
    

    B.法律改革委员会

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

    {
      "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 回复  |  直到 6 年前
        1
  •  0
  •   Jason G    6 年前

    多亏了Sulthan和JJJ,JS文件中混合了typescript。删除所有TS修复了该问题。