mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: introduce //lib/api.js (#3835)
Introduce `//lib/api.js` that declares a list of publicly exposed classes. The `//lib/api.js` list superceedes dynamic `helper.tracePublicAPI()` calls and is used in the following places: - [ASYNC STACKS]: generate "async stacks" for publicy exposed API in `//index.js` - [COVERAGE]: move coverage support from `//lib/helper` to `//test/utils` - [DOCLINT]: get rid of 'exluded classes' hardcoded list This will help us to re-use our coverage and doclint infrastructure for Puppeteer-Firefox. Drive-By: it turns out we didn't run coverage for `SecurityDetails` class, so we lack coverage for a few methods there. These are excluded for now, sanity tests will be added in a follow-up.
This commit is contained in:
@@ -18,7 +18,51 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
|
||||
|
||||
/**
|
||||
* @param {Map<string, boolean>} apiCoverage
|
||||
* @param {string} className
|
||||
* @param {!Object} classType
|
||||
*/
|
||||
function traceAPICoverage(apiCoverage, 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);
|
||||
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
|
||||
continue;
|
||||
apiCoverage.set(`${className}.${methodName}`, false);
|
||||
Reflect.set(classType.prototype, methodName, function(...args) {
|
||||
apiCoverage.set(`${className}.${methodName}`, true);
|
||||
return method.call(this, ...args);
|
||||
});
|
||||
}
|
||||
|
||||
if (classType.Events) {
|
||||
for (const event of Object.values(classType.Events))
|
||||
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))
|
||||
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
|
||||
return method.call(this, event, ...args);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const utils = module.exports = {
|
||||
recordAPICoverage: function(testRunner, api, disabled) {
|
||||
const coverage = new Map();
|
||||
for (const [className, classType] of Object.entries(api))
|
||||
traceAPICoverage(coverage, 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!');
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user