无法通过 jest + puppeteer 获得测试覆盖

Can't get test coverage with jest + puppeteer

我有项目Excellent.js setup for automatic testing with jest and puppeteer, which successfully runs all the tests, which can be seen on Travis CI

但经过大量配置调整后,我无法使其报告正确的覆盖率。无论执行什么测试,覆盖率根本反映不出来。

该库仅包含一个 JavaScript 文件 excellent.js,并且我的 jest.config.js 是按照说明设置的:

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

这里是all the tests,如果你先npm install,然后npm test

,都可以通过

那我错过了什么?为什么我无法正确报告覆盖率?

问题

大多数测试都使用 Puppeteer,当代码在 Puppeteer 提供的浏览器中执行时,代码执行不会反映在 Jest 代码覆盖率报告中。

解决方案

None 的测试需要 Puppeteer,因此我将它们重构为 Jest 测试。代码覆盖率现在是准确的,目前如下:

excellent.js | 63.47 | 48.7 | 57.78 | 62.96

我创建了一个pull request with these changes

附加信息

现在可以 generate code coverage reports for Puppeteer pages and there is a library to help view them in Instanbul 但这些代码覆盖率报告是独立于 Jest 生成的。

要在 Puppeteer 页面中进行测试并将这些测试的覆盖率反映在 Jest 生成的报告中,需要将 Puppeteer 页面覆盖率报告与 Jest 覆盖率报告合并。