Skip to content

Commit 07c6b30

Browse files
vdemedessindresorhus
vdemedes
authored andcommitted
Close #30 PR: Output test execution time. Fixes #14
1 parent 6d2baef commit 07c6b30

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
var prettyMs = require('pretty-ms');
23
var chalk = require('chalk');
34
var figures = require('figures');
45
var Squeak = require('squeak');
@@ -17,13 +18,27 @@ log.type('error', {
1718
prefix: figures.cross
1819
});
1920

20-
function test(err, title) {
21+
function test(err, title, duration) {
2122
if (err) {
2223
log.error(title, chalk.red(err.message));
2324
return;
2425
}
2526

26-
log.success(title);
27+
// format a time spent in the test
28+
var timeSpent = '';
29+
30+
// display duration only over a threshold
31+
var threshold = 100;
32+
33+
if (duration > threshold) {
34+
var formattedDuration = prettyMs(duration, {
35+
secDecimalDigits: 2
36+
});
37+
38+
timeSpent = chalk.gray.dim(' (' + formattedDuration + ')');
39+
}
40+
41+
log.success(title + timeSpent);
2742
}
2843

2944
function stack(results) {

lib/runner.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,37 @@ Runner.prototype.addSerialTest = function (title, cb) {
3636

3737
Runner.prototype.concurrent = function (cb) {
3838
each(this.tests.concurrent, function (test, i, next) {
39-
test.run(function (err) {
39+
test.run(function (err, duration) {
4040
if (err) {
4141
this.stats.failCount++;
4242
}
4343

4444
this.results.push({
45+
duration: duration,
4546
title: test.title,
4647
error: err
4748
});
4849

49-
this.emit('test', err, test.title);
50+
this.emit('test', err, test.title, duration);
5051
next();
5152
}.bind(this));
5253
}.bind(this), cb);
5354
};
5455

5556
Runner.prototype.serial = function (cb) {
5657
eachSerial(this.tests.serial, function (test, next) {
57-
test.run(function (err) {
58+
test.run(function (err, duration) {
5859
if (err) {
5960
this.stats.failCount++;
6061
}
6162

6263
this.results.push({
64+
duration: duration,
6365
title: test.title,
6466
error: err
6567
});
6668

67-
this.emit('test', err, test.title);
69+
this.emit('test', err, test.title, duration);
6870
next();
6971
}.bind(this));
7072
}.bind(this), cb);

lib/test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function Test(title, fn) {
2121
this.fn = fn;
2222
this.assertCount = 0;
2323
this.planCount = null;
24+
this.duration = null;
25+
26+
// store the time point
27+
// before test execution
28+
// to calculate the total time spent in test
29+
this._timeStart = null;
2430
}
2531

2632
util.inherits(Test, EventEmitter);
@@ -67,6 +73,8 @@ Test.prototype.run = function (cb) {
6773
this.exit();
6874
}
6975

76+
this._timeStart = Date.now();
77+
7078
try {
7179
var ret = this.fn(this);
7280

@@ -98,6 +106,9 @@ Test.prototype.end = function () {
98106
};
99107

100108
Test.prototype.exit = function () {
109+
// calculate total time spent in test
110+
this.duration = Date.now() - this._timeStart;
111+
101112
if (this.planCount !== null && this.planCount !== this.assertCount) {
102113
this.assertError = new assert.AssertionError({
103114
actual: this.assertCount,
@@ -113,7 +124,7 @@ Test.prototype.exit = function () {
113124
this.ended = true;
114125

115126
setImmediate(function () {
116-
this.cb(this.assertError);
127+
this.cb(this.assertError, this.duration);
117128
}.bind(this));
118129
}
119130
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"globby": "^2.0.0",
5555
"meow": "^3.3.0",
5656
"plur": "^2.0.0",
57+
"pretty-ms": "^2.0.0",
5758
"squeak": "^1.2.0",
5859
"update-notifier": "^0.5.0"
5960
},

test/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,21 @@ test('promise support - reject', function (t) {
343343
t.end();
344344
});
345345
});
346+
347+
test('record test duration', function (t) {
348+
var avaTest;
349+
350+
ava(function (a) {
351+
avaTest = a;
352+
353+
a.plan(1);
354+
355+
setTimeout(function () {
356+
a.true(true);
357+
}, 1234);
358+
}).run(function (err) {
359+
t.false(err);
360+
t.true(avaTest.duration >= 1234);
361+
t.end();
362+
});
363+
});

0 commit comments

Comments
 (0)