Skip to content

Commit

Permalink
fixed tests for timeout errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Jan 24, 2025
1 parent eb139fe commit 6e5d017
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
14 changes: 11 additions & 3 deletions lib/listener/globalTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const recorder = require('../recorder')
const Config = require('../config')
const store = require('../store')
const debug = require('debug')('codeceptjs:timeout')
const { TIMEOUT_ORDER, TimeoutError, TestTimeoutError } = require('../timeout')
const { TIMEOUT_ORDER, TimeoutError, TestTimeoutError, StepTimeoutError } = require('../timeout')
const { BeforeSuiteHook, AfterSuiteHook } = require('../mocha/hooks')

module.exports = function () {
Expand Down Expand Up @@ -124,9 +124,17 @@ module.exports = function () {
if (!store.timeouts) return

recorder.catchWithoutStop(err => {
if (timeout && err instanceof TimeoutError && +Date.now() - step.startTime >= timeout) {
// we wrap timeout errors in a StepTimeoutError
// but only if global timeout is set
// should we wrap all timeout errors?
if (err instanceof TimeoutError) {
const testTimeoutExceeded = timeout && +Date.now() - step.startTime >= timeout
debug('Step failed due to global test or suite timeout')
throw new TestTimeoutError(currentTimeout)
if (testTimeoutExceeded) {
debug('Test failed due to global test or suite timeout')
throw new TestTimeoutError(currentTimeout)
}
throw new StepTimeoutError(currentTimeout, step)
}
throw err
})
Expand Down
5 changes: 4 additions & 1 deletion lib/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,12 @@ class Cli extends Base {
if (lines.length > 5) {
truncatedLines.push('...')
}
err.message = '\n ' + truncatedLines.join('\n').replace(/^/gm, ' ').trim()
err.message = truncatedLines.join('\n').replace(/^/gm, ' ').trim()
}

// add new line before the message
err.message = '\n ' + err.message

const steps = test.steps || (test.ctx && test.ctx.test.steps)

if (steps && steps.length) {
Expand Down
2 changes: 1 addition & 1 deletion lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function getTimeoutPromise(timeoutMs, taskName) {
return [
new Promise((done, reject) => {
timer = setTimeout(() => {
reject(new TimeoutError(`Action ${taskName} was interrupted on step timeout ${timeoutMs}ms`))
reject(new TimeoutError(`Action ${taskName} was interrupted on timeout ${timeoutMs}ms`))
}, timeoutMs || 2e9)
}),
timer,
Expand Down
8 changes: 8 additions & 0 deletions lib/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@ class TestTimeoutError extends TimeoutError {
}
}

class StepTimeoutError extends TimeoutError {
constructor(timeout, step) {
super(`Step ${step.toCode().trim()} timed out after ${timeout}s`)
this.name = 'StepTimeoutError'
}
}

module.exports = {
TIMEOUT_ORDER,
getCurrentTimeout,
TimeoutError,
TestTimeoutError,
StepTimeoutError,
}
7 changes: 4 additions & 3 deletions test/runner/timeout_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('CodeceptJS Timeouts', function () {
it('should ignore timeouts if no timeout', done => {
exec(config_run_config('codecept.conf.js', 'no timeout test'), (err, stdout) => {
debug_this_test && console.log(stdout)
expect(stdout).not.toContain('Timeout')
expect(stdout).not.toContain('TimeoutError')
expect(stdout).not.toContain('was interrupted on')
expect(err).toBeFalsy()
done()
})
Expand All @@ -52,7 +53,7 @@ describe('CodeceptJS Timeouts', function () {
it('should prefer step timeout', done => {
exec(config_run_config('codecept.conf.js', 'timeout step', true), (err, stdout) => {
debug_this_test && console.log(stdout)
expect(stdout).toContain('was interrupted on step timeout 200ms')
expect(stdout).toContain('was interrupted on timeout 200ms')
expect(err).toBeTruthy()
done()
})
Expand All @@ -61,7 +62,7 @@ describe('CodeceptJS Timeouts', function () {
it('should keep timeout with steps', done => {
exec(config_run_config('codecept.timeout.conf.js', 'timeout step', true), (err, stdout) => {
debug_this_test && console.log(stdout)
expect(stdout).toContain('was interrupted on step timeout 100ms')
expect(stdout).toContain('was interrupted on timeout 100ms')
expect(err).toBeTruthy()
done()
})
Expand Down

0 comments on commit 6e5d017

Please sign in to comment.