SyntaxError:意外的令牌(用于开玩笑

时间:2017-06-25 06:20:13

标签: javascript testing jest

在设置了一些postcss和webpack后,我的一些开玩笑测试失败了。所有失败都是因为同样的错误。非常奇怪的错误,似乎Jest甚至无法识别箭头功能而不是async/await

FAIL  tests/backend/integration/models/threadsModel.test.js
  ● Test suite failed to run

    /Users/albertgao/codes/node/projectTalk/tests/backend/integration/models/threadsModel.test.js:115
        test('Should return empty list when no data could be fetched', async () => {
                                                                             ^
    SyntaxError: Unexpected token (

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)

测试很简单并且之前通过了:

test('Should return empty list when no data could be fetched', async () => {
    const result = await ThreadModel.getThreads(threads[19], 5);
    expect(result).toHaveLength(0);
});

我试过了:

  • --no-cache
  • 删除node_modules并重新安装所有模块

仍然有这个错误。

这是我调用测试的方式:

"NODE_ENV=test ./node_modules/.bin/jest --no-cache --config=configs/jest.config.json",

这是jest.config.js文件夹中的./configs/

{
  "verbose": true,
  "rootDir": "../",
  "testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/tests/acceptance/", "<rootDir>/tests/coverageReport/", "<rootDir>/dist/"],
  "collectCoverage": false,
  "coverageDirectory": "tests/coverageReport",
  "collectCoverageFrom" : ["**/src/**"],
  "coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/configs/", "<rootDir>/src/config/appconfig.json", "<rootDir>/dist/"],
  "coverageReporters": ["text", "text-summary"]
}

这是.babelrc文件夹中的./configs/

{
  "presets": [["env",{"modules":false}], "react"],
  "plugins": ["react-hot-loader/babel"],
  "ignore": [
    "tests/",
    "dist/",
    "node_modules/",
    "src/backend/",
    "public"
  ]
}

我使用最新的Jest

任何解决方案?

1 个答案:

答案 0 :(得分:1)

因为ES2017支持异步箭头功能。所以你必须配置一个javascript转换器,例如:Babel

 npm install --save-dev babel-jest regenerator-runtime babel-preset-stage-0

.babelrc

{
  "presets": ["es2015", "stage-0"]
}

您可以在Additional Configuration进一步查看正确的配置。