@@ -39,21 +39,6 @@ var getTestArgs = function (name_, opts_, cb_) {
3939 return { name : name , opts : opts , cb : cb } ;
4040} ;
4141
42- var runProgeny = function ( ) {
43- var self = this ;
44- if ( this . _progeny . length ) {
45- var t = this . _progeny . shift ( ) ;
46- t . on ( 'end' , function ( ) { runProgeny . call ( self ) } ) ;
47- nextTick ( function ( ) {
48- t . run ( ) ;
49- } ) ;
50- return ;
51- }
52- if ( this . calledEnd || this . _plan ) {
53- this . _end ( ) ;
54- }
55- } ;
56-
5742function Test ( name_ , opts_ , cb_ ) {
5843 if ( ! ( this instanceof Test ) ) {
5944 return new Test ( name_ , opts_ , cb_ ) ;
@@ -120,11 +105,19 @@ Test.prototype.test = function (name, opts, cb) {
120105 this . emit ( 'test' , t ) ;
121106 t . on ( 'prerun' , function ( ) {
122107 self . assertCount ++ ;
123- } ) ;
108+ } )
124109
125- if ( ! this . _pendingAsserts ( ) ) {
126- runProgeny . call ( this ) ;
110+ if ( ! self . _pendingAsserts ( ) ) {
111+ nextTick ( function ( ) {
112+ self . _end ( ) ;
113+ } ) ;
127114 }
115+
116+ nextTick ( function ( ) {
117+ if ( ! self . _plan && self . pendingCount == self . _progeny . length ) {
118+ self . _end ( ) ;
119+ }
120+ } ) ;
128121} ;
129122
130123Test . prototype . comment = function ( msg ) {
@@ -139,19 +132,20 @@ Test.prototype.plan = function (n) {
139132 this . emit ( 'plan' , n ) ;
140133} ;
141134
142- Test . prototype . timeoutAfter = function ( ms ) {
135+ Test . prototype . timeoutAfter = function ( ms ) {
143136 if ( ! ms ) throw new Error ( 'timeoutAfter requires a timespan' ) ;
144137 var self = this ;
145- var timeout = safeSetTimeout ( function ( ) {
138+ var timeout = safeSetTimeout ( function ( ) {
146139 self . fail ( 'test timed out after ' + ms + 'ms' ) ;
147140 self . end ( ) ;
148141 } , ms ) ;
149- this . once ( 'end' , function ( ) {
142+ this . once ( 'end' , function ( ) {
150143 safeClearTimeout ( timeout ) ;
151144 } ) ;
152145}
153146
154147Test . prototype . end = function ( err ) {
148+ var self = this ;
155149 if ( arguments . length >= 1 && ! ! err ) {
156150 this . ifError ( err ) ;
157151 }
@@ -160,10 +154,18 @@ Test.prototype.end = function (err) {
160154 this . fail ( '.end() called twice' ) ;
161155 }
162156 this . calledEnd = true ;
163- runProgeny . call ( this ) ;
157+ this . _end ( ) ;
164158} ;
165159
166160Test . prototype . _end = function ( err ) {
161+ var self = this ;
162+ if ( this . _progeny . length ) {
163+ var t = this . _progeny . shift ( ) ;
164+ t . on ( 'end' , function ( ) { self . _end ( ) } ) ;
165+ t . run ( ) ;
166+ return ;
167+ }
168+
167169 if ( ! this . ended ) this . emit ( 'end' ) ;
168170 var pendingAsserts = this . _pendingAsserts ( ) ;
169171 if ( ! this . _planError && this . _plan !== undefined && pendingAsserts ) {
@@ -298,7 +300,9 @@ Test.prototype._assert = function assert (ok, opts) {
298300 if ( extra . exiting ) {
299301 self . _end ( ) ;
300302 } else {
301- runProgeny . call ( self ) ;
303+ nextTick ( function ( ) {
304+ self . _end ( ) ;
305+ } ) ;
302306 }
303307 }
304308
0 commit comments