node-coveralls icon indicating copy to clipboard operation
node-coveralls copied to clipboard

Jest --coverage : help

Open seeliang opened this issue 8 years ago • 28 comments

Hi everyone,

is anyway i can setup this with Jest?

many thanks

seeliang avatar Apr 01 '16 22:04 seeliang

Just add: && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js

    "test": "node --harmony_proxies node_modules/.bin/jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",

jseminck avatar Aug 28 '16 08:08 jseminck

Thanks @jseminck would be good to add this to the README, I think?

tizmagik avatar Sep 17 '16 22:09 tizmagik

I followed @jseminck approach, but now Im getting the following error:

[error] "2016-11-04T14:51:39.503Z"  'error from lcovParse: ' 'Failed to parse string'
[error] "2016-11-04T14:51:39.505Z"  'input: ' ''
[error] "2016-11-04T14:51:39.506Z"  'error from convertLcovToCoveralls'

Any ideas? Thanks in advanced.

ndelvalle avatar Nov 04 '16 14:11 ndelvalle

@ndelvalle, most likely, your coverage/lcov.info is empty

cdaringe avatar Nov 16 '16 21:11 cdaringe

yep @cdaringe that was the problem 👍

ndelvalle avatar Nov 17 '16 09:11 ndelvalle

:+1:

joseluisq avatar Dec 06 '16 02:12 joseluisq

@jseminck @seeliang worth noting that Jest's coverage implementation outputs a .JSON report that is compatible with nyc and Istanbul in the coverage/ folder. So this would totally work:

nyc report --temp-directory=coverage --reporter=text-lcov | coveralls

bcoe avatar Dec 11 '16 01:12 bcoe

Thanks, guys. I will give a go later :)

seeliang avatar Dec 11 '16 22:12 seeliang

Can this be added to the README?

iddan avatar Jul 30 '17 08:07 iddan

👍 For adding to readme.

Is there any advantage to any specific coverage format? Which file in coverage should we pipe into coveralls if we add it to the readme?

  -rw-r--r--   1 nick  staff  10945 Sep  7 15:15 clover.xml
  -rw-r--r--   1 nick  staff  28542 Sep  7 15:15 coverage-final.json
  -rw-r--r--   1 nick  staff  23445 Sep  6 21:46 coverage.json
  -rw-r--r--   1 nick  staff  25730 Sep  6 03:59 coverage.raw.json
  drwxr-xr-x   9 nick  staff    306 Sep  7 13:36 lcov-report
  -rw-r--r--   1 nick  staff   4024 Sep  7 15:15 lcov.info

nickmccurdy avatar Sep 07 '17 19:09 nickmccurdy

Why not use a script like this?

jest --coverage && cat ./coverage/lcov.info | coveralls

arkgast avatar Sep 26 '17 04:09 arkgast

Personally I like to set "jest": { "collectCoverage": true } in package.json to enable coverage reporting everywhere, add coveralls as a devDependency, and have CI run coveralls < coverage/lcov.info.

nickmccurdy avatar Sep 26 '17 16:09 nickmccurdy

😮 now I got it, so most people don't have coveralls as a devDependency that's way ./node_modules/coveralls/bin/coveralls.js appears in their configurations.

I was asking about that part specially.

By the way I think this is the best approach to maintain configurations clearer. As a side effect you had clarify my doubt 😆, thanks man

arkgast avatar Sep 26 '17 18:09 arkgast

I don't think that's the case. Assuming that the other technique is to npm install coveralls in CI after a build, it would still install the package to node_modules with a binary in that location. Whether or not you install as a saved devDependency, it will only be executable directly via coveralls if you have it in your $PATH (some CI services like Travis do this automatically) or if you're calling it in an npm script (which does something similar).

nickmccurdy avatar Sep 26 '17 18:09 nickmccurdy

@nickmccurdy Your solution isnot supported by create-react-app It gives the warning below: Out of the box, Create React App only supports overriding these Jest options: • collectCoverageFrom • coverageReporters • coverageThreshold • snapshotSerializers. These options in your package.json Jest configuration are not currently supported by Create React App: • collectCoverage

dennisja avatar Feb 09 '18 23:02 dennisja

Yes, but you can still use jest --coverage in your build script even though the config option isn't supported.

nickmccurdy avatar Feb 10 '18 01:02 nickmccurdy

Yeah, I solved it by run yarn test --coverage which later runs jest --coverage

dennisja avatar Feb 10 '18 08:02 dennisja

