Skip to content

Commit 6e5d017

Browse files
author
DavertMik
committed
fixed tests for timeout errors
1 parent eb139fe commit 6e5d017

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

lib/listener/globalTimeout.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const recorder = require('../recorder')
44
const Config = require('../config')
55
const store = require('../store')
66
const debug = require('debug')('codeceptjs:timeout')
7-
const { TIMEOUT_ORDER, TimeoutError, TestTimeoutError } = require('../timeout')
7+
const { TIMEOUT_ORDER, TimeoutError, TestTimeoutError, StepTimeoutError } = require('../timeout')
88
const { BeforeSuiteHook, AfterSuiteHook } = require('../mocha/hooks')
99

1010
module.exports = function () {
@@ -124,9 +124,17 @@ module.exports = function () {
124124
if (!store.timeouts) return
125125

126126
recorder.catchWithoutStop(err => {
127-
if (timeout && err instanceof TimeoutError && +Date.now() - step.startTime >= timeout) {
127+
// we wrap timeout errors in a StepTimeoutError
128+
// but only if global timeout is set
129+
// should we wrap all timeout errors?
130+
if (err instanceof TimeoutError) {
131+
const testTimeoutExceeded = timeout && +Date.now() - step.startTime >= timeout
128132
debug('Step failed due to global test or suite timeout')
129-
throw new TestTimeoutError(currentTimeout)
133+
if (testTimeoutExceeded) {
134+
debug('Test failed due to global test or suite timeout')
135+
throw new TestTimeoutError(currentTimeout)
136+
}
137+
throw new StepTimeoutError(currentTimeout, step)
130138
}
131139
throw err
132140
})

lib/mocha/cli.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ class Cli extends Base {
192192
if (lines.length > 5) {
193193
truncatedLines.push('...')
194194
}
195-
err.message = '\n ' + truncatedLines.join('\n').replace(/^/gm, ' ').trim()
195+
err.message = truncatedLines.join('\n').replace(/^/gm, ' ').trim()
196196
}
197197

198+
// add new line before the message
199+
err.message = '\n ' + err.message
200+
198201
const steps = test.steps || (test.ctx && test.ctx.test.steps)
199202

200203
if (steps && steps.length) {

lib/recorder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ function getTimeoutPromise(timeoutMs, taskName) {
386386
return [
387387
new Promise((done, reject) => {
388388
timer = setTimeout(() => {
389-
reject(new TimeoutError(`Action ${taskName} was interrupted on step timeout ${timeoutMs}ms`))
389+
reject(new TimeoutError(`Action ${taskName} was interrupted on timeout ${timeoutMs}ms`))
390390
}, timeoutMs || 2e9)
391391
}),
392392
timer,

lib/timeout.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@ class TestTimeoutError extends TimeoutError {
5050
}
5151
}
5252

53+
class StepTimeoutError extends TimeoutError {
54+
constructor(timeout, step) {
55+
super(`Step ${step.toCode().trim()} timed out after ${timeout}s`)
56+
this.name = 'StepTimeoutError'
57+
}
58+
}
59+
5360
module.exports = {
5461
TIMEOUT_ORDER,
5562
getCurrentTimeout,
5663
TimeoutError,
5764
TestTimeoutError,
65+
StepTimeoutError,
5866
}

test/runner/timeout_test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('CodeceptJS Timeouts', function () {
3333
it('should ignore timeouts if no timeout', done => {
3434
exec(config_run_config('codecept.conf.js', 'no timeout test'), (err, stdout) => {
3535
debug_this_test && console.log(stdout)
36-
expect(stdout).not.toContain('Timeout')
36+
expect(stdout).not.toContain('TimeoutError')
37+
expect(stdout).not.toContain('was interrupted on')
3738
expect(err).toBeFalsy()
3839
done()
3940
})
@@ -52,7 +53,7 @@ describe('CodeceptJS Timeouts', function () {
5253
it('should prefer step timeout', done => {
5354
exec(config_run_config('codecept.conf.js', 'timeout step', true), (err, stdout) => {
5455
debug_this_test && console.log(stdout)
55-
expect(stdout).toContain('was interrupted on step timeout 200ms')
56+
expect(stdout).toContain('was interrupted on timeout 200ms')
5657
expect(err).toBeTruthy()
5758
done()
5859
})
@@ -61,7 +62,7 @@ describe('CodeceptJS Timeouts', function () {
6162
it('should keep timeout with steps', done => {
6263
exec(config_run_config('codecept.timeout.conf.js', 'timeout step', true), (err, stdout) => {
6364
debug_this_test && console.log(stdout)
64-
expect(stdout).toContain('was interrupted on step timeout 100ms')
65+
expect(stdout).toContain('was interrupted on timeout 100ms')
6566
expect(err).toBeTruthy()
6667
done()
6768
})

0 commit comments

Comments
 (0)