Skip to content

Commit 0ff2e6a

Browse files
authored
test: move api coverage to a spec file (#1703)
1 parent af01d15 commit 0ff2e6a

File tree

3 files changed

+106
-92
lines changed

3 files changed

+106
-92
lines changed

test/apicoverage.spec.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
* Modifications copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @param {Map<string, boolean>} apiCoverage
20+
* @param {Object} events
21+
* @param {string} className
22+
* @param {!Object} classType
23+
*/
24+
function traceAPICoverage(apiCoverage, events, className, classType) {
25+
className = className.substring(0, 1).toLowerCase() + className.substring(1);
26+
for (const methodName of Reflect.ownKeys(classType.prototype)) {
27+
const method = Reflect.get(classType.prototype, methodName);
28+
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
29+
continue;
30+
apiCoverage.set(`${className}.${methodName}`, false);
31+
Reflect.set(classType.prototype, methodName, function(...args) {
32+
apiCoverage.set(`${className}.${methodName}`, true);
33+
return method.call(this, ...args);
34+
});
35+
}
36+
37+
if (events[classType.name]) {
38+
for (const event of Object.values(events[classType.name])) {
39+
if (typeof event !== 'symbol')
40+
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
41+
}
42+
const method = Reflect.get(classType.prototype, 'emit');
43+
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
44+
if (typeof event !== 'symbol' && this.listenerCount(event))
45+
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
46+
return method.call(this, event, ...args);
47+
});
48+
}
49+
}
50+
51+
module.exports.describe = function({browserType}) {
52+
describe('**API COVERAGE**', () => {
53+
const BROWSER_CONFIGS = [
54+
{
55+
name: 'Firefox',
56+
events: require('../lib/events').Events,
57+
missingCoverage: ['browserContext.setGeolocation', 'browserContext.setOffline', 'cDPSession.send', 'cDPSession.detach'],
58+
},
59+
{
60+
name: 'WebKit',
61+
events: require('../lib/events').Events,
62+
missingCoverage: ['browserContext.clearPermissions', 'cDPSession.send', 'cDPSession.detach'],
63+
},
64+
{
65+
name: 'Chromium',
66+
events: {
67+
...require('../lib/events').Events,
68+
...require('../lib/chromium/events').Events,
69+
},
70+
missingCoverage: [],
71+
},
72+
];
73+
const browserConfig = BROWSER_CONFIGS.find(config => config.name.toLowerCase() === browserType.name());
74+
const events = browserConfig.events;
75+
const api = require('../lib/api');
76+
77+
const coverage = new Map();
78+
Object.keys(api).forEach(apiName => {
79+
if (BROWSER_CONFIGS.some(config => apiName.startsWith(config.name)) && !apiName.startsWith(browserConfig.name))
80+
return;
81+
traceAPICoverage(coverage, events, apiName, api[apiName]);
82+
});
83+
84+
it('should call all API methods', () => {
85+
const ignoredMethods = new Set(browserConfig.missingCoverage);
86+
const missingMethods = [];
87+
const extraIgnoredMethods = [];
88+
for (const method of coverage.keys()) {
89+
// Sometimes we already have a background page while launching, before adding a listener.
90+
if (method === 'chromiumBrowserContext.emit("backgroundpage")')
91+
continue;
92+
if (!coverage.get(method) && !ignoredMethods.has(method))
93+
missingMethods.push(method);
94+
else if (coverage.get(method) && ignoredMethods.has(method))
95+
extraIgnoredMethods.push(method);
96+
}
97+
if (extraIgnoredMethods.length)
98+
throw new Error('Certain API Methods are called and should not be ignored: ' + extraIgnoredMethods.join(', '));
99+
if (missingMethods.length)
100+
throw new Error('Certain API Methods are not called: ' + missingMethods.join(', '));
101+
});
102+
});
103+
};

test/playwright.spec.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -242,39 +242,8 @@ module.exports.addPlaywrightTests = ({platform, products, playwrightPath, headle
242242
loadTests('./chromium/tracing.spec.js');
243243
});
244244

