diff --git a/lib/listener/globalTimeout.js b/lib/listener/globalTimeout.js index 07bed2807..c674fc8b6 100644 --- a/lib/listener/globalTimeout.js +++ b/lib/listener/globalTimeout.js @@ -20,14 +20,29 @@ module.exports = function () { // disable timeout for BeforeSuite/AfterSuite hooks // add separate configs to them? + // When a BeforeSuite/AfterSuite hook starts we want to disable the + // per-test timeout during that hook execution only. Previously the + // code cleared `suiteTimeout` permanently which caused the suite + // level timeout to be lost for subsequent tests. Save previous + // values and restore them when the hook finishes. + let __prevTimeout = undefined + let __prevSuiteTimeout = undefined + event.dispatcher.on(event.hook.started, hook => { - if (hook instanceof BeforeSuiteHook) { + if (hook instanceof BeforeSuiteHook || hook instanceof AfterSuiteHook) { + __prevTimeout = timeout + // copy array to preserve original values + __prevSuiteTimeout = suiteTimeout.slice() timeout = null suiteTimeout = [] } - if (hook instanceof AfterSuiteHook) { - timeout = null - suiteTimeout = [] + }) + + event.dispatcher.on(event.hook.finished, hook => { + if (hook instanceof BeforeSuiteHook || hook instanceof AfterSuiteHook) { + // restore previously stored values + timeout = __prevTimeout + suiteTimeout = __prevSuiteTimeout.slice() } }) diff --git a/test/data/sandbox/configs/timeouts/beforeSuite_timeout_test.js b/test/data/sandbox/configs/timeouts/beforeSuite_timeout_test.js new file mode 100644 index 000000000..21a2fb9f7 --- /dev/null +++ b/test/data/sandbox/configs/timeouts/beforeSuite_timeout_test.js @@ -0,0 +1,9 @@ +Feature('Global Timeout with BeforeSuite') + +BeforeSuite(() => { + // No stuff needed here to reproduce the issue +}) + +Scenario('enforce global timeout with BeforeSuite', ({ I }) => { + I.waitForSleep(4 * 1000) +}) diff --git a/test/data/sandbox/configs/timeouts/codecept.beforeSuiteTimeout.conf.js b/test/data/sandbox/configs/timeouts/codecept.beforeSuiteTimeout.conf.js new file mode 100644 index 000000000..fe55427c8 --- /dev/null +++ b/test/data/sandbox/configs/timeouts/codecept.beforeSuiteTimeout.conf.js @@ -0,0 +1,11 @@ +exports.config = { + tests: './*_test.js', + output: './output', + helpers: { + CustomHelper: { + require: './customHelper.js', + }, + }, + name: 'beforeSuiteTimeout', + timeout: 2, +} diff --git a/test/runner/timeout_test.js b/test/runner/timeout_test.js index 2f1d8c1d5..5f8d543fb 100644 --- a/test/runner/timeout_test.js +++ b/test/runner/timeout_test.js @@ -100,4 +100,14 @@ describe('CodeceptJS Timeouts', function () { done() }) }) + + it('should enforce global timeout even with BeforeSuite', done => { + exec(config_run_config('codecept.beforeSuiteTimeout.conf.js', 'enforce global timeout with BeforeSuite', true), (err, stdout) => { + debug_this_test && console.log(stdout) + expect(stdout).toContain('Timeout 2s exceeded') + expect(stdout).toContain('TestTimeoutError') + expect(err).toBeTruthy() + done() + }) + }) })