From da58c623fc171fc6f30b76dcf793a624f133eb44 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Thu, 22 Oct 2015 19:02:35 +0200 Subject: [PATCH] ensure that hooks run only around tests, fix #89 --- lib/logger.js | 1 - lib/runner.js | 37 +++++++++++++++++++++---------------- test/test.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 73e2155..264800d 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -73,4 +73,3 @@ x.report = function (passed, failed) { log.writelpad(chalk.green(passed, plur('test', passed), 'passed')); } }; - diff --git a/lib/runner.js b/lib/runner.js index e6b3ced..08227d9 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -4,6 +4,8 @@ var EventEmitter = require('events').EventEmitter; var Promise = require('bluebird'); var Test = require('./test'); +function noop() {} + function each(items, fn) { return Promise.all(items.map(fn)); } @@ -71,7 +73,7 @@ Runner.prototype.addAfterEachHook = function (title, cb) { }); }; -Runner.prototype._runTest = function (test) { +Runner.prototype._runTestWithHooks = function (test) { var self = this; var beforeHooks = self.tests.beforeEach.map(function (hook) { @@ -88,22 +90,26 @@ Runner.prototype._runTest = function (test) { tests.push(test); tests.push.apply(tests, afterHooks); - return eachSeries(tests, function (test) { - // add test result regardless of state - // but on error, don't execute next tests - return test.run() - .finally(function () { - self._addTestResult(test); - }); - }).catch(function () {}); + return eachSeries(tests, this._runTest.bind(this)).catch(noop); +}; + +Runner.prototype._runTest = function (test) { + var self = this; + + // add test result regardless of state + // but on error, don't execute next tests + return test.run() + .finally(function () { + self._addTestResult(test); + }); }; Runner.prototype.concurrent = function (tests) { - return each(tests, this._runTest.bind(this)); + return each(tests, this._runTestWithHooks.bind(this)); }; Runner.prototype.serial = function (tests) { - return eachSeries(tests, this._runTest.bind(this)); + return eachSeries(tests, this._runTestWithHooks.bind(this)); }; Runner.prototype._addTestResult = function (test) { @@ -125,7 +131,8 @@ Runner.prototype.run = function () { var tests = this.tests; var stats = this.stats; - return this.serial(tests.before) + return eachSeries(tests.before, this._runTest.bind(this)) + .catch(noop) .then(function () { if (stats.failCount > 0) { return Promise.reject(); @@ -138,11 +145,9 @@ Runner.prototype.run = function () { return self.concurrent(tests.concurrent); }) .then(function () { - return self.serial(tests.after); - }) - .catch(function () { - return; + return eachSeries(tests.after, self._runTest.bind(self)); }) + .catch(noop) .then(function () { stats.passCount = stats.testCount - stats.failCount; }); diff --git a/test/test.js b/test/test.js index 3a4ed9e..4ba2d0b 100644 --- a/test/test.js +++ b/test/test.js @@ -678,6 +678,43 @@ test('hooks - after each with serial tests', function (t) { }); }); +test('hooks - ensure hooks run only around tests', function (t) { + t.plan(1); + + var runner = new Runner(); + var arr = []; + + runner.addBeforeEachHook(function (a) { + arr.push('beforeEach'); + a.end(); + }); + + runner.addBeforeHook(function (a) { + arr.push('before'); + a.end(); + }); + + runner.addAfterEachHook(function (a) { + arr.push('afterEach'); + a.end(); + }); + + runner.addAfterHook(function (a) { + arr.push('after'); + a.end(); + }); + + runner.addTest(function (a) { + arr.push('test'); + a.end(); + }); + + runner.run().then(function () { + t.same(arr, ['before', 'beforeEach', 'test', 'afterEach', 'after']); + t.end(); + }); +}); + test('ES2015 support', function (t) { t.plan(1);