@@ -79,8 +79,8 @@ Test.prototype.serverAddress = function(app, path) {
7979 */
8080
8181Test . 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' ) {
8484 this . _asserts . push ( a ) ;
8585 return this ;
8686 }
@@ -145,7 +145,7 @@ Test.prototype.end = function(fn) {
145145 */
146146
147147Test . prototype . assert = function ( resError , res , fn ) {
148- var error ;
148+ var maybePromise ;
149149 var i ;
150150
151151 // check for unexpected network errors or server not running/reachable errors
@@ -159,24 +159,61 @@ Test.prototype.assert = function(resError, res, fn) {
159159 ETIMEDOUT : 'Operation timed out'
160160 } ;
161161
162+
162163 if ( ! res && resError && ( resError instanceof Error ) && ( resError . syscall === 'connect' )
163164 && ( 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+ ) ;
166170 return ;
167171 }
168172
169173 // 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+ }
172208 }
173209
174210 // 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 ) ;
177213 }
178214
179- fn . call ( this , error || null , res ) ;
215+ // no error
216+ fn . call ( this , null , res ) ;
180217} ;
181218
182219/**
@@ -282,7 +319,17 @@ Test.prototype._assertFunction = function(check, res) {
282319 } catch ( e ) {
283320 err = e ;
284321 }
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+ }
286333} ;
287334
288335/**
0 commit comments