Skip to content

Commit 5100a59

Browse files
committed
add helper
1 parent 5bdcd05 commit 5100a59

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/listener/globalTimeout.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import debugModule from 'debug'
77
const debug = debugModule('codeceptjs:timeout')
88
import { TIMEOUT_ORDER, TimeoutError, TestTimeoutError, StepTimeoutError } from '../timeout.js'
99
import { BeforeSuiteHook, AfterSuiteHook } from '../mocha/hooks.js'
10+
import { extractStepCode } from '../step.js'
1011

1112
export default function () {
1213
let timeout
@@ -122,15 +123,15 @@ export default function () {
122123
if (typeof timeout !== 'number') return
123124

124125
if (!store.timeouts) {
125-
debug('step', (step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim(), 'timeout disabled')
126+
debug('step', extractStepCode(step), 'timeout disabled')
126127
return
127128
}
128129

129130
if (timeout < 0) {
130131
debug('Previous steps timed out, setting timeout to 0.01s')
131132
step.setTimeout(0.01, TIMEOUT_ORDER.testOrSuite)
132133
} else {
133-
debug(`Setting timeout ${timeout}ms for step ${(step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim()}`)
134+
debug(`Setting timeout ${timeout}ms for step ${extractStepCode(step)}`)
134135
step.setTimeout(timeout, TIMEOUT_ORDER.testOrSuite)
135136
}
136137
})
@@ -158,17 +159,17 @@ export default function () {
158159

159160
event.dispatcher.on(event.step.finished, step => {
160161
if (!store.timeouts) {
161-
debug('step', (step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim(), 'timeout disabled')
162+
debug('step', extractStepCode(step), 'timeout disabled')
162163
return
163164
}
164165

165166
if (typeof timeout === 'number') debug('Timeout', timeout)
166167

167-
debug(`step ${(step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim()}:${step.status} duration`, step.duration)
168+
debug(`step ${extractStepCode(step)}:${step.status} duration`, step.duration)
168169
if (typeof timeout === 'number' && !Number.isNaN(timeout)) timeout -= step.duration
169170

170171
if (typeof timeout === 'number' && timeout <= 0 && recorder.isRunning()) {
171-
debug(`step ${(step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim()} timed out`)
172+
debug(`step ${extractStepCode(step)} timed out`)
172173
recorder.throw(new TestTimeoutError(currentTimeout))
173174
}
174175
})

lib/step.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import BaseStep from './step/base.js'
77
import StepConfig from './step/config.js'
8-
import Step from './step/helper.js'
8+
import Step, { extractStepCode } from './step/helper.js'
99

1010
/**
1111
* MetaStep is a step that is used to wrap other steps.
@@ -20,4 +20,4 @@ import MetaStep from './step/meta.js'
2020
import FuncStep from './step/func.js'
2121

2222
export default Step
23-
export { MetaStep, BaseStep, StepConfig, FuncStep }
23+
export { MetaStep, BaseStep, StepConfig, FuncStep, extractStepCode }

lib/step/helper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class HelperStep extends Step {
4040

4141
export default HelperStep
4242

43+
export function extractStepCode(step) {
44+
return (step.code || (typeof step.toCode === 'function' ? step.toCode() : step.name)).trim()
45+
}
46+
4347
function dryRunResolver() {
4448
return {
4549
get(target, prop) {

0 commit comments

Comments
 (0)