8

I have project Excellent.js setup for automatic testing with jest and puppeteer, which successfully runs all the tests, which can be seen on Travis CI.

But after a lot of configuration tweaks I have been unable to make it report correct coverage. No matter what tests are executed, the coverage does not reflect it at all.

The library contains only a single JavaScript file excellent.js, and my jest.config.js was set up as instructed for coverage:

module.exports = {
    collectCoverage: true,
    collectCoverageFrom: [
        'src/excellent.js'
    ],
    testURL: 'http://localhost/',
    setupFiles: [
        './src/excellent.js'
    ]
};

Here're all the tests, which all pass if you do first npm install, and then npm test.

So what am I missing? Why can't I get the coverage reported correctly?

Community
  • 1
  • 1
vitaly-t
  • 24,279
  • 15
  • 116
  • 138

1 Answers1

9

ISSUE

Most of the tests are using Puppeteer and when the code is executed in the browser provided by Puppeteer, that code execution is not reflected in the Jest code coverage reports.

SOLUTION

None of the tests require Puppeteer so I refactored them as Jest tests. The code coverage is now accurate and is currently the following:

excellent.js | 63.47 | 48.7 | 57.78 | 62.96

I created a pull request with these changes.

Additional Info

It is now possible to generate code coverage reports for Puppeteer pages and there is a library to help view them in Instanbul but those code coverage reports are generated independently from Jest.

To do testing in Puppeteer pages and have the coverage from those tests reflected in the reports generated by Jest would require merging the Puppeteer page coverage reports with the Jest coverage report.

Brian Adams
  • 43,011
  • 9
  • 113
  • 111
  • 1
    This is a great effort, thank you! Integrating together all these test packages is such a daunting task. And it doesn't get any easier. I'm glad I can at least stick to just one of them - `jest`. – vitaly-t Aug 04 '18 at 19:11
  • 1
    your welcome, glad it was helpful! yeah, the tests really cleaned up nicely, it always feels great to delete a bunch of code and dependencies good luck with the project! – Brian Adams Aug 04 '18 at 19:31
  • @BrianAdams Why https://jestjs.io/docs/en/puppeteer#__docusaurus explain: _Generating code coverage for test files using Puppeteer is currently not possible if your test uses page.$eval, page.$$eval or page.evaluate as the passed function is executed outside of Jest's scope._ – storenth Jan 18 '20 at 10:25
  • @BrianAdams Is it possible to replicate drag and drop functionality with Jest? – lbragile Jan 17 '21 at 00:31