Skip to content

Commit

Permalink
chore: increase step timeout to prevent flakiness (#3518)
Browse files Browse the repository at this point in the history
Also added a custom error logic, so actual output is printed if condition times out.

Example https://travis-ci.org/github/karma-runner/karma/jobs/688421469#L643
  • Loading branch information
devoto13 authored May 18, 2020
1 parent 16010eb commit 9d66192
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion test/e2e/step_definitions/core_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ When('I start a server in background', async function () {
})

When('I wait until server output contains:', async function (expectedOutput) {
await waitForCondition(() => this.backgroundProcess.stdout.includes(expectedOutput))
await waitForCondition(
() => this.backgroundProcess.stdout.includes(expectedOutput),
5000,
() => new Error(
'Expected server output to contain the above text within 5000ms, but got:\n\n' +
this.backgroundProcess.stdout
)
)
})

defineParameterType({
Expand Down
8 changes: 6 additions & 2 deletions test/e2e/step_definitions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ const { promisify } = require('util')

const sleep = promisify(setTimeout)

module.exports.waitForCondition = async (evaluateCondition, timeout = 1000) => {
module.exports.waitForCondition = async (evaluateCondition, timeout = 1000, customError = null) => {
let remainingTime = timeout
while (!evaluateCondition()) {
if (remainingTime > 0) {
await sleep(50)
remainingTime -= 50
} else {
throw new Error(`Condition not fulfilled, waited ${timeout}ms`)
if (customError != null) {
throw customError()
} else {
throw new Error(`Condition not fulfilled, waited ${timeout}ms`)
}
}
}
}

0 comments on commit 9d66192

Please sign in to comment.