diff --git a/lib/helper.js b/lib/helper.js index 844e4e8a..8e49885e 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -137,13 +137,35 @@ class Helper { }); } + if (classType.Events) { + if (apiCoverage) { + for (let event of Object.values(classType.Events)) + apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false); + } + const method = Reflect.get(classType.prototype, 'emit'); + Reflect.set(classType.prototype, 'emit', function(event, ...args) { + let argsText = [JSON.stringify(event)].concat(args.map(stringifyArgument)).join(', '); + if (debug.enabled) + debug(`${className}.emit(${argsText})`); + if (apiCoverage && this.listenerCount(event)) + apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true); + return method.call(this, event, ...args); + }); + } + /** * @param {!Object} arg * @return {string} */ function stringifyArgument(arg) { - if (typeof arg !== 'function') - return JSON.stringify(arg); + if (typeof arg !== 'function') { + try { + return JSON.stringify(arg); + } catch (e) { + // The object was recursive + return arg.toString(); + } + } let text = arg.toString().split('\n').map(line => line.trim()).join(''); if (text.length > 20) text = text.substring(0, 20) + '…'; diff --git a/test/test.js b/test/test.js index e7041e80..627c4b08 100644 --- a/test/test.js +++ b/test/test.js @@ -1423,7 +1423,7 @@ if (process.env.COVERAGE) { } for (let method of coverage.keys()) { - (disabled.has(method) ? xit : it)(`public method '${method}' should be called`, SX(async function(){ + (disabled.has(method) ? xit : it)(`public api '${method}' should be called`, SX(async function(){ expect(coverage.get(method)).toBe(true); })); }