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

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

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",

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

{
  "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"]
}

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

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

我使用最新的Jest

有解决方案吗

解决方法:

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

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

.babelrc

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

您可以在Additional Configuration进一步查看jest配置.

上一篇:动态路由,面包屑


下一篇:VMware宣布计划收购Boxer公司