Skip to content

Commit

Permalink
Timeout fixes (#4744)
Browse files Browse the repository at this point in the history
* added logs for global timeout

* added debug info and removed timeout for before/after suite hooks

* fixed global timeouts for BeforeAfterSuite

---------

Co-authored-by: DavertMik <[email protected]>
  • Loading branch information
DavertMik and DavertMik authored Jan 14, 2025
1 parent 0e9c08b commit 69ca4b1
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/listener/globalTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@ const event = require('../event')
const output = require('../output')
const recorder = require('../recorder')
const Config = require('../config')
const { timeouts } = require('../store')
const store = require('../store')
const debug = require('debug')('codeceptjs:timeout')
const { TIMEOUT_ORDER } = require('../step/timeout')
const { BeforeSuiteHook, AfterSuiteHook } = require('../mocha/hooks')

module.exports = function () {
let timeout
let suiteTimeout = []
let currentTest
let currentTimeout

if (!timeouts) {
if (!store.timeouts) {
console.log('Timeouts were disabled')
return
}

// disable timeout for BeforeSuite/AfterSuite hooks
// add separate configs to them?
event.dispatcher.on(event.hook.started, hook => {
if (hook instanceof BeforeSuiteHook) {
timeout = null
suiteTimeout = []
}
if (hook instanceof AfterSuiteHook) {
timeout = null
suiteTimeout = []
}
})

event.dispatcher.on(event.suite.before, suite => {
suiteTimeout = []
let timeoutConfig = Config.get('timeout')

if (timeoutConfig) {
debug('config:', timeoutConfig)
if (!Number.isNaN(+timeoutConfig)) {
checkForSeconds(timeoutConfig)
suiteTimeout.push(timeoutConfig)
Expand All @@ -40,6 +56,8 @@ module.exports = function () {

if (suite.totalTimeout) suiteTimeout.push(suite.totalTimeout)
output.log(`Timeouts: ${suiteTimeout}`)

if (suiteTimeout.length > 0) debug(suite.title, 'timeout', suiteTimeout)
})

event.dispatcher.on(event.test.before, test => {
Expand All @@ -64,6 +82,13 @@ module.exports = function () {

timeout = test.totalTimeout || testTimeout || suiteTimeout[suiteTimeout.length - 1]
if (!timeout) return

debug(test.title, 'timeout', {
'config from file': testTimeout,
'suite timeout': suiteTimeout,
'dynamic config': test.totalTimeout,
})

currentTimeout = timeout
output.debug(`Test Timeout: ${timeout}s`)
timeout *= 1000
Expand All @@ -80,18 +105,32 @@ module.exports = function () {
event.dispatcher.on(event.step.before, step => {
if (typeof timeout !== 'number') return

if (!store.timeouts) {
debug('step', step.toCode().trim(), 'timeout disabled')
return
}

if (timeout < 0) {
debug('Previous steps timed out, setting timeout to 0.01s')
step.setTimeout(0.01, TIMEOUT_ORDER.testOrSuite)
} else {
debug(`Setting timeout ${timeout}ms for step ${step.toCode().trim()}`)
step.setTimeout(timeout, TIMEOUT_ORDER.testOrSuite)
}
})

event.dispatcher.on(event.step.finished, step => {
if (!store.timeouts) {
debug('step', step.toCode().trim(), 'timeout disabled')
return
}

if (typeof timeout === 'number' && !Number.isNaN(timeout)) timeout -= step.duration

if (typeof timeout === 'number' && timeout <= 0 && recorder.isRunning()) {
debug(`step ${step.toCode().trim()} timed out`)
if (currentTest && currentTest.callback) {
debug(`Failing test ${currentTest.title} with timeout ${currentTimeout}s`)
recorder.reset()
// replace mocha timeout with custom timeout
currentTest.timeout(0)
Expand Down

0 comments on commit 69ca4b1

Please sign in to comment.