Browse Source

[reporting] Added test duration reporting

master
Muthu Kumar 6 years ago
parent
commit
c947d0d585
  1. 5
      package.json
  2. 2
      sample/sample7.test.js
  3. 22
      src/lib/caller.js
  4. 12
      src/lib/testrunner.js
  5. 9
      src/reporters/xunit.js
  6. 2
      src/util/perf_hooks.js

5
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",

2
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 });

22
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 }),

12
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;
});
};

9
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 || ''
);

2
src/util/perf_hooks.js

@ -0,0 +1,2 @@
/* global performance */
module.exports = { performance };
Loading…
Cancel
Save