Jest错误意外的令牌......(ES6)

时间:2017-10-23 15:50:45

标签: javascript reactjs unit-testing ecmascript-6 jest

每当我在命令行中运行jest时,我都会收到以下错误:

 ● Test suite failed to run

/Users/<USER>/<Project>/src/login/LoginAPI.js:13
        ...headers,
        ^^^

SyntaxError: Unexpected token ...

它打破的代码使用ES6省略号:

headers: {
    ...headers
},

这就是我的.babelrc文件:

{ "presets":["env", "react"] }

这就是我在package.json中的内容:

"dependencies": {
    "express": "^4.15.4",
    "express-healthcheck": "^0.1.0",
    "js-cookie": "^2.1.4",
    "normalize.css": "^7.0.0",
    "query-string": "^5.0.0",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-router-dom": "^4.2.2"
  },
  "devDependencies": {
    "babel-jest": "^21.2.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "enzyme": "^3.1.0",
    "enzyme-adapter-react-15": "^1.0.2",
    "jest": "^21.2.1",
    "jest-cli": "^21.2.1",
    "react-scripts": "1.0.10"
  },
  "jest": {
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
    },
    "moduleFileExtensions": ["js"],
    "moduleDirectories": [
      "node_modules",
      "bower_components",
      "shared"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/yarn-cache/"
    ]
  }

4 个答案:

答案 0 :(得分:1)

您需要为此语法使用特定的babel预设。 Check this preset

npm install --save-dev babel-plugin-transform-object-rest-spread

然后将其添加到.babelrc

{
  "plugins": ["transform-object-rest-spread"]
}

您可能希望添加stage-2,因为它有更多ES6好东西。

注意: Jest can read your .babelrc file

答案 1 :(得分:1)

我相信有几个解决方案,这个GitHub Issue应该概述其中一些。我先试试这个:

 {
   "presets": ["es2015", "stage-3", "react"]
 }

答案 2 :(得分:1)

在巴贝尔7中, babel-plugin-transform-object-rest-spread会报告错误:SpreadProperty has been renamed to SpreadElement

所以我用:

npm install --save-dev @babel/plugin-proposal-object-rest-spread

和config .babelrc

{
  "plugins": ["@babel/plugin-proposal-object-rest-spread"]
}

答案 3 :(得分:0)

在package.json中添加“ schema-utils”:“ 2.6.6”作为“ dependencies”

"schema-utils": "2.6.6",