Skip to content

Commit aaf4fef

Browse files
authored
fix: global timeout before suite (#5275)
* Fix: preserve global timeout with BeforeSuite hook * test: clarify feature name for global timeout with BeforeSuite * chore: remove unused plugins field from beforeSuiteTimeout config
1 parent 2418a1e commit aaf4fef

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

lib/listener/globalTimeout.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,29 @@ module.exports = function () {
2020

2121
// disable timeout for BeforeSuite/AfterSuite hooks
2222
// add separate configs to them?
23+
// When a BeforeSuite/AfterSuite hook starts we want to disable the
24+
// per-test timeout during that hook execution only. Previously the
25+
// code cleared `suiteTimeout` permanently which caused the suite
26+
// level timeout to be lost for subsequent tests. Save previous
27+
// values and restore them when the hook finishes.
28+
let __prevTimeout = undefined
29+
let __prevSuiteTimeout = undefined
30+
2331
event.dispatcher.on(event.hook.started, hook => {
24-
if (hook instanceof BeforeSuiteHook) {
32+
if (hook instanceof BeforeSuiteHook || hook instanceof AfterSuiteHook) {
33+
__prevTimeout = timeout
34+
// copy array to preserve original values
35+
__prevSuiteTimeout = suiteTimeout.slice()
2536
timeout = null
2637
suiteTimeout = []
2738
}
28-
if (hook instanceof AfterSuiteHook) {
29-
timeout = null
30-
suiteTimeout = []
39+
})
40+
41+
event.dispatcher.on(event.hook.finished, hook => {
42+
if (hook instanceof BeforeSuiteHook || hook instanceof AfterSuiteHook) {
43+
// restore previously stored values
44+
timeout = __prevTimeout
45+
suiteTimeout = __prevSuiteTimeout.slice()
3146
}
3247
})
3348

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature('Global Timeout with BeforeSuite')
2+
3+
BeforeSuite(() => {
4+
// No stuff needed here to reproduce the issue
5+
})
6+
7+
Scenario('enforce global timeout with BeforeSuite', ({ I }) => {
8+
I.waitForSleep(4 * 1000)
9+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports.config = {
2+
tests: './*_test.js',
3+
output: './output',
4+
helpers: {
5+
CustomHelper: {
6+
require: './customHelper.js',
7+
},
8+
},
9+
name: 'beforeSuiteTimeout',
10+
timeout: 2,
11+
}

test/runner/timeout_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ describe('CodeceptJS Timeouts', function () {
100100
done()
101101
})
102102
})
103+
104+
it('should enforce global timeout even with BeforeSuite', done => {
105+
exec(config_run_config('codecept.beforeSuiteTimeout.conf.js', 'enforce global timeout with BeforeSuite', true), (err, stdout) => {
106+
debug_this_test && console.log(stdout)
107+
expect(stdout).toContain('Timeout 2s exceeded')
108+
expect(stdout).toContain('TestTimeoutError')
109+
expect(err).toBeTruthy()
110+
done()
111+
})
112+
})
103113
})

0 commit comments

Comments
 (0)