Skip to content

Commit 55d3613

Browse files
author
vdemedes
committed
display only failed hooks
1 parent fc201d2 commit 55d3613

File tree

6 files changed

+124
-8
lines changed

6 files changed

+124
-8
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ var isFailed = false;
1111

1212
Error.stackTraceLimit = Infinity;
1313

14-
function test(err, title, duration) {
14+
function test(err, title, duration, type) {
1515
if (isFailed) {
1616
return;
1717
}
1818

19+
// don't display anything, if it's a passed hook
20+
if (!err && type !== 'test') {
21+
return;
22+
}
23+
1924
process.send({
2025
name: 'test',
2126
data: {

lib/runner.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,37 @@ Runner.prototype.addSerialTest = function (title, cb) {
5353
};
5454

5555
Runner.prototype.addBeforeHook = function (title, cb) {
56-
this.tests.before.push(new Test(title, cb));
56+
var test = new Test(title, cb);
57+
test.type = 'hook';
58+
59+
this.tests.before.push(test);
5760
};
5861

5962
Runner.prototype.addAfterHook = function (title, cb) {
60-
this.tests.after.push(new Test(title, cb));
63+
var test = new Test(title, cb);
64+
test.type = 'hook';
65+
66+
this.tests.after.push(test);
6167
};
6268

6369
Runner.prototype.addBeforeEachHook = function (title, cb) {
70+
if (!cb) {
71+
cb = title;
72+
title = undefined;
73+
}
74+
6475
this.tests.beforeEach.push({
6576
title: title,
6677
fn: cb
6778
});
6879
};
6980

7081
Runner.prototype.addAfterEachHook = function (title, cb) {
82+
if (!cb) {
83+
cb = title;
84+
title = undefined;
85+
}
86+
7187
this.tests.afterEach.push({
7288
title: title,
7389
fn: cb
@@ -76,11 +92,19 @@ Runner.prototype.addAfterEachHook = function (title, cb) {
7692

7793
Runner.prototype._runTestWithHooks = function (test) {
7894
var beforeHooks = this.tests.beforeEach.map(function (hook) {
79-
return new Test(hook.title, hook.fn);
95+
var title = hook.title || 'beforeEach for "' + test.title + '"';
96+
hook = new Test(title, hook.fn);
97+
hook.type = 'eachHook';
98+
99+
return hook;
80100
});
81101

82102
var afterHooks = this.tests.afterEach.map(function (hook) {
83-
return new Test(hook.title, hook.fn);
103+
var title = hook.title || 'afterEach for "' + test.title + '"';
104+
hook = new Test(title, hook.fn);
105+
hook.type = 'eachHook';
106+
107+
return hook;
84108
});
85109

86110
var tests = [];
@@ -137,10 +161,11 @@ Runner.prototype._addTestResult = function (test) {
137161
this.results.push({
138162
duration: test.duration,
139163
title: test.title,
140-
error: test.assertError
164+
error: test.assertError,
165+
type: test.type
141166
});
142167

143-
this.emit('test', test.assertError, test.title, test.duration);
168+
this.emit('test', test.assertError, test.title, test.duration, test.type);
144169
};
145170

146171
Runner.prototype.run = function () {

lib/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Test(title, fn) {
1313
return new Test(title, fn);
1414
}
1515

16-
if (typeof title !== 'string') {
16+
if (typeof title === 'function') {
1717
fn = title;
1818
title = null;
1919
}
@@ -27,6 +27,9 @@ function Test(title, fn) {
2727
this.duration = null;
2828
this.context = {};
2929

30+
// test type, can be: test, hook, eachHook
31+
this.type = 'test';
32+
3033
// store the time point before test execution
3134
// to calculate the total time spent in test
3235
this._timeStart = null;

test/fixture/hooks-failing.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const test = require('../../');
2+
3+
test.beforeEach(fail);
4+
test(pass);
5+
6+
function pass(t) {
7+
t.end();
8+
}
9+
10+
function fail(t) {
11+
t.fail();
12+
t.end();
13+
}

test/fixture/hooks-passing.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const test = require('../../');
2+
3+
test.before(pass);
4+
test.beforeEach(pass);
5+
test.after(pass);
6+
test.afterEach(pass);
7+
test(pass);
8+
9+
function pass(t) {
10+
t.end();
11+
}

test/test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Promise = require('bluebird');
55
var figures = require('figures');
66
var test = require('tape');
77
var Runner = require('../lib/runner');
8+
var fork = require('../lib/fork');
89
var ava = require('../lib/test');
910

1011
function execCli(args, cb) {
@@ -824,6 +825,38 @@ test('hooks - shared context of any type', function (t) {
824825
});
825826
});
826827

828+
test('test types and titles', function (t) {
829+
t.plan(10);
830+
831+
var runner = new Runner();
832+
runner.addBeforeHook(pass);
833+
runner.addBeforeEachHook(pass);
834+
runner.addAfterHook(pass);
835+
runner.addAfterEachHook(pass);
836+
runner.addTest('test', pass);
837+
838+
function pass(a) {
839+
a.end();
840+
}
841+
842+
var tests = [
843+
{type: 'hook', title: 'pass'},
844+
{type: 'eachHook', title: 'beforeEach for "test"'},
845+
{type: 'test', title: 'test'},
846+
{type: 'eachHook', title: 'afterEach for "test"'},
847+
{type: 'hook', title: 'pass'}
848+
];
849+
850+
runner.on('test', function (err, title, duration, type) {
851+
var test = tests.shift();
852+
853+
t.is(test.title, title);
854+
t.is(test.type, type);
855+
});
856+
857+
runner.run().then(t.end);
858+
});
859+
827860
test('ES2015 support', function (t) {
828861
t.plan(1);
829862

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

954+
test('don\'t display hook title if it did not fail', function (t) {
955+
t.plan(2);
956+
957+
fork(path.join(__dirname, 'fixture', 'hooks-passing.js'))
958+
.on('test', function (test) {
959+
t.deepEqual(test.err, {});
960+
t.is(test.title, 'pass');
961+
})
962+
.then(function () {
963+
t.end();
964+
});
965+
});
966+
967+
test('display hook title if it failed', function (t) {
968+
t.plan(2);
969+
970+
fork(path.join(__dirname, 'fixture', 'hooks-failing.js'))
971+
.on('test', function (test) {
972+
t.is(test.err.name, 'AssertionError');
973+
t.is(test.title, 'beforeEach for "pass"');
974+
})
975+
.then(function () {
976+
t.end();
977+
});
978+
});
979+
921980
test('fail-fast mode', function (t) {
922981
t.plan(5);
923982

0 commit comments

Comments
 (0)