From c947d0d585b9022e7cb5cb09f8994dc1517b2dbd Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 18 Sep 2018 17:09:08 +0530 Subject: [PATCH] [reporting] Added test duration reporting --- package.json | 5 ++++- sample/sample7.test.js | 2 +- src/lib/caller.js | 22 ++++++++++++++++++++-- src/lib/testrunner.js | 12 ++++++++++-- src/reporters/xunit.js | 9 +++++++-- src/util/perf_hooks.js | 2 ++ 6 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 src/util/perf_hooks.js diff --git a/package.json b/package.json index 20894e2..fb77c4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@klenty/gunner", - "version": "0.10.4", + "version": "0.10.6", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "main": "es6/index.js", "repository": { @@ -23,6 +23,9 @@ "bugs": { "url": "https://github.com/klenty/gunner/issues" }, + "browser": { + "perf_utils": "src/util/perf_hooks.js" + }, "homepage": "https://github.com/klenty/gunner#readme", "dependencies": { "@codefeathers/iseq": "^1.2.1", diff --git a/sample/sample7.test.js b/sample/sample7.test.js index c561477..4005d69 100644 --- a/sample/sample7.test.js +++ b/sample/sample7.test.js @@ -99,4 +99,4 @@ gunner.test('(should fail) should not resolve to 5', () => const trace = process.argv.slice(2).indexOf('--trace') !== -1; const reporter = process.argv.slice(2).indexOf('--log') !== -1; -gunner.run({ trace, reporter: 'xunit' }); +gunner.run({ trace, reporter }); diff --git a/src/lib/caller.js b/src/lib/caller.js index 012d9e3..de71a0f 100644 --- a/src/lib/caller.js +++ b/src/lib/caller.js @@ -1,11 +1,18 @@ +const { performance } = require('perf_hooks'); + const { isPromise } = require('../util'); const caller = (test, state) => { + const perf = { start: 0, end: 0 }; + let value, error, errored; try { + perf.start = performance.now(); value = test(state); + perf.end = performance.now(); } catch (e) { + perf.end = performance.now(); errored = true; error = e; } @@ -14,10 +21,21 @@ const caller = (test, state) => { if (promise) { return value - .then(res => ({ status: 'ok', resolve: res, promise: true })) - .catch(rej => ({ status: 'notOk', rejection: rej, promise: true })); + .then(res => ({ + duration: performance.now() - perf.start, + status: 'ok', + resolve: res, + promise: true + })) + .catch(rej => ({ + duration: performance.now() - perf.start, + status: 'notOk', + rejection: rej, + promise: true + })); } else { return Promise.resolve({ + duration: perf.end - perf.start, status: errored ? 'notOk' : 'ok', ...(!errored && { value }), ...(errored && { error }), diff --git a/src/lib/testrunner.js b/src/lib/testrunner.js index 9d506d1..f1f980a 100644 --- a/src/lib/testrunner.js +++ b/src/lib/testrunner.js @@ -3,6 +3,8 @@ const Gunner = require('../gunner'); Promise.object = require('@codefeathers/promise.object'); +const { performance } = require('perf_hooks'); + const { last, pipe, pick, assignToObject } = require('../util'); const buildTestQueue = require('./buildTestQueue'); @@ -50,12 +52,13 @@ const reduceQueue = }) .then(result => { - const { status } = result; + const { status, duration } = result; if (item.type === '@test') { const resultObject = { status, + duration, description: item.unit.description, ...((status === 'notOk' || status === 'skip') && {reason : result.error @@ -113,11 +116,16 @@ const reduceQueue = */ const testrunner = (instance) => { + const perf = { start: performance.now() }; + return Promise.object(pipe( buildTestQueue, reduceQueue, pick('results'), - )(instance)); + )(instance)).then(results => { + results.duration = performance.now() - perf.start; + return results; + }); }; diff --git a/src/reporters/xunit.js b/src/reporters/xunit.js index 4d58369..f8c35ee 100644 --- a/src/reporters/xunit.js +++ b/src/reporters/xunit.js @@ -31,7 +31,9 @@ const convert = results => { tests: count, success: success.length, failures: failures.length, - skipped: skipped.length + skipped: skipped.length, + timestamp: new Date().toUTCString(), + time: (results.duration / 1000) || 0, }, false, results.reduce((acc, r) => { @@ -44,7 +46,10 @@ const convert = results => { !r.reason, r.reason ? r.reason : '')); acc += tag( 'testcase', - { name: r.description }, + { + name: r.description, + time: (r.duration / 1000) || 0, + }, close, content || '' ); diff --git a/src/util/perf_hooks.js b/src/util/perf_hooks.js new file mode 100644 index 0000000..d6c08b1 --- /dev/null +++ b/src/util/perf_hooks.js @@ -0,0 +1,2 @@ +/* global performance */ +module.exports = { performance };