Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -27,6 +27,7 @@ function Runner(opts) {

this.stats = {
failCount: 0,
passCount: 0,
testCount: 0
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -145,28 +145,29 @@ 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) {
if (hasFlag('serial')) {
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) {
Expand All @@ -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) {
Expand All @@ -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;
});
};