Skip to content

Output test execution time #30

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 11 commits into from
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var hrtime = require('pretty-hrtime');
var chalk = require('chalk');
var figures = require('figures');
var Squeak = require('squeak');
Expand All @@ -17,13 +18,13 @@ log.type('error', {
prefix: figures.cross
});

function test(err, title) {
function test(err, title, duration) {
if (err) {
log.error(title, chalk.red(err.message));
return;
}

log.success(title);
log.success(title + ' (' + hrtime(duration) + ')');
Copy link
Member

Choose a reason for hiding this comment

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

log.success(title + ' ' + chalk.dim('(' + hrtime(duration) + ')'));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed an update.

}

function stack(results) {
Expand Down
10 changes: 6 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,37 @@ Runner.prototype.addSerialTest = function (title, cb) {

Runner.prototype.concurrent = function (cb) {
each(this.tests.concurrent, function (test, i, next) {
test.run(function (err) {
test.run(function (err, duration) {
if (err) {
this.stats.failCount++;
}

this.results.push({
duration: duration,
title: test.title,
error: err
});

this.emit('test', err, test.title);
this.emit('test', err, test.title, duration);
next();
}.bind(this));
}.bind(this), cb);
};

Runner.prototype.serial = function (cb) {
eachSerial(this.tests.serial, function (test, next) {
test.run(function (err) {
test.run(function (err, duration) {
if (err) {
this.stats.failCount++;
}

this.results.push({
duration: duration,
title: test.title,
error: err
});

this.emit('test', err, test.title);
this.emit('test', err, test.title, duration);
next();
}.bind(this));
}.bind(this), cb);
Expand Down
13 changes: 12 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function Test(title, fn) {
this.fn = fn;
this.assertCount = 0;
this.planCount = null;

// store the time point
// before test execution
// to pass it to process.hrtime()
// and calculate the total time spent in test
this._timeStart = 0;
}

util.inherits(Test, EventEmitter);
Expand Down Expand Up @@ -67,6 +73,8 @@ Test.prototype.run = function (cb) {
this.exit();
}

this._timeStart = process.hrtime();

try {
var ret = this.fn(this);

Expand Down Expand Up @@ -98,6 +106,9 @@ Test.prototype.end = function () {
};

Test.prototype.exit = function () {
// calculate total time spent in test
var duration = process.hrtime(this._timeStart);

if (this.planCount !== null && this.planCount !== this.assertCount) {
this.assertError = new assert.AssertionError({
actual: this.assertCount,
Expand All @@ -113,7 +124,7 @@ Test.prototype.exit = function () {
this.ended = true;

setImmediate(function () {
this.cb(this.assertError);
this.cb(this.assertError, duration);
}.bind(this));
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"globby": "^2.0.0",
"meow": "^3.3.0",
"plur": "^2.0.0",
"pretty-hrtime": "^1.0.0",
"squeak": "^1.2.0",
"update-notifier": "^0.5.0"
},
Expand Down