@@ -2,25 +2,41 @@ const event = require('../event')
2
2
const output = require ( '../output' )
3
3
const recorder = require ( '../recorder' )
4
4
const Config = require ( '../config' )
5
- const { timeouts } = require ( '../store' )
5
+ const store = require ( '../store' )
6
+ const debug = require ( 'debug' ) ( 'codeceptjs:timeout' )
6
7
const { TIMEOUT_ORDER } = require ( '../step/timeout' )
8
+ const { BeforeSuiteHook, AfterSuiteHook } = require ( '../mocha/hooks' )
7
9
8
10
module . exports = function ( ) {
9
11
let timeout
10
12
let suiteTimeout = [ ]
11
13
let currentTest
12
14
let currentTimeout
13
15
14
- if ( ! timeouts ) {
16
+ if ( ! store . timeouts ) {
15
17
console . log ( 'Timeouts were disabled' )
16
18
return
17
19
}
18
20
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
+
19
34
event . dispatcher . on ( event . suite . before , suite => {
20
35
suiteTimeout = [ ]
21
36
let timeoutConfig = Config . get ( 'timeout' )
22
37
23
38
if ( timeoutConfig ) {
39
+ debug ( 'config:' , timeoutConfig )
24
40
if ( ! Number . isNaN ( + timeoutConfig ) ) {
25
41
checkForSeconds ( timeoutConfig )
26
42
suiteTimeout . push ( timeoutConfig )
@@ -40,6 +56,8 @@ module.exports = function () {
40
56
41
57
if ( suite . totalTimeout ) suiteTimeout . push ( suite . totalTimeout )
42
58
output . log ( `Timeouts: ${ suiteTimeout } ` )
59
+
60
+ if ( suiteTimeout . length > 0 ) debug ( suite . title , 'timeout' , suiteTimeout )
43
61
} )
44
62
45
63
event . dispatcher . on ( event . test . before , test => {
@@ -64,6 +82,13 @@ module.exports = function () {
64
82
65
83
timeout = test . totalTimeout || testTimeout || suiteTimeout [ suiteTimeout . length - 1 ]
66
84
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
+
67
92
currentTimeout = timeout
68
93
output . debug ( `Test Timeout: ${ timeout } s` )
69
94
timeout *= 1000
@@ -80,18 +105,32 @@ module.exports = function () {
80
105
event . dispatcher . on ( event . step . before , step => {
81
106
if ( typeof timeout !== 'number' ) return
82
107
108
+ if ( ! store . timeouts ) {
109
+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
110
+ return
111
+ }
112
+
83
113
if ( timeout < 0 ) {
114
+ debug ( 'Previous steps timed out, setting timeout to 0.01s' )
84
115
step . setTimeout ( 0.01 , TIMEOUT_ORDER . testOrSuite )
85
116
} else {
117
+ debug ( `Setting timeout ${ timeout } ms for step ${ step . toCode ( ) . trim ( ) } ` )
86
118
step . setTimeout ( timeout , TIMEOUT_ORDER . testOrSuite )
87
119
}
88
120
} )
89
121
90
122
event . dispatcher . on ( event . step . finished , step => {
123
+ if ( ! store . timeouts ) {
124
+ debug ( 'step' , step . toCode ( ) . trim ( ) , 'timeout disabled' )
125
+ return
126
+ }
127
+
91
128
if ( typeof timeout === 'number' && ! Number . isNaN ( timeout ) ) timeout -= step . duration
92
129
93
130
if ( typeof timeout === 'number' && timeout <= 0 && recorder . isRunning ( ) ) {
131
+ debug ( `step ${ step . toCode ( ) . trim ( ) } timed out` )
94
132
if ( currentTest && currentTest . callback ) {
133
+ debug ( `Failing test ${ currentTest . title } with timeout ${ currentTimeout } s` )
95
134
recorder . reset ( )
96
135
// replace mocha timeout with custom timeout
97
136
currentTest . timeout ( 0 )
0 commit comments