Skip to content

Commit f375884

Browse files
authored
refactor(test): reduce execKarma to a reasonable size (#3496)
Finally! Introduced extra step "I wait until server output contains" instead of hard-coded timeout. It will regularly evaluate a condition until it fulfills. This approach should potentially perform a little bit better than the previous solution.
1 parent a3d1f11 commit f375884

File tree

5 files changed

+52
-49
lines changed

5 files changed

+52
-49
lines changed

test/e2e/browser_console.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ Feature: Browser Console Configuration
183183
];
184184
singleRun = false;
185185
"""
186-
When I run Karma
186+
When I start a server in background
187+
And I wait until server output contains:
188+
"""
189+
Executed 1 of 1 SUCCESS
190+
"""
191+
And I run Karma
187192
Then it passes with like:
188193
"""
189194
LOG: 'foo'

test/e2e/error.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Feature: Error Display
1818
"""
1919
SyntaxError: Unexpected token '}'
2020
"""
21+
2122
Scenario: Not single-run Syntax Error in a test file
2223
Given a configuration with:
2324
"""
@@ -29,7 +30,12 @@ Feature: Error Display
2930
];
3031
singleRun = false;
3132
"""
32-
When I run Karma
33+
When I start a server in background
34+
And I wait until server output contains:
35+
"""
36+
Executed 2 of 2 (1 FAILED)
37+
"""
38+
And I run Karma
3339
Then it fails with like:
3440
"""
3541
SyntaxError: Unexpected token '}'

test/e2e/pass-opts.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ Feature: Passing Options
1515
singleRun = false;
1616
"""
1717
And command line arguments of: "-- arg1 arg2"
18-
When I run Karma
18+
When I start a server in background
19+
And I wait until server output contains:
20+
"""
21+
Executed 1 of 1 (1 FAILED)
22+
"""
23+
And I run Karma
1924
Then it passes with no debug:
2025
"""
2126
.

test/e2e/step_definitions/core_steps.js

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,21 @@ const { defineParameterType, Given, Then, When } = require('cucumber')
22
const fs = require('fs')
33
const path = require('path')
44
const { exec } = require('child_process')
5+
const { waitForCondition } = require('./utils')
56
const stopper = require('../../../lib/stopper')
67

78
let additionalArgs = []
89

910
function execKarma (command, level, callback) {
1011
level = level || 'warn'
1112

12-
const configFile = this.configFile
13-
const runtimePath = this.karmaExecutable
14-
const baseDir = this.workDir
15-
16-
const executor = (done) => {
17-
const cmd = runtimePath + ' ' + command + ' --log-level ' + level + ' ' + configFile + ' ' + additionalArgs
18-
19-
return exec(cmd, {
20-
cwd: baseDir
21-
}, done)
22-
}
23-
24-
if (command === 'run') {
25-
this.runBackgroundProcess(['start', '--log-level', 'warn', configFile])
26-
.then(() => {
27-
const cmd = runtimePath + ' run ' + configFile + ' ' + additionalArgs
28-
29-
setTimeout(() => {
30-
exec(cmd, {
31-
cwd: baseDir
32-
}, (error, stdout, stderr) => {
33-
if (error) {
34-
this.lastRun.error = error
35-
}
36-
this.lastRun.stdout = stdout
37-
this.lastRun.stderr = stderr
38-
callback()
39-
})
40-
}, 1000)
41-
})
42-
.catch((error) => callback(error))
43-
} else {
44-
executor((error, stdout, stderr) => {
45-
if (error) {
46-
this.lastRun.error = error
47-
}
48-
this.lastRun.stdout = stdout
49-
this.lastRun.stderr = stderr
50-
callback()
51-
})
52-
}
13+
const cmd = `${this.karmaExecutable} ${command} --log-level ${level} ${this.configFile} ${additionalArgs}`
14+
exec(cmd, { cwd: this.workDir }, (error, stdout, stderr) => {
15+
this.lastRun.error = error
16+
this.lastRun.stdout = stdout.toString()
17+
this.lastRun.stderr = stderr.toString()
18+
callback()
19+
})
5320
}
5421

5522
Given('a default configuration', function () {
@@ -83,6 +50,10 @@ When('I start a server in background', async function () {
8350
await this.runBackgroundProcess(['start', '--log-level', 'debug', this.configFile])
8451
})
8552

53+
When('I wait until server output contains:', async function (expectedOutput) {
54+
await waitForCondition(() => this.backgroundProcess.stdout.includes(expectedOutput))
55+
})
56+
8657
defineParameterType({
8758
name: 'command',
8859
regexp: /run|start|init|stop/
@@ -105,7 +76,7 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
10576
const noDebug = mode === 'no debug'
10677
const like = mode === 'like'
10778
const regexp = mode === 'regexp'
108-
let actualOutput = this.lastRun.stdout.toString()
79+
let actualOutput = this.lastRun.stdout
10980
let lines
11081

11182
if (noDebug) {
@@ -132,9 +103,9 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
132103
})
133104

134105
Then('it fails with:', function (expectedOutput, callback) {
135-
const actualOutput = this.lastRun.stdout.toString()
106+
const actualOutput = this.lastRun.stdout
136107
const actualError = this.lastRun.error
137-
const actualStderr = this.lastRun.stderr.toString()
108+
const actualStderr = this.lastRun.stderr
138109

139110
if (actualOutput.match(expectedOutput)) {
140111
return callback()
@@ -146,9 +117,10 @@ Then('it fails with:', function (expectedOutput, callback) {
146117
})
147118

148119
Then('it fails with like:', function (expectedOutput, callback) {
149-
const actualOutput = this.lastRun.stdout.toString()
120+
const actualOutput = this.lastRun.stdout
150121
const actualError = this.lastRun.error
151-
const actualStderr = this.lastRun.stderr.toString()
122+
const actualStderr = this.lastRun.stderr
123+
152124
if (actualOutput.match(new RegExp(expectedOutput))) {
153125
return callback()
154126
}

test/e2e/step_definitions/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { promisify } = require('util')
2+
3+
const sleep = promisify(setTimeout)
4+
5+
module.exports.waitForCondition = async (evaluateCondition, timeout = 1000) => {
6+
let remainingTime = timeout
7+
while (!evaluateCondition()) {
8+
if (remainingTime > 0) {
9+
await sleep(50)
10+
remainingTime -= 50
11+
} else {
12+
throw new Error(`Condition not fulfilled, waited ${timeout}ms`)
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)