Skip to content

Commit 843fd50

Browse files
author
DavertMik
committed
fixing timeout errors
1 parent 82075c8 commit 843fd50

File tree

13 files changed

+46
-29
lines changed

13 files changed

+46
-29
lines changed

examples/codecept.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ exports.config = {
3737
steps: ['./step_definitions/steps.js'],
3838
},
3939
plugins: {
40-
tryTo: {
41-
enabled: true,
42-
},
4340
analyze: {
4441
enabled: true,
4542
},
@@ -63,9 +60,6 @@ exports.config = {
6360
subtitles: {
6461
enabled: true,
6562
},
66-
retryTo: {
67-
enabled: true,
68-
},
6963
},
7064

7165
tests: './*_test.js',

lib/listener/exit.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
const event = require('../event')
2+
const debug = require('debug')('codeceptjs:exit')
23

34
module.exports = function () {
45
let failedTests = []
56

6-
event.dispatcher.on(event.test.failed, testOrSuite => {
7-
// NOTE When an error happens in one of the hooks (BeforeAll/BeforeEach...) the event object
8-
// is a suite and not a test
9-
const id = testOrSuite.uid || (testOrSuite.ctx && testOrSuite.ctx.test.uid) || 'empty'
7+
event.dispatcher.on(event.test.failed, test => {
8+
const id = test.uid || (test.ctx && test.ctx.test.uid) || 'empty'
109
failedTests.push(id)
1110
})
1211

1312
// if test was successful after retries
14-
event.dispatcher.on(event.test.passed, testOrSuite => {
15-
// NOTE When an error happens in one of the hooks (BeforeAll/BeforeEach...) the event object
16-
// is a suite and not a test
17-
const id = testOrSuite.uid || (testOrSuite.ctx && testOrSuite.ctx.test.uid) || 'empty'
13+
event.dispatcher.on(event.test.passed, test => {
14+
const id = test.uid || (test.ctx && test.ctx.test.uid) || 'empty'
1815
failedTests = failedTests.filter(failed => id !== failed)
1916
})
2017

lib/listener/globalTimeout.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ module.exports = function () {
125125
return
126126
}
127127

128+
debug(`step ${step.toCode().trim()}:${step.status} duration`, step.duration)
128129
if (typeof timeout === 'number' && !Number.isNaN(timeout)) timeout -= step.duration
129130

130131
if (typeof timeout === 'number' && timeout <= 0 && recorder.isRunning()) {
131132
debug(`step ${step.toCode().trim()} timed out`)
132-
if (currentTest && currentTest.callback) {
133+
if (currentTest && currentTest.callback && currentTest.type == 'test') {
133134
debug(`Failing test ${currentTest.title} with timeout ${currentTimeout}s`)
134135
recorder.reset()
135136
// replace mocha timeout with custom timeout
136-
// currentTest.timeout(0.1)
137+
currentTest.timeout(0.1)
137138
currentTest.callback(new Error(`Timeout ${currentTimeout}s exceeded (with Before hook)`))
138139
currentTest.timedOut = true
139140
}

lib/mocha/cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,15 @@ class Cli extends Base {
230230
stack.shift()
231231
}
232232

233+
if (stack[0].trim() == 'Error:') {
234+
stack.shift()
235+
}
236+
233237
if (output.level() < 3) {
234238
stack = stack.slice(0, 3)
235239
}
236240

237-
err.stack = `${stack.join('\n')}\n\n${output.colors.blue(log)}`.trim()
241+
err.stack = `${stack.join('\n')}\n\n${output.colors.blue(log)}`
238242
} catch (e) {
239243
console.error(e)
240244
}

lib/mocha/suite.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const MochaSuite = require('mocha/lib/suite')
2-
32
/**
43
* @typedef {import('mocha')} Mocha
54
*/

lib/mocha/test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Test = require('mocha/lib/test')
22
const Suite = require('mocha/lib/suite')
33
const { test: testWrapper } = require('./asyncWrapper')
44
const { enhanceMochaSuite, createSuite } = require('./suite')
5-
const { genTestId, serializeError, clearString } = require('../utils')
5+
const { genTestId, serializeError, clearString, relativeDir } = require('../utils')
66
const Step = require('../step/base')
77
/**
88
* Factory function to create enhanced tests
@@ -46,6 +46,7 @@ function enhanceMochaTest(test) {
4646
test.addToSuite = function (suite) {
4747
enhanceMochaSuite(suite)
4848
suite.addTest(testWrapper(this))
49+
if (test.file) suite.file = relativeDir(test.file)
4950
test.tags = [...(test.tags || []), ...(suite.tags || [])]
5051
test.fullTitle = () => `${suite.title}: ${test.title}`
5152
test.uid = genTestId(test)
@@ -73,7 +74,7 @@ function deserializeTest(test) {
7374
)
7475
test.parent = Object.assign(new Suite(test.parent?.title || 'Suite'), test.parent)
7576
enhanceMochaSuite(test.parent)
76-
test.steps = test.steps.map(step => Object.assign(new Step(step.title), step))
77+
if (test.steps) test.steps = test.steps.map(step => Object.assign(new Step(step.title), step))
7778
return test
7879
}
7980

@@ -117,7 +118,7 @@ function serializeTest(test, err = null) {
117118
duration: test.duration || 0,
118119
err,
119120
parent,
120-
steps: [...test.steps].map(step => (step.simplify ? step.simplify() : step)),
121+
steps: test.steps?.toArray()?.map(step => (step.simplify ? step.simplify() : step)),
121122
}
122123
}
123124

lib/mocha/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare global {
1818
opts: Record<string, any>
1919
throws?: Error | string | RegExp | Function
2020
totalTimeout?: number
21+
relativeFile?: string
2122
addToSuite(suite: Mocha.Suite): void
2223
applyOptions(opts: Record<string, any>): void
2324
simplify(): Record<string, any>

lib/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ module.exports = {
135135
*/
136136
started: suite => {
137137
if (!suite.title) return
138-
print(`${colors.bold(suite.title)} --`)
138+
print(`${colors.bold(suite.title)} --`, colors.underline.grey(suite.file || ''))
139139
if (suite.comment) print(suite.comment)
140140
},
141141
},

lib/plugin/retryTo.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
module.exports = function () {
1+
const { retryTo } = require('../effects')
2+
3+
module.exports = function (config) {
24
console.log(`
3-
Deprecated Warning: 'retryTo' has been moved to the effects module.
5+
Deprecation Warning: 'retryTo' has been moved to the effects module.
46
You should update your tests to use it as follows:
57
68
\`\`\`javascript
@@ -16,4 +18,10 @@ await retryTo((tryNum) => {
1618
1719
For more details, refer to the documentation.
1820
`)
21+
22+
if (config.registerGlobal) {
23+
global.retryTo = retryTo
24+
}
25+
26+
return retryTo
1927
}

lib/plugin/tryTo.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module.exports = function () {
1+
const { tryTo } = require('../effects')
2+
3+
module.exports = function (config) {
24
console.log(`
35
Deprecated Warning: 'tryTo' has been moved to the effects module.
46
You should update your tests to use it as follows:
@@ -14,4 +16,10 @@ await tryTo(() => {
1416
1517
For more details, refer to the documentation.
1618
`)
19+
20+
if (config.registerGlobal) {
21+
global[config.registerGlobal] = tryTo
22+
}
23+
24+
return tryTo
1725
}

0 commit comments

Comments
 (0)