Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest --coverage : help #126

Open
seeliang opened this issue Apr 1, 2016 · 28 comments
Open

Jest --coverage : help #126

seeliang opened this issue Apr 1, 2016 · 28 comments

Comments

@seeliang
Copy link

seeliang commented Apr 1, 2016

Hi everyone,

is anyway i can setup this with Jest?

many thanks

@jseminck
Copy link

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",

@tizmagik
Copy link

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

@ndelvalle
Copy link

ndelvalle commented Nov 4, 2016

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.

@cdaringe
Copy link

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

@ndelvalle
Copy link

yep @cdaringe that was the problem 👍

@joseluisq
Copy link

👍

@bcoe
Copy link
Contributor

bcoe commented Dec 11, 2016

@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

@seeliang
Copy link
Author

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

PhilippBaschke added a commit to PhilippBaschke/convert-to-stylelint that referenced this issue Dec 17, 2016
jest works with globals so they have to be added to the Eslint configuration.

I decided to move tests next to the files they test (jest picks up files that
are name *.test.js) to make them easier to locate. They need to be ignored by
babel to not show up in the lib folder (that is published to npm).

jest comes with a coverage tool and a watch mode so nyc and npm-watch are no
longer needed.
nickmerwin/node-coveralls#126 (comment)
PhilippBaschke added a commit to PhilippBaschke/convert-to-stylelint that referenced this issue Dec 17, 2016
jest works with globals so they have to be added to the Eslint configuration.

I decided to move tests next to the files they test (jest picks up files that
are name *.test.js) to make them easier to locate. They need to be ignored when
the code is published to npm (instead of the test folder).

jest comes with a coverage tool and a watch mode so nyc and npm-watch are no
longer needed.
nickmerwin/node-coveralls#126 (comment)
PhilippBaschke added a commit to PhilippBaschke/convert-to-stylelint that referenced this issue Dec 17, 2016
jest works with globals so they have to be added to the Eslint configuration.

I decided to move tests next to the files they test (jest picks up files that
are name *.test.js) to make them easier to locate.

The .test.js files need to be ignored when the application is build (they are
not needed to run and are shown as extra testcases otherwise).
https://github.com/skovhus/jest-codemods/blob/master/package.json
(scripts section)

jest comes with a coverage tool and a watch mode so nyc and npm-watch are no
longer needed.
nickmerwin/node-coveralls#126 (comment)

The jest configuration ensures that all src files are checked when the coverage
report is created (even when they are never required) and makes sure that the
compiled code is ignored.
PhilippBaschke added a commit to PhilippBaschke/convert-to-stylelint that referenced this issue Dec 17, 2016
jest works with globals so they have to be added to the Eslint configuration.

I decided to move tests next to the files they test (jest picks up files that
are name *.test.js) to make them easier to locate.

The .test.js files need to be ignored when the application is build (they are
not needed to run and are shown as extra testcases otherwise).
https://github.com/skovhus/jest-codemods/blob/master/package.json
(scripts section)

jest comes with a coverage tool and a watch mode so nyc and npm-watch are no
longer needed.
nickmerwin/node-coveralls#126 (comment)

The jest configuration ensures that all src files are checked when the coverage
report is created (even when they are never required) and makes sure that the
compiled code is ignored.

babel-jest is needed to transform the code (including tests) before it is
executed. It just needs to be installed (will be used automatically). ES6
modules do not work without it.
oatycreates added a commit to oatycreates/solitaire-react that referenced this issue Jan 1, 2017
dijonkitchen added a commit to dijonkitchen/reader that referenced this issue Jun 4, 2017
@iddan
Copy link

iddan commented Jul 30, 2017

Can this be added to the README?

@nickmccurdy
Copy link

nickmccurdy commented Sep 7, 2017

👍 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

@arkgast
Copy link

arkgast commented Sep 26, 2017

Why not use a script like this?

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

@nickmccurdy
Copy link

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.

@arkgast
Copy link

arkgast commented Sep 26, 2017

😮 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

@nickmccurdy
Copy link

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).

@dennisja
Copy link

dennisja commented Feb 9, 2018

@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

@nickmccurdy
Copy link

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

@dennisja
Copy link

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

Glavin001 added a commit to Unibeautify/unibeautify that referenced this issue Feb 13, 2018
@ummahusla
Copy link

This is how I got it to work

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

@jeff1evesque
Copy link

jeff1evesque commented May 31, 2018

@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! reactjs@1.0.0 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 reactjs@1.0.0 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
Copy link

@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
Copy link

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.

@petermikitsh
Copy link

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

@ambroiseRabier
Copy link

ambroiseRabier commented Jul 14, 2019

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.

@zdrawku
Copy link

zdrawku commented Jan 7, 2021

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

@adefemi171
Copy link

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

@zdrawku
Copy link

zdrawku commented Jan 11, 2021

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

@adefemi171
Copy link

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

@Yegorich555
Copy link

Yegorich555 commented Nov 30, 2021

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.*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests