jest: jest --watch doesn't show coverage in Vue

🐛 Bug Report

Running jest --watch in a Vue project with collectCoverage: true and collectCoverageFrom: ['src/**/*.{js,vue}'] doesn’t show coverage.

It works as expected when running just jest, but problem arises when running jest --watch

To Reproduce

Run jest & jest --watch in the reproduction repo

Expected behavior

jest works as expected, shows coverage info

> jest

 PASS  src/test.js
  test
    √ test (3ms)

------------|----------|----------|----------|----------|-------------------|
File        |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files   |    57.14 |       50 |        0 |    57.14 |                   |
 src        |        0 |      100 |      100 |        0 |                   |
  index.js  |        0 |      100 |      100 |        0 |             2,3,5 |
 src/App    |    72.73 |       50 |        0 |    72.73 |                   |
  index.vue |    72.73 |       50 |        0 |    72.73 |            7,8,11 |
------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.748s
Ran all test suites.

jest --watch does not show any coverage

> jest --watch

 PASS  src/test.js
  test
    √ test (7ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.985s
Ran all test suites.

Watch Usage: Press w to show more.

Link to repl or repo (highly encouraged)

Repro: https://github.com/laggingreflex/repro-vue-jest-template

Run npx envinfo --preset jest

  System:
    OS: Windows 10
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 10.15.0
    npm: 6.4.1

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 9
  • Comments: 17

Most upvoted comments

at this stage, I think this is actually a feature.

–watch is meant to run tests only on files that both:

  1. has a test(s)
  2. have been changed

–watchAll runs all tests on every file change.

IMO it’s just a little confusing for newbies (like myself) when using --watch and seeing no output. Perhaps this is an opportunity for clarification in the docs to make it absolutely obvious?? at this stage, i don’t think it’s a bug. In fact it’s actually quite smart as it wont waste time running tests on files that have not changed.

Same here in React after upgrade react-scripts 2.1.8 => 3.0.0

react-scripts test --coverage
File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 0 0 0 0

Test Suites: 56 passed, 56 total Tests: 308 passed, 308 total Snapshots: 0 total Time: 20.996s, estimated 21s Ran all test suites.

Watch Usage: Press w to show more.

And I try to use create-react-app to creat an new app, copy my code and paste in new-app folder. Run “react-scripts test --coverage” again. It works!?

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 100 100 100 100
components/App 100 100 100 100
… 100 100 100 100

Test Suites: 56 passed, 56 total Tests: 308 passed, 308 total Snapshots: 0 total Time: 24.253s Ran all test suites related to changed files.

Then I copy all code include “node_modules” in new-app folder and paste in my old object. No coverage again, always be empty

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 0 0 0 0

???


envinfo

System: OS: Windows 10 CPU: (4) x64 Intel® Core™ i5-7500 CPU @ 3.40GHz Binaries: Node: 8.14.0 - D:\nodejs\node.EXE Yarn: 1.12.3 - C:\Users\DELL\AppData\Roaming\npm\yarn.CMD npm: 6.4.1 - D:\nodejs\npm.CMD

I notice that jest only tests files that are not in a git commit. To test files that regardless of git status i used --watchAll to get around this.

I was having the same issue. With Jest version 24.9.0 if I add --watchAll=false or --watchAll=false it works. Apparently is the only presence of --watchAll flag, at least for me.

I’m experiencing this as well in a vanilla NodeJS project.

$ npx jest
 PASS  __tests__/index.js
 PASS  __tests__/compat.js
-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |    31.25 |    10.34 |    53.85 |       35 |                   |
 compat.js |     17.5 |      3.7 |    33.33 |    21.21 |... 60,61,77,78,82 |
 index.js  |      100 |      100 |      100 |      100 |                   |
-----------|----------|----------|----------|----------|-------------------|

Test Suites: 2 passed, 2 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        2.298s
Ran all test suites.
$ npx jest --watch
 PASS  __tests__/compat.js
 PASS  __tests__/index.js
-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |     17.5 |      3.7 |    33.33 |    21.21 |                   |
 compat.js |     17.5 |      3.7 |    33.33 |    21.21 |... 60,61,77,78,82 |
-----------|----------|----------|----------|----------|-------------------|

Test Suites: 2 passed, 2 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        2.504s
Ran all test suites related to changed files.

When run with --watch note that coverage for index.js is not reported.

Interestingly, when using --watch, if I make a change in index.js that causes a test to fail then coverage for index.js will be reported. However, if I instead make a change in __tests__/index.js that causes a test to fail then coverage for index.js is not reported.

envinfo

npx envinfo --preset jest
npx: installed 1 in 1.469s

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (8) x64 Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz
  Binaries:
    Node: 10.15.3 - ~/.local/nodeenv/lts/bin/node
    Yarn: 1.15.2 - ~/.local/nodeenv/lts/bin/yarn
    npm: 6.9.0 - ~/.local/nodeenv/lts/bin/npm
  npmPackages:
    jest: 24.7.1 => 24.7.1