@@ -72,34 +72,32 @@ function wrapInControlFlow(globalFn, fnName) {
72
72
var driverError = new Error ( ) ;
73
73
driverError . stack = driverError . stack . replace ( / + a t .+ j a s m i n e w d .+ \n / , '' ) ;
74
74
75
- function asyncTestFn ( fn ) {
75
+ function asyncTestFn ( fn , description ) {
76
+ description = description ? ( '("' + description + '")' ) : '' ;
76
77
return function ( done ) {
77
- // deferred object for signaling completion of asychronous function within globalFn
78
- var asyncFnDone = webdriver . promise . defer ( ) ;
78
+ var async = fn . length > 0 ;
79
+ testFn = fn . bind ( this ) ;
79
80
80
- if ( fn . length === 0 ) {
81
- // function with globalFn not asychronous
82
- asyncFnDone . fulfill ( ) ;
83
- } else if ( fn . length > 1 ) {
84
- throw Error ( 'Invalid # arguments (' + fn . length + ') within function "' + fnName + '"' ) ;
85
- } else {
86
- // Add fail method to async done callback and override env fail to
87
- // reject async done promise
88
- asyncFnDone . fulfill . fail = function ( userError ) {
89
- asyncFnDone . reject ( new Error ( userError ) ) ;
90
- } ;
91
-
92
- }
93
-
94
- var flowFinished = flow . execute ( function ( ) {
95
- fn . call ( jasmine . getEnv ( ) , asyncFnDone . fulfill ) ;
96
- } , 'Run ' + fnName + ' in control flow' ) ;
97
-
98
- webdriver . promise . all ( [ asyncFnDone , flowFinished ] ) . then ( function ( ) {
99
- seal ( done ) ( ) ;
100
- } , function ( e ) {
101
- e . stack = e . stack + '\n==== async task ====\n' + driverError . stack ;
102
- done . fail ( e ) ;
81
+ flow . execute ( function controlFlowExecute ( ) {
82
+ return new webdriver . promise . Promise ( function ( fulfill , reject ) {
83
+ if ( async ) {
84
+ // If testFn is async (it expects a done callback), resolve the promise of this
85
+ // test whenever that callback says to. Any promises returned from testFn are
86
+ // ignored.
87
+ var proxyDone = fulfill ;
88
+ proxyDone . fail = function ( err ) {
89
+ reject ( err ) ;
90
+ } ;
91
+ testFn ( proxyDone ) ;
92
+ } else {
93
+ // Without a callback, testFn can return a promise, or it will
94
+ // be assumed to have completed synchronously.
95
+ fulfill ( testFn ( ) ) ;
96
+ }
97
+ } , flow ) ;
98
+ } , 'Run ' + fnName + description + ' in control flow' ) . then ( seal ( done ) , function ( err ) {
99
+ err . stack = err . stack + '\n==== async task ====\n' + driverError . stack ;
100
+ done . fail ( err ) ;
103
101
} ) ;
104
102
} ;
105
103
}
@@ -111,10 +109,10 @@ function wrapInControlFlow(globalFn, fnName) {
111
109
description = validateString ( arguments [ 0 ] ) ;
112
110
func = validateFunction ( arguments [ 1 ] ) ;
113
111
if ( ! arguments [ 2 ] ) {
114
- globalFn ( description , asyncTestFn ( func ) ) ;
112
+ globalFn ( description , asyncTestFn ( func , description ) ) ;
115
113
} else {
116
114
timeout = validateNumber ( arguments [ 2 ] ) ;
117
- globalFn ( description , asyncTestFn ( func ) , timeout ) ;
115
+ globalFn ( description , asyncTestFn ( func , description ) , timeout ) ;
118
116
}
119
117
break ;
120
118
case 'beforeEach' :
0 commit comments