@@ -8,12 +8,12 @@ var send = require('./send');
8
8
9
9
function noop ( ) { }
10
10
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 ) ) ;
13
13
}
14
14
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 ) ) ;
17
17
}
18
18
19
19
function Runner ( opts ) {
@@ -27,6 +27,7 @@ function Runner(opts) {
27
27
28
28
this . stats = {
29
29
failCount : 0 ,
30
+ passCount : 0 ,
30
31
testCount : 0
31
32
} ;
32
33
@@ -106,8 +107,7 @@ Runner.prototype.addOnlyTest = function (title, cb) {
106
107
107
108
Runner . prototype . _runTestWithHooks = function ( test ) {
108
109
if ( test . skip ) {
109
- this . _addTestResult ( test ) ;
110
- return Promise . resolve ( ) ;
110
+ return this . _addTestResult ( test ) ;
111
111
}
112
112
113
113
var beforeHooks = this . tests . beforeEach . map ( function ( hook ) {
@@ -145,28 +145,29 @@ Runner.prototype._runTestWithHooks = function (test) {
145
145
} ) ;
146
146
147
147
return this . _runTest ( test ) ;
148
- } . bind ( this ) ) . catch ( noop ) ;
148
+ } , this ) . catch ( noop ) ;
149
149
} ;
150
150
151
151
Runner . prototype . _runTest = function ( test ) {
152
+ var self = this ;
153
+
152
154
// add test result regardless of state
153
155
// 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
+ } ) ;
158
159
} ;
159
160
160
161
Runner . prototype . concurrent = function ( tests ) {
161
162
if ( hasFlag ( 'serial' ) ) {
162
163
return this . serial ( tests ) ;
163
164
}
164
165
165
- return each ( tests , this . _runTestWithHooks . bind ( this ) ) ;
166
+ return each ( tests , this . _runTestWithHooks , this ) ;
166
167
} ;
167
168
168
169
Runner . prototype . serial = function ( tests ) {
169
- return eachSeries ( tests , this . _runTestWithHooks . bind ( this ) ) ;
170
+ return eachSeries ( tests , this . _runTestWithHooks , this ) ;
170
171
} ;
171
172
172
173
Runner . prototype . _addTestResult = function ( test ) {
@@ -187,16 +188,18 @@ Runner.prototype._addTestResult = function (test) {
187
188
} ;
188
189
189
190
Runner . prototype . run = function ( ) {
190
- var self = this ;
191
191
var tests = this . tests ;
192
192
var stats = this . stats ;
193
+ var self = this ;
194
+
195
+ var hasOnlyTests = tests . only . length > 0 ;
193
196
194
197
// Runner is executed directly in tests, in that case process.send() == undefined
195
198
if ( process . send ) {
196
199
send ( 'stats' , stats ) ;
197
200
}
198
201
199
- return eachSeries ( tests . before , this . _runTest . bind ( this ) )
202
+ return eachSeries ( tests . before , this . _runTest , this )
200
203
. catch ( noop )
201
204
. then ( function ( ) {
202
205
if ( stats . failCount > 0 ) {
@@ -207,17 +210,21 @@ Runner.prototype.run = function () {
207
210
return self . concurrent ( tests . only ) ;
208
211
} )
209
212
. then ( function ( ) {
210
- return tests . only . length ? [ ] : self . serial ( tests . serial ) ;
213
+ if ( ! hasOnlyTests ) {
214
+ return self . serial ( tests . serial ) ;
215
+ }
211
216
} )
212
217
. then ( function ( ) {
213
- return tests . only . length ? [ ] : self . concurrent ( tests . concurrent ) ;
218
+ if ( ! hasOnlyTests ) {
219
+ return self . concurrent ( tests . concurrent ) ;
220
+ }
214
221
} )
215
222
. then ( function ( ) {
216
- return eachSeries ( tests . after , self . _runTest . bind ( self ) ) ;
223
+ return eachSeries ( tests . after , self . _runTest , self ) ;
217
224
} )
218
225
. catch ( noop )
219
226
. then ( function ( ) {
220
- stats . testCount = tests . only . length ? tests . only . length : stats . testCount ;
227
+ stats . testCount = tests . only . length || stats . testCount ;
221
228
stats . passCount = stats . testCount - stats . failCount ;
222
229
} ) ;
223
230
} ;
0 commit comments