Skip to content

Commit

Permalink
chore(coverage): fix coverage for events (#4787)
Browse files Browse the repository at this point in the history
We used to track API Coverage for public events, but this was regressed in the refactoring that
introduced `//lib/Events.js`.

This patch:
- Brings back API Coverage for events
- Combines all coverage-generated tests into a single one. This way
we can generate less data for flakiness dashboard.
  • Loading branch information
aslushnikov committed Aug 1, 2019
1 parent 7ee9af4 commit 5c7c45f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ if (process.env.BROWSER === 'firefox') {
testRunner,
});
if (process.env.COVERAGE)
utils.recordAPICoverage(testRunner, require('../lib/api'), CHROMIUM_NO_COVERAGE);
utils.recordAPICoverage(testRunner, require('../lib/api'), require('../lib/Events').Events, CHROMIUM_NO_COVERAGE);
});
}

Expand Down
32 changes: 19 additions & 13 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ?

/**
* @param {Map<string, boolean>} apiCoverage
* @param {Object} events
* @param {string} className
* @param {!Object} classType
*/
function traceAPICoverage(apiCoverage, className, classType) {
function traceAPICoverage(apiCoverage, events, className, classType) {
className = className.substring(0, 1).toLowerCase() + className.substring(1);
for (const methodName of Reflect.ownKeys(classType.prototype)) {
const method = Reflect.get(classType.prototype, methodName);
Expand All @@ -37,30 +38,35 @@ function traceAPICoverage(apiCoverage, className, classType) {
});
}

if (classType.Events) {
for (const event of Object.values(classType.Events))
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
if (events[classType.name]) {
for (const event of Object.values(events[classType.name])) {
if (typeof event !== 'symbol')
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
}
const method = Reflect.get(classType.prototype, 'emit');
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
if (this.listenerCount(event))
if (typeof event !== 'symbol' && this.listenerCount(event))
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
return method.call(this, event, ...args);
});
}
}

const utils = module.exports = {
recordAPICoverage: function(testRunner, api, disabled) {
recordAPICoverage: function(testRunner, api, events, disabled) {
const coverage = new Map();
for (const [className, classType] of Object.entries(api))
traceAPICoverage(coverage, className, classType);
traceAPICoverage(coverage, events, className, classType);
testRunner.describe('COVERAGE', () => {
for (const method of coverage.keys()) {
(disabled.has(method) ? testRunner.xit : testRunner.it)(`public api '${method}' should be called`, async({page, server}) => {
if (!coverage.get(method))
throw new Error('NOT CALLED!');
});
}
testRunner.it('should call all API methods', () => {
const missingMethods = [];
for (const method of coverage.keys()) {
if (!coverage.get(method) && !disabled.has(method))
missingMethods.push(method);
}
if (missingMethods.length)
throw new Error('Certain API Methods are not called: ' + missingMethods.join(', '));
});
});
},

Expand Down

0 comments on commit 5c7c45f

Please sign in to comment.