Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get test coverage with jest + puppeteer

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?

like image 537
vitaly-t Avatar asked Aug 01 '18 03:08

vitaly-t


1 Answers

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.

like image 61
Brian Adams Avatar answered Sep 28 '22 09:09

Brian Adams