question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax error when running Jest with coverage and watch (example repo included)

See original GitHub issue

🐛 Bug Report

This bug is likely the same as #6258.

In some cases, when I run jest --coverage --watch, I get the following error:

Running coverage on untested files...Failed to collect coverage from /Users/gary/Downloads/Unsorted/test/jest-bug/package.json
ERROR: Unexpected token, expected ; (2:8)
STACK: SyntaxError: Unexpected token, expected ; (2:8)

To Reproduce

Steps to reproduce the behavior:

  1. Clone this repo.
  2. Create a new branch from master. Reset HEAD to the commit before HEAD (“add package.json and yarn.lock”), but keep the changes, and unstage them. So now you have a.test.js uncommitted, and package.json has "name": "test" uncommitted.
  3. Run yarn test-w. You will get the same error.

I assume the cause is because:

  1. coverage is enabled. It has to be via Jest config collectCoverage: true, and not CLI --coverage.
  2. watch is enabled (and it possibly also occurs with watch-all), so only uncommitted changes are watched. In order to reproduce this bug, there needs to be changes to a test file, and changes in a non-JS file, like .eslintrc.json or package.json.
  3. This is actually a pretty common scenario, at least for me, so I get this error a lot.

Expected behavior

The error should not happen.

Link to repl or repo (highly encouraged)

The repo is linked to in step 1 of “Reproduce” section above.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.5
    CPU: x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
  Binaries:
    Node: 8.11.2 - /usr/local/bin/node
    Yarn: 1.9.4 - /usr/local/bin/yarn
    npm: 6.4.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.5.0 => 23.5.0

The bug does not exist in 22.4.4. The bug exists in 23.0.0.

Unfortunately, I can’t downgrade to Jest 22 for now, since that version has the jsdom errors instead.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:19
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
rodgobbicommented, Jan 11, 2019

My team was having this problem too, even without the watch mode or trying to use collectCoverage: true. My teammate discovered this solution: Put into the jest config

"jest": {
  ...
  "coveragePathIgnorePatterns": [
    "/node_modules/",
    "testconfig.js",
     ... (any file that doesn't need coverage check but is targeted by the coverage report)
    "package.json",
    "package-lock.json"
  ],
}
2reactions
stipsancommented, Jan 14, 2019

@SimenB sure I’ll take a look 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest finds tests but doesn't collect coverage - Stack Overflow
I found that when upgrading jest (from 23 to 26) that i had this issue, and the resolution was to run with the...
Read more >
Jest CLI Options
The jest command line runner has a number of useful options. You can run jest --help to view all available options. Many of...
Read more >
Test runner Addon | Storybook: Frontend workshop for UI ...
2 - Run tests with --coverage flag; 3 - Merging code coverage with coverage ... The Storybook test runner uses Jest as a...
Read more >
syntaxerror cannot use import statement outside a module ...
Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run...
Read more >
vscode-jest - Visual Studio Marketplace
How to set up the extension? How to trigger the test run? How to debug tests? How to use code coverage? How to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found