285

Is there a way to have code coverage in the JavaScript Jest testing framework, which is built on top of Jasmine?

The internal framework does not print out the code coverage it gets. I've also tried using Istanbul, blanket, and JSCover, but none of them work.

Alex Palcuie
  • 4,434
  • 3
  • 22
  • 27

9 Answers9

337

When using Jest 21.2.1, I can see code coverage at the command line and create a coverage directory by passing --coverage to the Jest script. Below are some examples:

I tend to install Jest locally, in which case the command might look like this:

npx jest --coverage

I assume (though haven't confirmed), that this would also work if I installed Jest globally:

jest --coverage

The very sparse docs are here

When I navigated into the coverage/lcov-report directory I found an index.html file that could be loaded into a browser. It included the information printed at the command line, plus additional information and some graphical output.

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
ccalvert
  • 3,816
  • 1
  • 23
  • 22
  • 43
    Never use global modules. To ensure version consistency, and package presence, always use the executable in `node_modules` via `./node_modules/.bin/jest --coverage`. As long as the package is named in package.json, you can guarantee execution with the exact version of jest you expect. – taystack Jan 28 '19 at 14:48
  • How to get jest coverage only for changed files? – Shanika Ediriweera Nov 27 '19 at 05:59
  • 1
    Is there an easy way to fail tests if coverage is below a certain limit? Corresponding SO-question: https://stackoverflow.com/questions/60512167/how-to-check-jest-coverage-in-console – Stefan Mar 03 '20 at 17:03
  • @ShanikaEdiriweera https://jestjs.io/docs/en/cli.html#--collectcoveragefromglob – marcellothearcane May 07 '20 at 07:45
  • Can we change location of 'coverage' directory? – aagjalpankaj Jan 24 '22 at 23:13
  • Is there a way to calculate coverage without running tests? Kinda like a dry run? – Ali Mert Çakar Jan 09 '23 at 18:31
  • @aagjalpankaj Yes, by specifying a `collectCoverageFrom` array as documented [here](https://jestjs.io/docs/configuration#collectcoveragefrom-array). – Mike Lowery Jun 30 '23 at 21:18
117

UPDATE: 7/20/2018 - Added links and updated name for coverageReporters.

UPDATE: 8/14/2017 - This answer is totally outdated. Just look at the Jest docs now. They have official support and documentation about how to do this.

@hankhsiao has got a forked repo where Istanbul is working with Jest. Add this to your dev dependencies

 "devDependencies": {
     "jest-cli": "git://github.com/hankhsiao/jest.git"
 }

Also make sure coverage is enabled in your package.json jest entry and you can also specify formats you want. (The html is pretty bad ass).

 "jest": {
     "collectCoverage": true,
     "coverageReporters": ["json", "html"],
 }

See Jest documentation for coverageReporters (default is ["json", "lcov", "text"])

Or add --coverage when you invoke jest.

bformet
  • 13,465
  • 1
  • 22
  • 25
Adrian Adkison
  • 3,537
  • 5
  • 33
  • 36
  • 1
    The coverageFormats option has been removed -- looks like json and html are both generated every time. –  Mar 01 '15 at 15:47
  • 2
    I use the --coverage option but absolutely nothing different happens. – gbbr Mar 02 '15 at 13:28
  • 3
    It's (now) called `coverageReporters` and the default is `["json", "lcov", "text"]`. See http://facebook.github.io/jest/docs/configuration.html#coveragereporters-array-string – Karsten S. Apr 13 '17 at 18:32
  • dont work, Unknown option "coverageFormats" with value ["json", "html"] was found. – srghma Aug 12 '17 at 18:32
  • 1
    Thanks, setting coverageReporters: ["json", "lcov", "text"] in jest.config.ts fixed the issue for me. – Sachintha Udara Mar 20 '23 at 17:45
100

Jan 2019: Jest version 23.6

For anyone looking into this question recently especially if testing using npm or yarn directly

Currently, you don't have to change the configuration options

As per Jest official website, you can do the following to generate coverage reports:

1- For npm:

You must put -- before passing the --coverage argument of Jest

npm test -- --coverage

if you try invoking the --coverage directly without the -- it won't work

2- For yarn:

You can pass the --coverage argument of jest directly

yarn test --coverage
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
22

This works for me:

 "jest": {
    "collectCoverage": true,
    "coverageReporters": ["json", "html"]
  },
  "scripts": {
    "test": "jest  --coverage"
  },

Run:

yarn/npm test
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deen John
  • 3,522
  • 4
  • 29
  • 32
11

You can run npx jest --coverage -- path/to/your/file.spec.js that will show coverage for affected files

If you want to view this in browser you can do as follows,

  1. Go to Browser and CMD+O.
  2. Navigate to your repo and search for coverage/lcov-report/index.html

Then you can visually see all the coverage areas. enter image description here

You can also refer to this link below, for more information https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j

Anusha Bhat
  • 391
  • 4
  • 6
7
  1. Check the latest Jest (v 0.22): https://github.com/facebook/jest

  2. The Facebook team adds the Istanbul code coverage output as part of the coverage report and you can use it directly.

  3. After executing Jest, you can get a coverage report in the console and under the root folder set by Jest, you will find the coverage report in JSON and HTML format.

  4. FYI, if you install from npm, you might not get the latest version; so try the GitHub first and make sure the coverage is what you need.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Winters Huang
  • 761
  • 10
  • 18
  • 2
    The question was how to get Code coverage for Jest. That Istanbul and Jest don't work together might be true, but that doesn't answer the question. – David Raab Nov 06 '14 at 01:52
4

If you are having trouble with --coverage not working it may also be due to having coverageReporters enabled without 'text' or 'text-summary' being added. From the docs: "Note: Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output." Source

thebkr7
  • 51
  • 4
2

Configure your package.json file

"test": "jest --coverage",

enter image description here

Now run:

yarn test

All the test will start running and you will get the report. enter image description here

Saif Siddiqui
  • 856
  • 1
  • 13
  • 33
-12

I had the same issue and I fixed it as below.

  1. install yarn npm install --save-dev yarn
  2. install jest-cli npm install --save-dev jest-cli
  3. add this to the package.json "jest-coverage": "yarn run jest -- --coverage"

After you write the tests, run the command npm run jest-coverage. This will create a coverage folder in the root directory. /coverage/icov-report/index.html has the HTML view of the code coverage.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Laksh
  • 41
  • 2