Skip to content

Commit d6699d1

Browse files
author
vdemedes
committed
clean up runner
1 parent 305fcdb commit d6699d1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/runner.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ var send = require('./send');
88

99
function noop() {}
1010

11-
function each(items, fn) {
12-
return Promise.all(items.map(fn));
11+
function each(items, fn, context) {
12+
return Promise.all(items.map(fn, context));
1313
}
1414

15-
function eachSeries(items, fn) {
16-
return Promise.resolve(items).each(fn);
15+
function eachSeries(items, fn, context) {
16+
return Promise.resolve(items).each(fn.bind(context));
1717
}
1818

1919
function Runner(opts) {
@@ -27,6 +27,7 @@ function Runner(opts) {
2727

2828
this.stats = {
2929
failCount: 0,
30+
passCount: 0,
3031
testCount: 0
3132
};
3233

@@ -106,8 +107,7 @@ Runner.prototype.addOnlyTest = function (title, cb) {
106107

107108
Runner.prototype._runTestWithHooks = function (test) {
108109
if (test.skip) {
109-
this._addTestResult(test);
110-
return Promise.resolve();
110+
return this._addTestResult(test);
111111
}
112112

113113
var beforeHooks = this.tests.beforeEach.map(function (hook) {
@@ -145,28 +145,29 @@ Runner.prototype._runTestWithHooks = function (test) {
145145
});
146146

147147
return this._runTest(test);
148-
}.bind(this)).catch(noop);
148+
}, this).catch(noop);
149149
};
150150

151151
Runner.prototype._runTest = function (test) {
152+
var self = this;
153+
152154
// add test result regardless of state
153155
// but on error, don't execute next tests
154-
return test.run()
155-
.finally(function () {
156-
this._addTestResult(test);
157-
}.bind(this));
156+
return test.run().finally(function () {
157+
self._addTestResult(test);
158+
});
158159
};
159160

160161
Runner.prototype.concurrent = function (tests) {
161162
if (hasFlag('serial')) {
162163
return this.serial(tests);
163164
}
164165

165-
return each(tests, this._runTestWithHooks.bind(this));
166+
return each(tests, this._runTestWithHooks, this);
166167
};
167168

168169
Runner.prototype.serial = function (tests) {
169-
return eachSeries(tests, this._runTestWithHooks.bind(this));
170+
return eachSeries(tests, this._runTestWithHooks, this);
170171
};
171172

172173
Runner.prototype._addTestResult = function (test) {
@@ -187,16 +188,18 @@ Runner.prototype._addTestResult = function (test) {
187188
};
188189

189190
Runner.prototype.run = function () {
190-
var self = this;
191191
var tests = this.tests;
192192
var stats = this.stats;
193+
var self = this;
194+
195+
var hasOnlyTests = tests.only.length > 0;
193196

194197
// Runner is executed directly in tests, in that case process.send() == undefined
195198
if (process.send) {
196199
send('stats', stats);
197200
}
198201

199-
return eachSeries(tests.before, this._runTest.bind(this))
202+
return eachSeries(tests.before, this._runTest, this)
200203
.catch(noop)
201204
.then(function () {
202205
if (stats.failCount > 0) {
@@ -207,17 +210,21 @@ Runner.prototype.run = function () {
207210
return self.concurrent(tests.only);
208211
})
209212
.then(function () {
210-
return tests.only.length ? [] : self.serial(tests.serial);
213+
if (!hasOnlyTests) {
214+
return self.serial(tests.serial);
215+
}
211216
})
212217
.then(function () {
213-
return tests.only.length ? [] : self.concurrent(tests.concurrent);
218+
if (!hasOnlyTests) {
219+
return self.concurrent(tests.concurrent);
220+
}
214221
})
215222
.then(function () {
216-
return eachSeries(tests.after, self._runTest.bind(self));
223+
return eachSeries(tests.after, self._runTest, self);
217224
})
218225
.catch(noop)
219226
.then(function () {
220-
stats.testCount = tests.only.length ? tests.only.length : stats.testCount;
227+
stats.testCount = tests.only.length || stats.testCount;
221228
stats.passCount = stats.testCount - stats.failCount;
222229
});
223230
};

0 commit comments

Comments
 (0)