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:
- Clone this repo.
- 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 havea.test.js
uncommitted, andpackage.json
has"name": "test"
uncommitted. - Run
yarn test-w
. You will get the same error.
I assume the cause is because:
coverage
is enabled. It has to be via Jest configcollectCoverage: true
, and not CLI--coverage
.watch
is enabled (and it possibly also occurs withwatch-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
orpackage.json
.- 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:
- Created 5 years ago
- Reactions:19
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My team was having this problem too, even without the
watch
mode or trying to usecollectCoverage: true
. My teammate discovered this solution: Put into the jest config@SimenB sure I’ll take a look 😄