Jest: An option switch off coverage in stdout, but still generate coverage/lcov.info

Created on 16 Mar 2017  路  20Comments  路  Source: facebook/jest


Do you want to request a feature or report a bug?
Feature

What is the current behavior?

I've got two test scripts in my package.json, which is likely to be pretty common:

    "test": "cross-env NODE_ENV=test jest --coverage",
    "test:watch": "cross-env NODE_ENV=test jest --watch"

The first one is used in CI and the second one is what I tend to keep running in the console as I type.

I recently discovered a nice Atom package called lcov-info by @jacogr, which lets me toggle line coverage right in the editor and see what lines I still need to cover:

Such a nice visual feedback suggests using Atom鈥檚 lcov-info together with jest --watch --coverage and this trick actually works! After I write a new test and then switch between the file.test.js and file.js, coverage indicators update themselves and this helps a lot for seeing what else is left to do. The problem I face though is that the coverage report appends to the console output each time and it becomes impossible to see whether the recently changed test has failed or not (the console is embedded into Atom and uses about 1/3 of the screen height, while the coverage report is about the whole screen height).

What is the expected behavior?

I'd like to be able to optionally suppress the coverage report from the standard output, while still keeping coverage/lcov.info up-to-date. E.g.: jest --watch --coverage --no-coverage-in-stdout or jest --watch --coverage=no-stdout. What do you guys think?

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest 19.0.2, jest section in package.json:

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js}",
      "!src/**/*.test.{js,jsx}"
    ],
    "testRegex": "tests/.*\\.test\\.js$"
  },

Most helpful comment

config seems to completely override the package.json section instead of inheriting it

Ouch, I didn't notice that. I'm +1 to reopening this, or some sort of similar issues that resolves the use case of selectively disabling coverage from the command line without duplicating configuration.

All 20 comments

This would be useful for a lot of different editors. I use Emacs and I like to keep a compilation buffer open with a similar watch script, and it's distracting to scroll up from the coverage output to keep my test failures. I personally don't find the coverage display useful on CI either, since I use tools like Coveralls to display my generated coverage separately.

There is a "coverageReporters" config option that can be used for this.

Thanks for your reply @cpojer and for pointing to coverageReports config option! This is exactly what I need, just through the command line. The reason is that I'd like to see the coverage output in stdout during build and in CI, but hide it when --watch is on. Like this:

"scripts": {
    "test": "jest --coverage --coverage-reporters=json,lcov,text",
    "test:watch": "jest --watch --coverage --coverage-reporters=lcov"
}

Coverage report unnecessarily clutters Atom's terminal in the --watch mode and it's hard to see whether a test I've just changed has failed.

I'm not sure if something like this can be done now, given that there is no --coverage-reporters among the CLI options. What would you suggest?

I would recommend trying Coveralls or a similar coverage hosting service, that way you can keep your coverage information visible online without filling build logs or local logs with it. There's a Node client which supports LCOV data through stdin and files (for Jest, you'll want to use coveralls < coverage/lcov.info), and you can configure it with a CI service to upload coverage automatically on every build.

@kachkaev I just found --config, this is probably what you want.

"scripts": {
  "test": "jest --coverage",
  "test:watch": "jest --watch --coverage --config '{ \"coverageReporters\" : [\"lcov\"]}"
}

Note that the coverage summary is still displayed unless coverageReporters is empty.

Thanks for your investigation @nickmccurdy! I've tried --config and it does exactly what you've said.

In addition to the unwanted coverage summary in stdout, there is one more pretty serious caveat: --config seems to completely override the package.json section instead of inheriting it. In my case jest ignored collectCoverageFrom and testRegex, so the coverage numbers increased significantly.

I feel like it would be great to reopen this issue to discuss whether it's worth implementing --coverage-reporters CLI option.

config seems to completely override the package.json section instead of inheriting it

Ouch, I didn't notice that. I'm +1 to reopening this, or some sort of similar issues that resolves the use case of selectively disabling coverage from the command line without duplicating configuration.