245-
if (coverage) {
246-
const BROWSER_CONFIGS = [
247-
{
248-
name: 'Firefox',
249-
events: require('../lib/events').Events,
250-
missingCoverage: ['browserContext.setGeolocation', 'browserContext.setOffline', 'cDPSession.send', 'cDPSession.detach'],
251-
},
252-
{
253-
name: 'WebKit',
254-
events: require('../lib/events').Events,
255-
missingCoverage: ['browserContext.clearPermissions', 'cDPSession.send', 'cDPSession.detach'],
256-
},
257-
{
258-
name: 'Chromium',
259-
events: {
260-
...require('../lib/events').Events,
261-
...require('../lib/chromium/events').Events,
262-
},
263-
// Sometimes we already have a background page while launching, before adding a listener.
264-
missingCoverage: ['chromiumBrowserContext.emit("backgroundpage")'],
265-
},
266-
];
267-
const browserNames = BROWSER_CONFIGS.map(config => config.name);
268-
const browserConfig = BROWSER_CONFIGS.find(config => config.name === product);
269-
const api = require('../lib/api');
270-
const filteredApi = {};
271-
Object.keys(api).forEach(apiName => {
272-
if (browserNames.some(browserName => apiName.startsWith(browserName)) && !apiName.startsWith(product))
273-
return;
274-
filteredApi[apiName] = api[apiName];
275-
});
276-
require('./utils').recordAPICoverage(filteredApi, browserConfig.events, browserConfig.missingCoverage);
277-
}
245+
if (coverage)
246+
loadTests('./apicoverage.spec.js');
278247
});
279248
}
280249
};

test/utils.js

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,65 +28,7 @@ const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ?
2828
const mkdtempAsync = util.promisify(require('fs').mkdtemp);
2929
const removeFolderAsync = util.promisify(removeFolder);
3030

31-
const COVERAGE_TESTSUITE_NAME = '**API COVERAGE**';
32-
33-
/**
34-
* @param {Map<string, boolean>} apiCoverage
35-
* @param {Object} events
36-
* @param {string} className
37-
* @param {!Object} classType
38-
*/
39-
function traceAPICoverage(apiCoverage, events, className, classType) {
40-
className = className.substring(0, 1).toLowerCase() + className.substring(1);
41-
for (const methodName of Reflect.ownKeys(classType.prototype)) {
42-
const method = Reflect.get(classType.prototype, methodName);
43-
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
44-
continue;
45-
apiCoverage.set(`${className}.${methodName}`, false);
46-
Reflect.set(classType.prototype, methodName, function(...args) {
47-
apiCoverage.set(`${className}.${methodName}`, true);
48-
return method.call(this, ...args);
49-
});
50-
}
51-
52-
if (events[classType.name]) {
53-
for (const event of Object.values(events[classType.name])) {
54-
if (typeof event !== 'symbol')
55-
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
56-
}
57-
const method = Reflect.get(classType.prototype, 'emit');
58-
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
59-
if (typeof event !== 'symbol' && this.listenerCount(event))
60-
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
61-
return method.call(this, event, ...args);
62-
});
63-
}
64-
}
65-
6631
const utils = module.exports = {
67-
recordAPICoverage: function(api, events, ignoredMethodsArray = []) {
68-
const coverage = new Map();
69-
const ignoredMethods = new Set(ignoredMethodsArray);
70-
for (const [className, classType] of Object.entries(api))
71-
traceAPICoverage(coverage, events, className, classType);
72-
describe(COVERAGE_TESTSUITE_NAME, () => {
73-
it('should call all API methods', () => {
74-
const missingMethods = [];
75-
const extraIgnoredMethods = [];
76-
for (const method of coverage.keys()) {
77-
if (!coverage.get(method) && !ignoredMethods.has(method))
78-
missingMethods.push(method);
79-
else if (coverage.get(method) && ignoredMethods.has(method))
80-
extraIgnoredMethods.push(method);
81-
}
82-
if (extraIgnoredMethods.length)
83-
throw new Error('Certain API Methods are called and should not be ignored: ' + extraIgnoredMethods.join(', '));
84-
if (missingMethods.length)
85-
throw new Error('Certain API Methods are not called: ' + missingMethods.join(', '));
86-
});
87-
});
88-
},
89-
9032
/**
9133
* @return {string}
9234
*/
@@ -186,7 +128,7 @@ const utils = module.exports = {
186128
testRunner.on('testfinished', test => {
187129
// Do not report tests from COVERAGE testsuite.
188130
// They don't bring much value to us.
189-
if (test.fullName.includes(COVERAGE_TESTSUITE_NAME))
131+
if (test.fullName.includes('**API COVERAGE**'))
190132
return;
191133
const testpath = test.location.filePath.substring(utils.projectRoot().length);
192134
const url = `https://github.com/Microsoft/playwright/blob/${sha}/${testpath}#L${test.location.lineNumber}`;

0 commit comments

Comments
 (0)