Browse Source

display only failed hooks

babel-plugin-for-integration-tests
vdemedes 9 years ago
parent
commit
55d3613bed
  1. 7
      index.js
  2. 37
      lib/runner.js
  3. 5
      lib/test.js
  4. 13
      test/fixture/hooks-failing.js
  5. 11
      test/fixture/hooks-passing.js
  6. 59
      test/test.js

7
index.js

@ -11,11 +11,16 @@ var isFailed = false;
Error.stackTraceLimit = Infinity; Error.stackTraceLimit = Infinity;
function test(err, title, duration) { function test(err, title, duration, type) {
if (isFailed) { if (isFailed) {
return; return;
} }
// don't display anything, if it's a passed hook
if (!err && type !== 'test') {
return;
}
process.send({ process.send({
name: 'test', name: 'test',
data: { data: {

37
lib/runner.js

@ -53,14 +53,25 @@ Runner.prototype.addSerialTest = function (title, cb) {
}; };
Runner.prototype.addBeforeHook = 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) { 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) { Runner.prototype.addBeforeEachHook = function (title, cb) {
if (!cb) {
cb = title;
title = undefined;
}
this.tests.beforeEach.push({ this.tests.beforeEach.push({
title: title, title: title,
fn: cb fn: cb
@ -68,6 +79,11 @@ Runner.prototype.addBeforeEachHook = function (title, cb) {
}; };
Runner.prototype.addAfterEachHook = function (title, cb) { Runner.prototype.addAfterEachHook = function (title, cb) {
if (!cb) {
cb = title;
title = undefined;
}
this.tests.afterEach.push({ this.tests.afterEach.push({
title: title, title: title,
fn: cb fn: cb
@ -76,11 +92,19 @@ Runner.prototype.addAfterEachHook = function (title, cb) {
Runner.prototype._runTestWithHooks = function (test) { Runner.prototype._runTestWithHooks = function (test) {
var beforeHooks = this.tests.beforeEach.map(function (hook) { 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) { 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 = []; var tests = [];
@ -137,10 +161,11 @@ Runner.prototype._addTestResult = function (test) {
this.results.push({ this.results.push({
duration: test.duration, duration: test.duration,
title: test.title, 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 () { Runner.prototype.run = function () {

5
lib/test.js

@ -13,7 +13,7 @@ function Test(title, fn) {
return new Test(title, fn); return new Test(title, fn);
} }
if (typeof title !== 'string') { if (typeof title === 'function') {
fn = title; fn = title;
title = null; title = null;
} }
@ -27,6 +27,9 @@ function Test(title, fn) {
this.duration = null; this.duration = null;
this.context = {}; this.context = {};
// test type, can be: test, hook, eachHook
this.type = 'test';
// store the time point before test execution // store the time point before test execution
// to calculate the total time spent in test // to calculate the total time spent in test
this._timeStart = null; this._timeStart = null;

13
test/fixture/hooks-failing.js

@ -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
test/fixture/hooks-passing.js

@ -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
test/test.js

@ -5,6 +5,7 @@ var Promise = require('bluebird');
var figures = require('figures'); var figures = require('figures');
var test = require('tape'); var test = require('tape');
var Runner = require('../lib/runner'); var Runner = require('../lib/runner');
var fork = require('../lib/fork');
var ava = require('../lib/test'); var ava = require('../lib/test');
function execCli(args, cb) { function execCli(args, cb) {
@ -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) { test('ES2015 support', function (t) {
t.plan(1); t.plan(1);
@ -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) { test('fail-fast mode', function (t) {
t.plan(5); t.plan(5);

Loading…
Cancel
Save