Skip to content

Commit 30e21e0

Browse files
authored
test: fix api coverage (#3114)
Coverage for browser-specific events was missing.
1 parent 244ce45 commit 30e21e0

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

test/jest/coverage.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,43 @@
2121
* @param {string} className
2222
* @param {!Object} classType
2323
*/
24-
function traceAPICoverage(apiCoverage, events, className, classType) {
24+
function traceAPICoverage(apiCoverage, api, events) {
2525
const uninstalls = [];
26-
className = className.substring(0, 1).toLowerCase() + className.substring(1);
27-
for (const methodName of Reflect.ownKeys(classType.prototype)) {
28-
const method = Reflect.get(classType.prototype, methodName);
29-
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
30-
continue;
31-
apiCoverage.set(`${className}.${methodName}`, false);
32-
const override = function(...args) {
33-
apiCoverage.set(`${className}.${methodName}`, true);
34-
return method.call(this, ...args);
35-
};
36-
Object.defineProperty(override, 'name', { writable: false, value: methodName });
37-
Reflect.set(classType.prototype, methodName, override);
38-
uninstalls.push(() => Reflect.set(classType.prototype, methodName, method));
39-
}
40-
41-
if (events[classType.name]) {
42-
for (const event of Object.values(events[classType.name])) {
43-
if (typeof event !== 'symbol')
44-
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
26+
for (const [name, classType] of Object.entries(api)) {
27+
const className = name.substring(0, 1).toLowerCase() + name.substring(1);
28+
for (const methodName of Reflect.ownKeys(classType.prototype)) {
29+
const method = Reflect.get(classType.prototype, methodName);
30+
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
31+
continue;
32+
apiCoverage.set(`${className}.${methodName}`, false);
33+
const override = function(...args) {
34+
apiCoverage.set(`${className}.${methodName}`, true);
35+
return method.call(this, ...args);
36+
};
37+
Object.defineProperty(override, 'name', { writable: false, value: methodName });
38+
Reflect.set(classType.prototype, methodName, override);
39+
uninstalls.push(() => Reflect.set(classType.prototype, methodName, method));
40+
}
41+
if (events[name]) {
42+
const emitClassType = (name === 'BrowserContext' ? api['ChromiumBrowserContext'] : undefined) || classType;
43+
for (const event of Object.values(events[name])) {
44+
if (typeof event !== 'symbol')
45+
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
46+
}
47+
const method = Reflect.get(emitClassType.prototype, 'emit');
48+
Reflect.set(emitClassType.prototype, 'emit', function(event, ...args) {
49+
if (typeof event !== 'symbol' && this.listenerCount(event))
50+
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
51+
return method.call(this, event, ...args);
52+
});
53+
uninstalls.push(() => Reflect.set(emitClassType.prototype, 'emit', method));
4554
}
46-
const method = Reflect.get(classType.prototype, 'emit');
47-
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
48-
if (typeof event !== 'symbol' && this.listenerCount(event))
49-
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
50-
return method.call(this, event, ...args);
51-
});
52-
uninstalls.push(() => Reflect.set(classType.prototype, 'emit', method));
5355
}
54-
5556
return () => uninstalls.forEach(u => u());
5657
}
5758

5859
/**
59-
* @param {string} browserName
60+
* @param {string} browserName
6061
*/
6162
function apiForBrowser(browserName) {
6263
const BROWSER_CONFIGS = [
@@ -72,7 +73,7 @@ function apiForBrowser(browserName) {
7273
name: 'Chromium',
7374
events: {
7475
...require('../../lib/events').Events,
75-
...require('../../lib/chromium/events').Events,
76+
ChromiumBrowserContext: require('../../lib/chromium/events').Events.CRBrowserContext,
7677
}
7778
},
7879
];
@@ -98,15 +99,12 @@ function apiForBrowser(browserName) {
9899
}
99100

100101
/**
101-
* @param {string} browserName
102+
* @param {string} browserName
102103
*/
103104
function installCoverageHooks(browserName) {
104-
const uninstalls = [];
105105
const {api, events} = apiForBrowser(browserName);
106106
const coverage = new Map();
107-
for (const [name, value] of Object.entries(api))
108-
uninstalls.push(traceAPICoverage(coverage, events, name, value));
109-
const uninstall = () => uninstalls.map(u => u());
107+
const uninstall = traceAPICoverage(coverage, api, events);
110108
return {coverage, uninstall};
111109
}
112110

0 commit comments

Comments
 (0)