@@ -4,7 +4,7 @@ const recorder = require('../recorder')
44const Config = require ( '../config' )
55const store = require ( '../store' )
66const debug = require ( 'debug' ) ( 'codeceptjs:timeout' )
7- const { TIMEOUT_ORDER } = require ( '../step /timeout' )
7+ const { TIMEOUT_ORDER , TimeoutError , TestTimeoutError } = require ( '../timeout' )
88const { BeforeSuiteHook, AfterSuiteHook } = require ( '../mocha/hooks' )
99
1010module . exports = function ( ) {
@@ -119,25 +119,33 @@ module.exports = function () {
119119 }
120120 } )
121121
122+ event . dispatcher . on ( event . step . after , step => {
123+ if ( typeof timeout !== 'number' ) return
124+ if ( ! store . timeouts ) return
125+
126+ recorder . catchWithoutStop ( err => {
127+ if ( timeout && err instanceof TimeoutError && + Date . now ( ) - step . startTime >= timeout ) {
128+ debug ( 'Step failed due to global test or suite timeout' )
129+ throw new TestTimeoutError ( currentTimeout )
130+ }
131+ throw err
132+ } )
133+ } )
134+
122135 event . dispatcher . on ( event . step . finished , step => {
123136 if ( ! store . timeouts ) {
124137 debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
125138 return
126139 }
127140
141+ if ( typeof timeout === 'number' ) debug ( 'Timeout' , timeout )
142+
128143 debug ( `step ${ step . toCode ( ) . trim ( ) } :${ step . status } duration` , step . duration )
129144 if ( typeof timeout === 'number' && ! Number . isNaN ( timeout ) ) timeout -= step . duration
130145
131146 if ( typeof timeout === 'number' && timeout <= 0 && recorder . isRunning ( ) ) {
132147 debug ( `step ${ step . toCode ( ) . trim ( ) } timed out` )
133- if ( currentTest && currentTest . callback ) {
134- debug ( `Failing test ${ currentTest . title } with timeout ${ currentTimeout } s` )
135- recorder . reset ( )
136- // replace mocha timeout with custom timeout
137- currentTest . timeout ( 0.1 )
138- currentTest . callback ( new Error ( `Timeout ${ currentTimeout } s exceeded (with Before hook)` ) )
139- currentTest . timedOut = true
140- }
148+ recorder . throw ( new TestTimeoutError ( currentTimeout ) )
141149 }
142150 } )
143151}
0 commit comments