Skip to content

Add test run duration #1038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ logger.start();

api.on('test-run', function (runStatus) {
reporter.api = runStatus;
runStatus.testRunStart = Date.now();
runStatus.on('test', logger.test);
runStatus.on('error', logger.unhandledError);

Expand All @@ -183,6 +184,7 @@ if (cli.flags.watch) {
} else {
api.run(files)
.then(function (runStatus) {
runStatus.testRunElapsed = runStatus.testRunStart && Date.now() - runStatus.testRunStart;
logger.finish(runStatus);
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
})
Expand Down
32 changes: 32 additions & 0 deletions lib/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var second = 1000;
var minute = 60 * second;
var hour = 60 * minute;
var day = 24 * hour;

function round(value) {
return Number(Math.round(value * 1000) / 1000);
}

function duration(ms) {
if (ms >= day) {
return round(ms / day) + 'd';
}

if (ms >= hour) {
return round(ms / hour) + 'h';
}

if (ms >= minute) {
return round(ms / minute) + 'm';
}

if (ms >= second) {
return round(ms / second) + 's';
}

return round(ms) + 'ms';
}

module.exports = duration;
Copy link
Member

@sindresorhus sindresorhus Oct 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be pretty-ms instead.

15 changes: 11 additions & 4 deletions lib/reporters/mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var cross = require('figures').cross;
var repeating = require('repeating');
var objectAssign = require('object-assign');
var colors = require('../colors');
var duration = require('../duration');

chalk.enabled = true;
Object.keys(colors).forEach(function (key) {
Expand Down Expand Up @@ -121,7 +122,7 @@ MiniReporter.prototype.unhandledError = function (err) {
}
};

MiniReporter.prototype.reportCounts = function (time) {
MiniReporter.prototype.reportCounts = function (time, testRunElapsed) {
var lines = [
this.passCount > 0 ? '\n ' + colors.pass(this.passCount, 'passed') : '',
this.knownFailureCount > 0 ? '\n ' + colors.error(this.knownFailureCount, plur('known failure', this.knownFailureCount)) : '',
Expand All @@ -130,8 +131,14 @@ MiniReporter.prototype.reportCounts = function (time) {
this.todoCount > 0 ? '\n ' + colors.todo(this.todoCount, 'todo') : ''
].filter(Boolean);

if (time && lines.length > 0) {
lines[0] += ' ' + time;
if (lines.length > 0) {
if (time) {
lines[0] += ' ' + time;
}

if (testRunElapsed) {
lines[0] += ' (' + duration(testRunElapsed) + ')';
}
}

return lines.join('');
Expand All @@ -145,7 +152,7 @@ MiniReporter.prototype.finish = function (runStatus) {
time = chalk.gray.dim('[' + new Date().toLocaleTimeString('en-US', {hour12: false}) + ']');
}

var status = this.reportCounts(time);
var status = this.reportCounts(time, runStatus.testRunElapsed);

if (this.rejectionCount > 0) {
status += '\n ' + colors.error(this.rejectionCount, plur('rejection', this.rejectionCount));
Expand Down
2 changes: 2 additions & 0 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ function Watcher(logger, api, files, sources) {
this.busy = api.run(specificFiles || files, {
runOnlyExclusive: runOnlyExclusive
}).then(function (runStatus) {
runStatus.testRunElapsed = runStatus.testRunStart && Date.now() - runStatus.testRunStart;

runStatus.previousFailCount = self.sumPreviousFailures(currentVector);
logger.finish(runStatus);

Expand Down
52 changes: 52 additions & 0 deletions test/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var test = require('tap').test;
var duration = require('../lib/duration');

test('displays milliseconds', function (t) {
t.is(duration(0), '0ms');
t.is(duration(1), '1ms');
t.is(duration(500), '500ms');
t.is(duration(999), '999ms');
t.end();
});

test('calculates seconds', function (t) {
var second = 1000;

t.is(duration(second), '1s');
t.is(duration(second + 49), '1.049s');
t.is(duration((second * 6) - 1), '5.999s');
t.is(duration((second * 60) - 1), '59.999s');
t.end();
});

test('calculates minutes', function (t) {
var minute = 1000 * 60;

t.is(duration(minute), '1m');
t.is(duration(minute * 5 / 3), '1.667m');
t.is(duration(minute * 59.5), '59.5m');
t.is(duration((minute * 60) - 1), '60m');
t.end();
});

test('calculates hours', function (t) {
var hour = 1000 * 60 * 60;

t.is(duration(hour), '1h');
t.is(duration(hour * 5 / 3), '1.667h');
t.is(duration(hour * 23.5), '23.5h');
t.is(duration((hour * 24) - 1), '24h');
t.end();
});

test('calculates days', function (t) {
var day = 1000 * 60 * 60 * 24;

t.is(duration(day), '1d');
t.is(duration(day * 5 / 3), '1.667d');
t.is(duration(day * 364.5), '364.5d');
t.is(duration(day * 500), '500d');
t.end();
});
50 changes: 50 additions & 0 deletions test/reporters/mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,53 @@ test('stderr and stdout should call _update', function (t) {
reporter._update.restore();
t.end();
});

test('shows cumulative test run duration on first line of output', function (t) {
var clock = lolex.install(0);
var reporter = miniReporter();

reporter.failCount = 1;
reporter.skipCount = 1;

clock.tick('07');

var actualOutput = reporter.finish({
errors: [{}, {}],
testRunElapsed: Date.now()
});
var expectedOutput = [
'\n ' + colors.error('1 failed') + ' (7s)',
' ' + colors.skip('1 skipped'),
'\n'
].join('\n');

t.is(actualOutput, expectedOutput);
t.end();
});

test('shows cumulative test run duration during watch', function (t) {
var startDate = new Date(2014, 11, 19, 17, 12, 5, 200);
var clock = lolex.install(startDate.getTime());
var time = ' ' + chalk.grey.dim('[17:19:12]');
var reporter = _miniReporter({watching: true});

t.is(reporter.start(), ' \n ' + graySpinner + ' ');
reporter.clearInterval();

clock.tick('07:07');

reporter.passCount = 1;
reporter.todoCount = 1;

var actualOutput = reporter.finish({
testRunElapsed: Date.now() - startDate
});
var expectedOutput = [
'\n ' + colors.pass('1 passed') + time + ' (7.117m)',
' ' + colors.todo('1 todo'),
'\n'
].join('\n');

t.is(actualOutput, expectedOutput);
t.end();
});