@cpojer could you please reconsider your decision? At least three people are voting for reopening this issue (at least to discuss it).

i think the alternative option was to implement .js based configs, so it's possible to do something like

const baseConfig = require('./base-config');

module.exports = {
   ...baseConfig,
  coverage: true,
};

@dmitriiabramov there's a PR for that: https://github.com/facebook/jest/pull/2445

Personally I would prefer having more options exposed as command line flags, or having a flag similar to --config that combines options instead of replacing them entirely. I think it would be confusing to need to have a JavaScript based config in a different place just to change certain features at the command line (or a build tool that calls the command line interface). However, I'm not against the JS config otherwise since it could be useful for automation.

Any chance to see this open again?

@FezVrasta js configs feature was shipped, so you can now programmatically create configs
see: example

How can I disable the text reporter and keep the html reporter using the new feature you mentioned? I don't think I understand. 馃様

@FezVrasta you should use coverageReporters option and set it to only have lcov but not text http://facebook.github.io/jest/docs/en/configuration.html#coveragereporters-array-string

Thanks for letting know about this new feature @dmitriiabramov! I almost achieved what I was after when upgrading to jest v20! Just curious if I'm facing a bug or just doing something wrong.

I moved my jestConfig from package.json to jest.config.js with the following contents:

module.exports = {
  collectCoverageFrom: ['src/**/*.{js}', '!src/**/__tests__/*.{js}'],
  collectCoverage: true,
  coverageReporters: process.env.CI ? ['text'] : ['lcov'],
  coverageThreshold: {
    global: {
      statements: 1,
      branches: 1,
      functions: 1,
      lines: 1,
    },
  },
  testRegex: '__tests__/.*\\.test\\.js$',
};

The idea is to get an overwhelming output in GitLab CI, but only generate the lcov files while I'm working in Atom with jest --watch. Strangely, I still get ======== Coverage summary ======== no matter what is set for coverageReporters (even []). Can the summary disabled as well? I would prefer not to see it in Atom, because it occupies 6 extra lines in a pretty short terminal.

Just to avoid that question, my package.json does not contain jestConfig anymore and there are also no other conflicting sources of the config. Changing ['lcov'] to ['lcov', 'text', 'json'] etc. in jest.config.js does make the difference, I just can't disable text-summary in any way.

@kachkaev this line comes from text-summary reporter in istanbul-reprots

and it seems like we force enabling it here

i'm not sure if we really want to do this, maybe we can just have it as a defalut

@dmitriiabramov do you think it's worth opening a separate issue? Or shall we reopen this one and continue the discussion?

@kachkaev we should open a separate issue for that!

Just I have other problem similar to this.
My situation is like that.
package.json file:
"scripts": {
"build": "webpack --config webpack.prod.js",
"start": "webpack-dev-server --open --config webpack.dev.js",
"watch": "webpack-dev-server --open --config webpack.dev.js",
"test": "NODE_ENV=test jest --coverage",
"test:watch": "NODE_ENV=test jest --watchAll",
"lint": "eslint -c ./.eslintrc.js src",
"format": "prettier-eslint --write \"src/*/.js\" \"webpack*.js\" && npm run lint"
},

I am using Webpack and Babel configuration instead of Creact React CLI, I did install Jest and enzyme but I can't execute "npm test" which says this error:

Apollos-iMac:web-react-test apollo$ npm test

[email protected] test /Volumes/Workspace/workspace/10:2018/US_React/web-react-test
NODE_ENV=test jest --coverage

sh: jest: command not found
npm ERR! Test failed. See above for more details.

I think I omitted NODE_ENV configuration for jest test.
Anybody can fix this problem?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bookman25 picture bookman25  路  79Comments

iffy picture iffy  路  137Comments

eldh picture eldh  路  84Comments

maraisr picture maraisr  路  77Comments

SimenB picture SimenB  路  131Comments