Skip to content

Commit 1205bce

Browse files
devoto13johnjbarton
authored andcommitted
chore(test): fix flaky test cases (#3314)
* fix(test): fixed flaky reconnecting test cc2eff2 introduced different behavior depending on whether socket reconnected or browser reconnected after reload. This lead to the reconnecting test being flaky as, depending on the timing, it will trigger second test run and result in unexpected output. * fix(test): remove broken monitor action for Cucumber The issue is that it resulted in the following sequence of actions: - run karma start - run karma run - and then instantly kill karma start command This resulted in flaky tests because the kill could race, resulting in incomplete output. In fact test was unnecessary complicated as it was enough to use runOut, which asserts output of the karma run command. * fix(test): guard from repeated executions of the karma run 'data' event handler may be called multiple times, which will result in multiple calls to karma run. To take it even further, its execution is delayed by setTimeout, which means that subsequent executions will run while next test scenario is in progress and may mess it up. To prevent this make sure that karma run is executed only once independently of how many time 'data' event is fired.
1 parent 7f40349 commit 1205bce

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

test/e2e/error.feature

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Feature: Error Display
1818
"""
1919
SyntaxError: Unexpected token }
2020
"""
21-
Scenario: Single-run Syntax Error in a test file
21+
Scenario: Not single-run Syntax Error in a test file
2222
Given a configuration with:
2323
"""
2424
files = ['error/test.js', 'error/under-test.js'];
@@ -29,10 +29,8 @@ Feature: Error Display
2929
];
3030
singleRun = false;
3131
"""
32-
When I monitor Karma
33-
And I stop when the log contains 'SyntaxError'
32+
When I runOut Karma
3433
Then it fails with like:
3534
"""
3635
SyntaxError: Unexpected token }
3736
"""
38-
And I stop a server programmatically

test/e2e/step_definitions/core_steps.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ cucumber.defineSupportCode((a) => {
6464
}
6565

6666
const runOut = command === 'runOut'
67-
if (command === 'run' || command === 'runOut' || command === 'monitor') {
67+
if (command === 'run' || command === 'runOut') {
68+
let isRun = false
6869
this.child = spawn('' + runtimePath, ['start', '--log-level', 'warn', configFile])
6970
const done = () => {
7071
cleansingNeeded = true
@@ -84,22 +85,24 @@ cucumber.defineSupportCode((a) => {
8485
this.child.stdout.on('data', (chunk) => {
8586
this.lastRun.stdout += chunk.toString()
8687
const cmd = runtimePath + ' run ' + configFile + ' ' + additionalArgs
87-
setTimeout(() => {
88-
exec(cmd, {
89-
cwd: baseDir
90-
}, (error, stdout) => {
91-
if (error) {
92-
this.lastRun.error = error
93-
}
94-
if (runOut) {
95-
this.lastRun.stdout = stdout
96-
}
97-
done()
98-
})
99-
if (command === 'monitor') {
100-
done()
101-
}
102-
}, 1000)
88+
if (!isRun) {
89+
isRun = true
90+
91+
setTimeout(() => {
92+
exec(cmd, {
93+
cwd: baseDir
94+
}, (error, stdout, stderr) => {
95+
if (error) {
96+
this.lastRun.error = error
97+
}
98+
if (runOut) {
99+
this.lastRun.stdout = stdout
100+
this.lastRun.stderr = stderr
101+
}
102+
done()
103+
})
104+
}, 1000)
105+
}
103106
})
104107
} else {
105108
executor((error, stdout, stderr) => {
@@ -132,8 +135,8 @@ cucumber.defineSupportCode((a) => {
132135
setTimeout(function () {
133136
stopper.stop(_this.configFile, function (exitCode) {
134137
_this.stopperExitCode = exitCode
138+
callback()
135139
})
136-
callback()
137140
}, 1000)
138141
})
139142

@@ -160,7 +163,7 @@ cucumber.defineSupportCode((a) => {
160163

161164
defineParameterType({
162165
name: 'command',
163-
regexp: /run|runOut|start|init|stop|monitor/
166+
regexp: /run|runOut|start|init|stop/
164167
})
165168

166169
defineParameterType({
@@ -180,15 +183,6 @@ cucumber.defineSupportCode((a) => {
180183
execKarma.apply(this, [command, undefined, proxyPort, proxyPath, callback])
181184
})
182185

183-
When('I stop when the log contains {string}', function (message, callback) {
184-
setInterval(() => {
185-
if (this.lastRun.stdout.includes(message)) {
186-
this.child && this.child.kill()
187-
callback()
188-
}
189-
}, 100)
190-
})
191-
192186
defineParameterType({
193187
name: 'exact',
194188
regexp: /no\sdebug|like/

test/e2e/stop.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Feature: Stop karma
4444
"""
4545

4646

47-
Scenario: A server can be stopped programically
47+
Scenario: A server can be stopped programmatically
4848
Given a configuration with:
4949
"""
5050
files = ['basic/plus.js', 'basic/test.js'];

test/e2e/support/reconnecting/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ describe('plus', function () {
2222

2323
it('should re-connect', function (done) {
2424
expect(4).toBe(4)
25+
// Emit reconnect, so Karma will not start new test run after reconnecting.
26+
socket().emit('reconnect')
2527
socket().connect()
26-
// window.parent.socket.socket.connect()
2728

2829
done()
2930
})

0 commit comments

Comments
 (0)