Albert Gao
Albert Gao

Reputation: 3769

SyntaxError: Unexpected token ( for jest

After setting up some postcss and webpack, some of my jest tests failed. All failed for the same error. Very strange error, it seems that Jest can't even recognize arrow function rather than 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)

The test is simple and passed before:

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

I tried:

Still got this error.

This is how I invoke the test:

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

This is my jest.config.js in ./configs/ folder:

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

This is my .babelrc in ./configs/ folder:

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

I use the latest Jest

Any solution?

Upvotes: 4

Views: 5191

Answers (1)

holi-java
holi-java

Reputation: 30696

since the async arrow function is supported in ES2017. so you must config a javascript transpiler, e.g: Babel.

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

.babelrc

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

you can see the jest configuration as further at Additional Configuration.

Upvotes: 1

Related Questions