mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: update how we track coverage during unit tests (#5779)
* chore: update how we track coverage during unit tests The old method of tracking coverage was causing issues. If a test failed on CI, that test's failure would be lost because the test failing would in turn cause the coverage to fail, but the `process.exit(1)` in the coverage code caused Mocha to not output anything useful. Instead the coverage checker now: * tracks the coverage in memory in a Map (this hasn't changed) * after all tests, writes that to disk in test/coverage.json (which is gitignored) * we then run a single Mocha test that asserts every method was called. This means if the test run fails, the build will fail and give the error about that test run, and that output won't be lost when the coverage then fails too. Co-authored-by: Mathias Bynens <mathias@qiwi.be>
This commit is contained in:
24
test/assert-coverage-test.js
Normal file
24
test/assert-coverage-test.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const {describe, it} = require('mocha');
|
||||
const {getCoverageResults} = require('./coverage-utils');
|
||||
const expect = require('expect');
|
||||
|
||||
describe('API coverage test', () => {
|
||||
it('calls every method', () => {
|
||||
if (!process.env.COVERAGE) return;
|
||||
|
||||
const coverageMap = getCoverageResults();
|
||||
const missingMethods = [];
|
||||
for (const method of coverageMap.keys()) {
|
||||
if (!coverageMap.get(method))
|
||||
missingMethods.push(method);
|
||||
}
|
||||
if (missingMethods.length) {
|
||||
console.error('\nCoverage check failed: not all API methods called. See above output for list of missing methods.');
|
||||
console.error(missingMethods.join('\n'));
|
||||
}
|
||||
|
||||
// We know this will fail because we checked above
|
||||
// but we need the actual test to fail.
|
||||
expect(missingMethods.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -26,6 +26,9 @@
|
||||
* We run this when COVERAGE=1.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* @param {Map<string, boolean>} apiCoverage
|
||||
* @param {Object} events
|
||||
@@ -59,9 +62,37 @@ function traceAPICoverage(apiCoverage, events, className, classType) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function() {
|
||||
const coverageLocation = path.join(__dirname, 'coverage.json');
|
||||
|
||||
const clearOldCoverage = () => {
|
||||
try {
|
||||
fs.unlinkSync(coverageLocation);
|
||||
} catch (error) {
|
||||
// do nothing, the file didn't exist
|
||||
}
|
||||
};
|
||||
const writeCoverage = coverage => {
|
||||
fs.writeFileSync(coverageLocation, JSON.stringify([...coverage.entries()]));
|
||||
};
|
||||
|
||||
const getCoverageResults = () => {
|
||||
let contents;
|
||||
try {
|
||||
contents = fs.readFileSync(coverageLocation, {encoding: 'utf8'});
|
||||
} catch (error) {
|
||||
console.error('Warning: coverage file does not exist or is not readable.');
|
||||
}
|
||||
|
||||
const coverageMap = new Map(JSON.parse(contents));
|
||||
return coverageMap;
|
||||
};
|
||||
|
||||
const trackCoverage = () => {
|
||||
clearOldCoverage();
|
||||
const coverageMap = new Map();
|
||||
|
||||
before(() => {
|
||||
|
||||
const api = require('../lib/api');
|
||||
const events = require('../lib/Events');
|
||||
for (const [className, classType] of Object.entries(api))
|
||||
@@ -69,16 +100,11 @@ module.exports = function() {
|
||||
});
|
||||
|
||||
after(() => {
|
||||
const missingMethods = [];
|
||||
for (const method of coverageMap.keys()) {
|
||||
if (!coverageMap.get(method))
|
||||
missingMethods.push(method);
|
||||
}
|
||||
if (missingMethods.length) {
|
||||
console.error('\nCoverage check failed: not all API methods called. See above ouptut for list of missing methods.');
|
||||
console.error(Array.from(missingMethods).join('\n'));
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('\nAll Puppeteer API methods were called. Coverage test passed.\n');
|
||||
writeCoverage(coverageMap);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
trackCoverage,
|
||||
getCoverageResults
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ const fs = require('fs');
|
||||
const os = require('os');
|
||||
const puppeteer = require('../');
|
||||
const utils = require('./utils');
|
||||
const assertCoverage = require('./coverage-utils');
|
||||
const {trackCoverage} = require('./coverage-utils');
|
||||
|
||||
const setupServer = async() => {
|
||||
const assetsPath = path.join(__dirname, 'assets');
|
||||
@@ -115,7 +115,7 @@ global.describeChromeOnly = (...args) => {
|
||||
};
|
||||
|
||||
if (process.env.COVERAGE)
|
||||
assertCoverage();
|
||||
trackCoverage();
|
||||
|
||||
console.log(
|
||||
`Running unit tests with:
|
||||
|
||||
Reference in New Issue
Block a user