@@ -2,28 +2,39 @@ 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' )
66const debug = require ( 'debug' ) ( 'codeceptjs:timeout' )
77const TIMEOUT_ORDER = require ( '../step' ) . TIMEOUT_ORDER
8+ const { BeforeSuiteHook, AfterSuiteHook } = require ( '../mocha/hooks' )
89
910module . exports = function ( ) {
1011 let timeout
1112 let suiteTimeout = [ ]
1213 let currentTest
1314 let currentTimeout
1415
15- if ( ! timeouts ) {
16+ if ( ! store . timeouts ) {
1617 console . log ( 'Timeouts were disabled' )
1718 return
1819 }
1920
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+ suiteTimeout = [ ]
26+ }
27+ if ( hook instanceof AfterSuiteHook ) {
28+ suiteTimeout = [ ]
29+ }
30+ } )
31+
2032 event . dispatcher . on ( event . suite . before , suite => {
2133 suiteTimeout = [ ]
2234 let timeoutConfig = Config . get ( 'timeout' )
2335
24- debug ( 'config:' , timeoutConfig || 'none' )
25-
2636 if ( timeoutConfig ) {
37+ debug ( 'config:' , timeoutConfig )
2738 if ( ! Number . isNaN ( + timeoutConfig ) ) {
2839 checkForSeconds ( timeoutConfig )
2940 suiteTimeout . push ( timeoutConfig )
@@ -41,10 +52,10 @@ module.exports = function () {
4152 }
4253 }
4354
44- debug ( 'current suite timeout:' , suite . totalTimeout || 'none' )
4555 if ( suite . totalTimeout ) suiteTimeout . push ( suite . totalTimeout )
4656 output . log ( `Timeouts: ${ suiteTimeout } ` )
47- debug ( 'active timeouts' , suiteTimeout )
57+
58+ if ( suiteTimeout . length > 0 ) debug ( suite . title , 'timeout' , suiteTimeout )
4859 } )
4960
5061 event . dispatcher . on ( event . test . before , test => {
@@ -69,6 +80,13 @@ module.exports = function () {
6980
7081 timeout = test . totalTimeout || testTimeout || suiteTimeout [ suiteTimeout . length - 1 ]
7182 if ( ! timeout ) return
83+
84+ debug ( test . title , 'timeout' , {
85+ 'config from file' : testTimeout ,
86+ 'suite timeout' : suiteTimeout ,
87+ 'dynamic config' : test . totalTimeout ,
88+ } )
89+
7290 currentTimeout = timeout
7391 output . debug ( `Test Timeout: ${ timeout } s` )
7492 timeout *= 1000
@@ -85,6 +103,11 @@ module.exports = function () {
85103 event . dispatcher . on ( event . step . before , step => {
86104 if ( typeof timeout !== 'number' ) return
87105
106+ if ( ! store . timeouts ) {
107+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
108+ return
109+ }
110+
88111 if ( timeout < 0 ) {
89112 debug ( 'Previous steps timed out, setting timeout to 0.01s' )
90113 step . setTimeout ( 0.01 , TIMEOUT_ORDER . testOrSuite )
@@ -95,6 +118,11 @@ module.exports = function () {
95118 } )
96119
97120 event . dispatcher . on ( event . step . finished , step => {
121+ if ( ! store . timeouts ) {
122+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
123+ return
124+ }
125+
98126 if ( typeof timeout === 'number' && ! Number . isNaN ( timeout ) ) timeout -= step . duration
99127
100128 if ( typeof timeout === 'number' && timeout <= 0 && recorder . isRunning ( ) ) {
0 commit comments