test: fix api coverage (#3114)

Coverage for browser-specific events was missing.
This commit is contained in:
Dmitry Gozman 2020-07-23 10:30:47 -07:00 committed by GitHub
parent 244ce45799
commit 30e21e0bee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,42 +21,43 @@
* @param {string} className * @param {string} className
* @param {!Object} classType * @param {!Object} classType
*/ */
function traceAPICoverage(apiCoverage, events, className, classType) { function traceAPICoverage(apiCoverage, api, events) {
const uninstalls = []; const uninstalls = [];
className = className.substring(0, 1).toLowerCase() + className.substring(1); for (const [name, classType] of Object.entries(api)) {
for (const methodName of Reflect.ownKeys(classType.prototype)) { const className = name.substring(0, 1).toLowerCase() + name.substring(1);
const method = Reflect.get(classType.prototype, methodName); for (const methodName of Reflect.ownKeys(classType.prototype)) {
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function') const method = Reflect.get(classType.prototype, methodName);
continue; if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
apiCoverage.set(`${className}.${methodName}`, false); continue;
const override = function(...args) { apiCoverage.set(`${className}.${methodName}`, false);
apiCoverage.set(`${className}.${methodName}`, true); const override = function(...args) {
return method.call(this, ...args); apiCoverage.set(`${className}.${methodName}`, true);
}; return method.call(this, ...args);
Object.defineProperty(override, 'name', { writable: false, value: methodName }); };
Reflect.set(classType.prototype, methodName, override); Object.defineProperty(override, 'name', { writable: false, value: methodName });
uninstalls.push(() => Reflect.set(classType.prototype, methodName, method)); Reflect.set(classType.prototype, methodName, override);
} uninstalls.push(() => Reflect.set(classType.prototype, methodName, method));
}
if (events[classType.name]) { if (events[name]) {
for (const event of Object.values(events[classType.name])) { const emitClassType = (name === 'BrowserContext' ? api['ChromiumBrowserContext'] : undefined) || classType;
if (typeof event !== 'symbol') for (const event of Object.values(events[name])) {
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false); if (typeof event !== 'symbol')
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
}
const method = Reflect.get(emitClassType.prototype, 'emit');
Reflect.set(emitClassType.prototype, 'emit', function(event, ...args) {
if (typeof event !== 'symbol' && this.listenerCount(event))
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
return method.call(this, event, ...args);
});
uninstalls.push(() => Reflect.set(emitClassType.prototype, 'emit', method));
} }
const method = Reflect.get(classType.prototype, 'emit');
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
if (typeof event !== 'symbol' && this.listenerCount(event))
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
return method.call(this, event, ...args);
});
uninstalls.push(() => Reflect.set(classType.prototype, 'emit', method));
} }
return () => uninstalls.forEach(u => u()); return () => uninstalls.forEach(u => u());
} }
/** /**
* @param {string} browserName * @param {string} browserName
*/ */
function apiForBrowser(browserName) { function apiForBrowser(browserName) {
const BROWSER_CONFIGS = [ const BROWSER_CONFIGS = [
@ -72,7 +73,7 @@ function apiForBrowser(browserName) {
name: 'Chromium', name: 'Chromium',
events: { events: {
...require('../../lib/events').Events, ...require('../../lib/events').Events,
...require('../../lib/chromium/events').Events, ChromiumBrowserContext: require('../../lib/chromium/events').Events.CRBrowserContext,
} }
}, },
]; ];
@ -98,15 +99,12 @@ function apiForBrowser(browserName) {
} }
/** /**
* @param {string} browserName * @param {string} browserName
*/ */
function installCoverageHooks(browserName) { function installCoverageHooks(browserName) {
const uninstalls = [];
const {api, events} = apiForBrowser(browserName); const {api, events} = apiForBrowser(browserName);
const coverage = new Map(); const coverage = new Map();
for (const [name, value] of Object.entries(api)) const uninstall = traceAPICoverage(coverage, api, events);
uninstalls.push(traceAPICoverage(coverage, events, name, value));
const uninstall = () => uninstalls.map(u => u());
return {coverage, uninstall}; return {coverage, uninstall};
} }