@@ -118,19 +118,25 @@ Test.prototype = {
118118
119119 // ensure the callbacks are executed serially for each module
120120 var callbackPromises = notStartedModules . reduce ( ( promiseChain , startModule ) => {
121- return promiseChain . then ( ( ) => {
121+ const moduleStartCallback = ( ) => {
122122 startModule . stats = { all : 0 , bad : 0 , started : now ( ) } ;
123123 emit ( "suiteStart" , startModule . suiteReport . start ( true ) ) ;
124124 return runLoggingCallbacks ( "moduleStart" , {
125125 name : startModule . name ,
126126 tests : startModule . tests
127127 } ) ;
128- } ) ;
128+ } ;
129+
130+ return promiseChain . then ( moduleStartCallback , moduleStartCallback ) ;
129131 } , Promise . resolve ( [ ] ) ) ;
130132
131- return callbackPromises . then ( ( ) => {
133+ const testStartHandler = ( ) => {
132134 config . current = this ;
133-
135+ const testStartResolvedHandler = ( ) => {
136+ if ( ! config . pollution ) {
137+ saveGlobal ( ) ;
138+ }
139+ } ;
134140 this . testEnvironment = extend ( { } , module . testEnvironment ) ;
135141
136142 this . started = now ( ) ;
@@ -140,10 +146,16 @@ Test.prototype = {
140146 module : module . name ,
141147 testId : this . testId ,
142148 previousFailure : this . previousFailure
143- } ) . then ( ( ) => {
144- if ( ! config . pollution ) {
145- saveGlobal ( ) ;
146- }
149+ } ) . then ( testStartResolvedHandler , function ( err ) {
150+ setTimeout ( testStartResolvedHandler ) ;
151+ throw err ;
152+ } ) ;
153+ } ;
154+
155+ return callbackPromises . then ( testStartHandler , ( err ) => {
156+ return Promise . reject ( err ) . catch ( ( err ) => {
157+ setTimeout ( testStartHandler ) ;
158+ throw err ;
147159 } ) ;
148160 } ) ;
149161 } ,
@@ -319,23 +331,7 @@ Test.prototype = {
319331 emit ( "testEnd" , this . testReport . end ( true ) ) ;
320332 this . testReport . slimAssertions ( ) ;
321333
322- return runLoggingCallbacks ( "testDone" , {
323- name : testName ,
324- module : moduleName ,
325- skipped : skipped ,
326- todo : todo ,
327- failed : bad ,
328- passed : this . assertions . length - bad ,
329- total : this . assertions . length ,
330- runtime : skipped ? 0 : this . runtime ,
331-
332- // HTML Reporter use
333- assertions : this . assertions ,
334- testId : this . testId ,
335-
336- // Source of Test
337- source : this . stack
338- } ) . then ( function ( ) {
334+ const testDoneResolvedHandler = function ( ) {
339335 if ( module . testsRun === numberOfTests ( module ) ) {
340336 const completedModules = [ module ] ;
341337
@@ -353,8 +349,35 @@ Test.prototype = {
353349 } ) ;
354350 } , Promise . resolve ( [ ] ) ) ;
355351 }
356- } ) . then ( function ( ) {
352+ } ;
353+
354+ return runLoggingCallbacks ( "testDone" , {
355+ name : testName ,
356+ module : moduleName ,
357+ skipped : skipped ,
358+ todo : todo ,
359+ failed : bad ,
360+ passed : this . assertions . length - bad ,
361+ total : this . assertions . length ,
362+ runtime : skipped ? 0 : this . runtime ,
363+
364+ // HTML Reporter use
365+ assertions : this . assertions ,
366+ testId : this . testId ,
367+
368+ // Source of Test
369+ source : this . stack
370+ } ) . then (
371+ testDoneResolvedHandler ,
372+ function ( err ) {
373+ setTimeout ( testDoneResolvedHandler ) ;
374+ throw err ;
375+ }
376+ ) . then ( function ( ) {
377+ config . current = undefined ;
378+ } , function ( err ) {
357379 config . current = undefined ;
380+ throw err ;
358381 } ) ;
359382
360383 function logSuiteEnd ( module ) {
@@ -660,7 +683,6 @@ function checkPollution() {
660683 if ( newGlobals . length > 0 ) {
661684 pushFailure ( "Introduced global variable(s): " + newGlobals . join ( ", " ) ) ;
662685 }
663-
664686 deletedGlobals = diff ( old , config . pollution ) ;
665687 if ( deletedGlobals . length > 0 ) {
666688 pushFailure ( "Deleted global variable(s): " + deletedGlobals . join ( ", " ) ) ;
0 commit comments