This is how I got it to work

node_modules/.bin/jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js

ummahusla avatar Feb 22 '18 09:02 ummahusla

@jseminck, @tizmagik: sorry to bother in this thread. I'm using jest + enzyme, and hoping to pipe the coverage results to coveralls. So, I implemented test:coveralls, a script from my package.json:

  "scripts": {
    "build:browserify": "browserify -t [ babelify --presets env,stage-2,react ] content.jsx > ../../interface/static/js/content.js",
    "prebuild:dos2unix": "find . -type f -print0 | xargs -0 dos2unix",
    "watch:jsx": "onchange 'src/jsx/**/*.jsx' 'src/jsx/**/*.js' -- npm run build:browserify",
    "test": "NODE_ENV=test jest --config jest.config.js",
    "test:coveralls": "npm run test --coverage --coverageReporters=lcov.info | ./node_modules/coveralls/bin/coveralls.js"
  },

I've verified both coveralls.js, and lcov.info are nonempty. However, my travis build, seems to error:

[error] "2018-05-31T16:37:12.946Z"  'error from convertLcovToCoveralls'
/home/travis/build/jeff1evesque/machine-learning/src/jsx/node_modules/coveralls/bin/coveralls.js:18
        throw err;
        ^
Failed to parse string
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:coveralls: `npm run test --coverage --coverageReporters=lcov.info | ./node_modules/coveralls/bin/coveralls.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test:coveralls script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/travis/.npm/_logs/2018-05-31T16_37_12_965Z-debug.log
The command "npm run test:coveralls" exited with 1.

jeff1evesque avatar May 31 '18 16:05 jeff1evesque

@jseminck, @tizmagik: nevermind, I think I've fixed it. The corresponding travis build, seems to have run the following without erroring:

cat lcov.info | ./node_modules/coveralls/bin/coveralls.js

I guess I'll merge my corresponding pull request, and see if my webhook updates coveralls.

jeff1evesque avatar May 31 '18 19:05 jeff1evesque

Ah, didn't have to wait for PR to be merged. Seems like the configurations worked. Thank you everyone, pretty much recycled different aspects from various comments in this issue.

jeff1evesque avatar May 31 '18 19:05 jeff1evesque

@nickmccurdy's solution worked for me. Thanks Nick!

petermikitsh avatar Jul 21 '18 09:07 petermikitsh

Based on @jeff1evesque and @nickmccurdy answer:

package.json

"scripts": {
  "ci-process-cover": "cat coverage/lcov.info | coveralls"
}

jest.config.js

module.exports = {
  "collectCoverage": true
  // ...
}

.travis.yml

script:
  - npm run test
after_script:
  - npm run ci-process-cover

Or have tests run twice:

package.json

`cover`: `jest --coverage --coverageReporters=text-lcov | coveralls`

.travis.yml

script:
  - npm run test
after_script:
  - npm run cover

If you replace npm run test by npm run cover, coverage will work, but your CI will not fail if one test fail.

ambroiseRabier avatar Jul 14 '19 09:07 ambroiseRabier

I've faced a similar problem, hope this post might help you a little bit.

zdrawku avatar Jan 07 '21 16:01 zdrawku

Hi all am facing

[error] "2021-01-11T05:26:15.227Z"  'error from lcovParse: ' 'Failed to parse string'
[error] "2021-01-11T05:26:15.230Z"  'input: ' ''
[error] "2021-01-11T05:26:15.230Z"  'error from convertLcovToCoveralls'

when running it locally and in my Github action. How do I generate .lcov so the file won't be empty? @nickmccurdy @jseminck @tizmagik

adefemi171 avatar Jan 11 '21 05:01 adefemi171

@adefemi171 if you are using Angular --code-coverage will generate a coverage report for you.

zdrawku avatar Jan 11 '21 07:01 zdrawku

Am using Node and Jest and I ran jest --coverage which created a coverage folder but with an empty lcov.info @zdrawku

adefemi171 avatar Jan 11 '21 18:01 adefemi171

Why not use a script like this?

jest --coverage && cat ./coverage/lcov.info | coveralls

Because it doesn't work on Windows.

Do we have any cross-platform solution ?

I figured only following:

jest --coverage && coveralls < coverage\\lcov.info

Ordinary pipe jest --coverage --coverageReporters=text-lcov | coveralls somehow doesn't work with NodeJS 14.15.*

Yegorich555 avatar Nov 30 '21 05:11 Yegorich555