Skip to content

Commit

Permalink
Merge pull request #190 from sindresorhus/hook-titles
Browse files Browse the repository at this point in the history
Display only failed hooks
  • Loading branch information
sindresorhus committed Nov 11, 2015
2 parents fc201d2 + 55d3613 commit 3a75cb1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ var isFailed = false;

Error.stackTraceLimit = Infinity;

function test(err, title, duration) {
function test(err, title, duration, type) {
if (isFailed) {
return;
}

// don't display anything, if it's a passed hook
if (!err && type !== 'test') {
return;
}

process.send({
name: 'test',
data: {
Expand Down
37 changes: 31 additions & 6 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,37 @@ Runner.prototype.addSerialTest = function (title, cb) {
};

Runner.prototype.addBeforeHook = function (title, cb) {
this.tests.before.push(new Test(title, cb));
var test = new Test(title, cb);
test.type = 'hook';

this.tests.before.push(test);
};

Runner.prototype.addAfterHook = function (title, cb) {
this.tests.after.push(new Test(title, cb));
var test = new Test(title, cb);
test.type = 'hook';

this.tests.after.push(test);
};

Runner.prototype.addBeforeEachHook = function (title, cb) {
if (!cb) {
cb = title;
title = undefined;
}

this.tests.beforeEach.push({
title: title,
fn: cb
});
};

Runner.prototype.addAfterEachHook = function (title, cb) {
if (!cb) {
cb = title;
title = undefined;
}

this.tests.afterEach.push({
title: title,
fn: cb
Expand All @@ -76,11 +92,19 @@ Runner.prototype.addAfterEachHook = function (title, cb) {

Runner.prototype._runTestWithHooks = function (test) {
var beforeHooks = this.tests.beforeEach.map(function (hook) {
return new Test(hook.title, hook.fn);
var title = hook.title || 'beforeEach for "' + test.title + '"';
hook = new Test(title, hook.fn);
hook.type = 'eachHook';

return hook;
});

var afterHooks = this.tests.afterEach.map(function (hook) {
return new Test(hook.title, hook.fn);
var title = hook.title || 'afterEach for "' + test.title + '"';
hook = new Test(title, hook.fn);
hook.type = 'eachHook';

return hook;
});

var tests = [];
Expand Down Expand Up @@ -137,10 +161,11 @@ Runner.prototype._addTestResult = function (test) {
this.results.push({
duration: test.duration,
title: test.title,
error: test.assertError
error: test.assertError,
type: test.type
});

this.emit('test', test.assertError, test.title, test.duration);
this.emit('test', test.assertError, test.title, test.duration, test.type);
};

Runner.prototype.run = function () {
Expand Down
5 changes: 4 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Test(title, fn) {
return new Test(title, fn);
}

if (typeof title !== 'string') {
if (typeof title === 'function') {
fn = title;
title = null;
}
Expand All @@ -27,6 +27,9 @@ function Test(title, fn) {
this.duration = null;
this.context = {};

// test type, can be: test, hook, eachHook
this.type = 'test';

// store the time point before test execution
// to calculate the total time spent in test
this._timeStart = null;
Expand Down
13 changes: 13 additions & 0 deletions test/fixture/hooks-failing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const test = require('../../');

test.beforeEach(fail);
test(pass);

function pass(t) {
t.end();
}

function fail(t) {
t.fail();
t.end();
}
11 changes: 11 additions & 0 deletions test/fixture/hooks-passing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const test = require('../../');

test.before(pass);
test.beforeEach(pass);
test.after(pass);
test.afterEach(pass);
test(pass);

function pass(t) {
t.end();
}
59 changes: 59 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Promise = require('bluebird');
var figures = require('figures');
var test = require('tape');
var Runner = require('../lib/runner');
var fork = require('../lib/fork');
var ava = require('../lib/test');

function execCli(args, cb) {
Expand Down Expand Up @@ -824,6 +825,38 @@ test('hooks - shared context of any type', function (t) {
});
});

test('test types and titles', function (t) {
t.plan(10);

var runner = new Runner();
runner.addBeforeHook(pass);
runner.addBeforeEachHook(pass);
runner.addAfterHook(pass);
runner.addAfterEachHook(pass);
runner.addTest('test', pass);

function pass(a) {
a.end();
}

var tests = [
{type: 'hook', title: 'pass'},
{type: 'eachHook', title: 'beforeEach for "test"'},
{type: 'test', title: 'test'},
{type: 'eachHook', title: 'afterEach for "test"'},
{type: 'hook', title: 'pass'}
];

runner.on('test', function (err, title, duration, type) {
var test = tests.shift();

t.is(test.title, title);
t.is(test.type, type);
});

runner.run().then(t.end);
});

test('ES2015 support', function (t) {
t.plan(1);

Expand Down Expand Up @@ -918,6 +951,32 @@ test('don\'t display test title, if there is only one anonymous test', function
});
});

test('don\'t display hook title if it did not fail', function (t) {
t.plan(2);

fork(path.join(__dirname, 'fixture', 'hooks-passing.js'))
.on('test', function (test) {
t.deepEqual(test.err, {});
t.is(test.title, 'pass');
})
.then(function () {
t.end();
});
});

test('display hook title if it failed', function (t) {
t.plan(2);

fork(path.join(__dirname, 'fixture', 'hooks-failing.js'))
.on('test', function (test) {
t.is(test.err.name, 'AssertionError');
t.is(test.title, 'beforeEach for "pass"');
})
.then(function () {
t.end();
});
});

test('fail-fast mode', function (t) {
t.plan(5);

Expand Down

0 comments on commit 3a75cb1

Please sign in to comment.