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,9 +21,10 @@
* @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)) {
const className = name.substring(0, 1).toLowerCase() + name.substring(1);
for (const methodName of Reflect.ownKeys(classType.prototype)) { for (const methodName of Reflect.ownKeys(classType.prototype)) {
const method = Reflect.get(classType.prototype, methodName); const method = Reflect.get(classType.prototype, methodName);
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function') if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
@ -37,21 +38,21 @@ function traceAPICoverage(apiCoverage, events, className, classType) {
Reflect.set(classType.prototype, methodName, override); Reflect.set(classType.prototype, methodName, override);
uninstalls.push(() => Reflect.set(classType.prototype, methodName, method)); uninstalls.push(() => Reflect.set(classType.prototype, methodName, method));
} }
if (events[name]) {
if (events[classType.name]) { const emitClassType = (name === 'BrowserContext' ? api['ChromiumBrowserContext'] : undefined) || classType;
for (const event of Object.values(events[classType.name])) { for (const event of Object.values(events[name])) {
if (typeof event !== 'symbol') if (typeof event !== 'symbol')
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false); apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
} }
const method = Reflect.get(classType.prototype, 'emit'); const method = Reflect.get(emitClassType.prototype, 'emit');
Reflect.set(classType.prototype, 'emit', function(event, ...args) { Reflect.set(emitClassType.prototype, 'emit', function(event, ...args) {
if (typeof event !== 'symbol' && this.listenerCount(event)) if (typeof event !== 'symbol' && this.listenerCount(event))
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true); apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
return method.call(this, event, ...args); return method.call(this, event, ...args);
}); });
uninstalls.push(() => Reflect.set(classType.prototype, 'emit', method)); uninstalls.push(() => Reflect.set(emitClassType.prototype, 'emit', method));
}
} }
return () => uninstalls.forEach(u => u()); return () => uninstalls.forEach(u => u());
} }
@ -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,
} }
}, },
]; ];
@ -101,12 +102,9 @@ 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};
} }