@@ -2,25 +2,41 @@ const event = require('../event')
22const output = require ( '../output' )
33const recorder = require ( '../recorder' )
44const Config = require ( '../config' )
5- const { timeouts } = require ( '../store' )
5+ const store = require ( '../store' )
6+ const debug = require ( 'debug' ) ( 'codeceptjs:timeout' )
67const { TIMEOUT_ORDER } = require ( '../step/timeout' )
8+ const { BeforeSuiteHook, AfterSuiteHook } = require ( '../mocha/hooks' )
79
810module . exports = function ( ) {
911 let timeout
1012 let suiteTimeout = [ ]
1113 let currentTest
1214 let currentTimeout
1315
14- if ( ! timeouts ) {
16+ if ( ! store . timeouts ) {
1517 console . log ( 'Timeouts were disabled' )
1618 return
1719 }
1820
21+ // disable timeout for BeforeSuite/AfterSuite hooks
22+ // add separate configs to them?
23+ event . dispatcher . on ( event . hook . started , hook => {
24+ if ( hook instanceof BeforeSuiteHook ) {
25+ timeout = null
26+ suiteTimeout = [ ]
27+ }
28+ if ( hook instanceof AfterSuiteHook ) {
29+ timeout = null
30+ suiteTimeout = [ ]
31+ }
32+ } )
33+
1934 event . dispatcher . on ( event . suite . before , suite => {
2035 suiteTimeout = [ ]
2136 let timeoutConfig = Config . get ( 'timeout' )
2237
2338 if ( timeoutConfig ) {
39+ debug ( 'config:' , timeoutConfig )
2440 if ( ! Number . isNaN ( + timeoutConfig ) ) {
2541 checkForSeconds ( timeoutConfig )
2642 suiteTimeout . push ( timeoutConfig )
@@ -40,6 +56,8 @@ module.exports = function () {
4056
4157 if ( suite . totalTimeout ) suiteTimeout . push ( suite . totalTimeout )
4258 output . log ( `Timeouts: ${ suiteTimeout } ` )
59+
60+ if ( suiteTimeout . length > 0 ) debug ( suite . title , 'timeout' , suiteTimeout )
4361 } )
4462
4563 event . dispatcher . on ( event . test . before , test => {
@@ -64,6 +82,13 @@ module.exports = function () {
6482
6583 timeout = test . totalTimeout || testTimeout || suiteTimeout [ suiteTimeout . length - 1 ]
6684 if ( ! timeout ) return
85+
86+ debug ( test . title , 'timeout' , {
87+ 'config from file' : testTimeout ,
88+ 'suite timeout' : suiteTimeout ,
89+ 'dynamic config' : test . totalTimeout ,
90+ } )
91+
6792 currentTimeout = timeout
6893 output . debug ( `Test Timeout: ${ timeout } s` )
6994 timeout *= 1000
@@ -80,18 +105,32 @@ module.exports = function () {
80105 event . dispatcher . on ( event . step . before , step => {
81106 if ( typeof timeout !== 'number' ) return
82107
108+ if ( ! store . timeouts ) {
109+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
110+ return
111+ }
112+
83113 if ( timeout < 0 ) {
114+ debug ( 'Previous steps timed out, setting timeout to 0.01s' )
84115 step . setTimeout ( 0.01 , TIMEOUT_ORDER . testOrSuite )
85116 } else {
117+ debug ( `Setting timeout ${ timeout } ms for step ${ step . toCode ( ) . trim ( ) } ` )
86118 step . setTimeout ( timeout , TIMEOUT_ORDER . testOrSuite )
87119 }
88120 } )
89121
90122 event . dispatcher . on ( event . step . finished , step => {
123+ if ( ! store . timeouts ) {
124+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
125+ return
126+ }
127+
91128 if ( typeof timeout === 'number' && ! Number . isNaN ( timeout ) ) timeout -= step . duration
92129
93130 if ( typeof timeout === 'number' && timeout <= 0 && recorder . isRunning ( ) ) {
131+ debug ( `step ${ step . toCode ( ) . trim ( ) } timed out` )
94132 if ( currentTest && currentTest . callback ) {
133+ debug ( `Failing test ${ currentTest . title } with timeout ${ currentTimeout } s` )
95134 recorder . reset ( )
96135 // replace mocha timeout with custom timeout
97136 currentTest . timeout ( 0 )
0 commit comments