Ray Pierce
Ray Pierce

Reputation: 59

Command line way to generate HTML coverageReport in Jest

In Jest, is there a way from the command line to generate an HTML coverage report if it is not defined in the jest.config.js files?

I only want to generate the HTML report some of the time rather than every time I run Jest. The only way I've been able to generate the HTML report was by changing the config manually.

Upvotes: 3

Views: 19375

Answers (3)

Butifarra
Butifarra

Reputation: 1114

Here is a portion from my test scripts in package.json that may help you generate coverage reports:

"test": "react-scripts test --env=jsdom",
"test:coverage": "react-scripts test --coverage"

My setup uses [email protected] with [email protected] and [email protected]

Upvotes: 0

enail131
enail131

Reputation: 65

By default the collectCoverage option for jest is set to false. The easiest way to get an HTML coverage report is to configure jest in either package.json or jest.config.js. You will also need to set the coverageDirectory in the config as well.

There are several different configuration options for coverage reporting. See here for all config settings: https://jestjs.io/docs/en/configuration.html#collectcoverage-boolean

Here is an example of how to configure jest in package.json with a few options.

{
  "name": "appname",
  "version": "1.0.0",
  "description": "description",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "postinstall": "jspm install"
  },
  "jest": {
    "scriptPreprocessor": "./preprocessor.js",
    "testPathIgnorePatterns": [
      "/node_modules/",
    ],
    "unmockedModulePathPatterns": [
      "./node_modules/react"
    ],
    "collectCoverage": true,
    "coverageDirectory": "path/to/coverage/reports",
  },
  "author": "author",
  "license": "ISC",
  "dependencies": {
    "del": "^1.1.1",
    "gulp": "^3.8.11",
    "gulp-filter": "^2.0.2",
    "gulp-load-plugins": "^0.10.0",
    "gulp-react": "^3.0.1",
    "gulp-shell": "^0.4.1",
    "harmonize": "^1.4.1",
    "jest-cli": "^0.4.1",
    "jspm": "^0.15.5",
    "react": "^0.13.2",
    "react-tools": "^0.13.2",
    "run-sequence": "^1.1.0"
  },
  "devDependencies": {
    "browser-sync": "^2.7.1",
    "gulp": "^3.8.11"
  },
  "jspm": {
    "directories": {},
    "dependencies": {
      "react": "npm:react@^0.13.2"
    },
    "devDependencies": {
      "babel": "npm:babel-core@^5.1.13",
      "babel-runtime": "npm:babel-runtime@^5.1.13",
      "core-js": "npm:core-js@^0.9.4"
    }
  }
}

Now when you run your test with jest --coverage the HTML reports will be generated in the specified directory.

Upvotes: 3

Sharkparty
Sharkparty

Reputation: 176

Have you tried to pass in a JSON config via the command line?

npx jest ./src/**/*.js --coverage --config='{ "coverageReporters": ["html"] }'

Upvotes: 2

Related Questions