From d6699d1a2160a4f4294865252ab8eb9dc588c8da Mon Sep 17 00:00:00 2001 From: vdemedes Date: Wed, 18 Nov 2015 21:58:30 +0200 Subject: [PATCH] clean up runner --- lib/runner.js | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index a1f70b7de..15b017e43 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -8,12 +8,12 @@ var send = require('./send'); function noop() {} -function each(items, fn) { - return Promise.all(items.map(fn)); +function each(items, fn, context) { + return Promise.all(items.map(fn, context)); } -function eachSeries(items, fn) { - return Promise.resolve(items).each(fn); +function eachSeries(items, fn, context) { + return Promise.resolve(items).each(fn.bind(context)); } function Runner(opts) { @@ -27,6 +27,7 @@ function Runner(opts) { this.stats = { failCount: 0, + passCount: 0, testCount: 0 }; @@ -106,8 +107,7 @@ Runner.prototype.addOnlyTest = function (title, cb) { Runner.prototype._runTestWithHooks = function (test) { if (test.skip) { - this._addTestResult(test); - return Promise.resolve(); + return this._addTestResult(test); } var beforeHooks = this.tests.beforeEach.map(function (hook) { @@ -145,16 +145,17 @@ Runner.prototype._runTestWithHooks = function (test) { }); return this._runTest(test); - }.bind(this)).catch(noop); + }, 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 () { - this._addTestResult(test); - }.bind(this)); + return test.run().finally(function () { + self._addTestResult(test); + }); }; Runner.prototype.concurrent = function (tests) { @@ -162,11 +163,11 @@ Runner.prototype.concurrent = function (tests) { return this.serial(tests); } - return each(tests, this._runTestWithHooks.bind(this)); + return each(tests, this._runTestWithHooks, this); }; Runner.prototype.serial = function (tests) { - return eachSeries(tests, this._runTestWithHooks.bind(this)); + return eachSeries(tests, this._runTestWithHooks, this); }; Runner.prototype._addTestResult = function (test) { @@ -187,16 +188,18 @@ Runner.prototype._addTestResult = function (test) { }; Runner.prototype.run = function () { - var self = this; var tests = this.tests; var stats = this.stats; + var self = this; + + var hasOnlyTests = tests.only.length > 0; // Runner is executed directly in tests, in that case process.send() == undefined if (process.send) { send('stats', stats); } - return eachSeries(tests.before, this._runTest.bind(this)) + return eachSeries(tests.before, this._runTest, this) .catch(noop) .then(function () { if (stats.failCount > 0) { @@ -207,17 +210,21 @@ Runner.prototype.run = function () { return self.concurrent(tests.only); }) .then(function () { - return tests.only.length ? [] : self.serial(tests.serial); + if (!hasOnlyTests) { + return self.serial(tests.serial); + } }) .then(function () { - return tests.only.length ? [] : self.concurrent(tests.concurrent); + if (!hasOnlyTests) { + return self.concurrent(tests.concurrent); + } }) .then(function () { - return eachSeries(tests.after, self._runTest.bind(self)); + return eachSeries(tests.after, self._runTest, self); }) .catch(noop) .then(function () { - stats.testCount = tests.only.length ? tests.only.length : stats.testCount; + stats.testCount = tests.only.length || stats.testCount; stats.passCount = stats.testCount - stats.failCount; }); };