@@ -79,8 +79,8 @@ Test.prototype.serverAddress = function(app, path) {
79
79
*/
80
80
81
81
Test . prototype . expect = function ( a , b , c ) {
82
- // callback
83
- if ( typeof a === 'function' ) {
82
+ // callback or promise
83
+ if ( typeof a === 'function' || typeof a . then === 'function' ) {
84
84
this . _asserts . push ( a ) ;
85
85
return this ;
86
86
}
@@ -145,7 +145,7 @@ Test.prototype.end = function(fn) {
145
145
*/
146
146
147
147
Test . prototype . assert = function ( resError , res , fn ) {
148
- var error ;
148
+ var maybePromise ;
149
149
var i ;
150
150
151
151
// check for unexpected network errors or server not running/reachable errors
@@ -159,24 +159,61 @@ Test.prototype.assert = function(resError, res, fn) {
159
159
ETIMEDOUT : 'Operation timed out'
160
160
} ;
161
161
162
+
162
163
if ( ! res && resError && ( resError instanceof Error ) && ( resError . syscall === 'connect' )
163
164
&& ( Object . getOwnPropertyNames ( sysErrors ) . indexOf ( resError . code ) >= 0 ) ) {
164
- error = new Error ( resError . code + ': ' + sysErrors [ resError . code ] ) ;
165
- fn . call ( this , error , null ) ;
165
+ fn . call (
166
+ this ,
167
+ new Error ( resError . code + ': ' + sysErrors [ resError . code ] ) ,
168
+ null
169
+ ) ;
166
170
return ;
167
171
}
168
172
169
173
// asserts
170
- for ( i = 0 ; i < this . _asserts . length && ! error ; i += 1 ) {
171
- error = this . _assertFunction ( this . _asserts [ i ] , res ) ;
174
+ for ( i = 0 ; i < this . _asserts . length ; i += 1 ) {
175
+ // handle promises.
176
+ if ( typeof this . _asserts [ i ] . then === 'function' ) {
177
+ this . _asserts [ i ]
178
+ . then ( res )
179
+ . catch ( function ( promiseError ) {
180
+ return fn . call ( this , promiseError , res ) ;
181
+ } )
182
+ . then ( function ( maybeError ) {
183
+ if ( maybeError instanceof Error ) {
184
+ return fn . call ( this , maybeError , res ) ;
185
+ }
186
+ } ) ;
187
+ return ;
188
+ }
189
+
190
+ // handle functions
191
+ maybePromise = this . _assertFunction ( this . _asserts [ i ] , res ) ;
192
+ if ( maybePromise && typeof maybePromise . then === 'function' ) {
193
+ // function returned a promise
194
+ maybePromise
195
+ . then ( function ( maybeError ) { // eslint-disable-line no-loop-func
196
+ if ( maybeError instanceof Error ) {
197
+ return fn . call ( this , maybeError , res ) ;
198
+ }
199
+ } )
200
+ . catch ( function ( promiseError ) {
201
+ return fn . call ( this , promiseError , res ) ;
202
+ } ) ;
203
+ return ;
204
+ } else if ( maybePromise instanceof Error ) {
205
+ // function returned a non-promise. if it is an error, report it.
206
+ return fn . call ( this , maybePromise , res ) ;
207
+ }
172
208
}
173
209
174
210
// set unexpected superagent error if no other error has occurred.
175
- if ( ! error && resError instanceof Error && ( ! res || resError . status !== res . status ) ) {
176
- error = resError ;
211
+ if ( resError instanceof Error && ( ! res || resError . status !== res . status ) ) {
212
+ return fn . call ( this , resError , res ) ;
177
213
}
178
214
179
- fn . call ( this , error || null , res ) ;
215
+ // no error
216
+ fn . call ( this , null , res ) ;
180
217
} ;
181
218
182
219
/**
@@ -282,7 +319,17 @@ Test.prototype._assertFunction = function(check, res) {
282
319
} catch ( e ) {
283
320
err = e ;
284
321
}
285
- if ( err instanceof Error ) return err ;
322
+
323
+ // We got an error, return it.
324
+ if ( err instanceof Error ) {
325
+ return err ;
326
+ }
327
+
328
+ // We got a promise, return it and let the caller figure out if it contains
329
+ // an error.
330
+ if ( err && typeof err . then === 'function' ) {
331
+ return err ;
332
+ }
286
333
} ;
287
334
288
335
/**
0 commit comments