Implement public API coverage

This patch:
- implements a basic public API coverage based on 'helper.tracePublicAPI' methods
- adds `npm run coverage` command which reports coverage after running all of the unit tests

References #50.
This commit is contained in:
JoelEinbinder
2017-07-27 16:16:37 -07:00
committed by Andrey Lushnikov
parent c26d2c8271
commit a2e0d27fb6
3 changed files with 44 additions and 4 deletions

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
/** @type {?Map<string, boolean>} */
let apiCoverage = null;
class Helper {
/**
* @param {function()|string} fun
@@ -116,16 +118,21 @@ class Helper {
let className = classType.prototype.constructor.name;
className = className.substring(0, 1).toLowerCase() + className.substring(1);
const debug = require('debug')(`puppeteer:${className}`);
if (!debug.enabled)
if (!debug.enabled && !apiCoverage)
return;
for (let 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;
if (apiCoverage)
apiCoverage.set(`${className}.${methodName}`, false);
Reflect.set(classType.prototype, methodName, function(...args) {
let argsText = args.map(stringifyArgument).join(', ');
let callsite = `${className}.${methodName}(${argsText})`;
debug(callsite);
if (debug.enabled)
debug(callsite);
if (apiCoverage)
apiCoverage.set(`${className}.${methodName}`, true);
return method.call(this, ...args);
});
}
@@ -163,6 +170,17 @@ class Helper {
listener.emitter.removeListener(listener.eventName, listener.handler);
listeners.splice(0, listeners.length);
}
/**
* @return {?Map<string, boolean>}
*/
static publicAPICoverage() {
return apiCoverage;
}
static recordPublicAPICoverage() {
apiCoverage = new Map();
}
}
module.exports = Helper;