Expected behaviour
When running Karma with singleRun = false and autoWatch = false I would expect to be able to schedule a new run as soon as the 'old' run is completed.
Actual behaviour
A race condition in clearContext can report a false error in the new run.
Environment Details
- Karma version (output of
karma --version): 4.4.1
- Relevant part of your
karma.config.js file
{
singleRun: false,
autoWatch: false
}
Steps to reproduce the behaviour
It is difficult to reproduce since it is a race condition. Essentially it can happen in this scenario:
const karam = require('karma');
const server = new karma.Server({ singleRun: false, watch: false });
await server.start();
function runUntilError() {
karma.runner.run({}, exitCode => {
if(exitCode === 0) {
runUntilError();
} else {
console.log('Aha! Run errored');
}
});
}
runUntilError()
Triage
The problem is in the complete handler. Results are reported to the server before the context is cleared:
|
this.complete = function (result) { |
|
if (resultsBuffer.length) { |
|
socket.emit('result', resultsBuffer) |
|
resultsBuffer = [] |
|
} |
|
|
|
if (self.config.clearContext) { |
|
// give the browser some time to breath, there could be a page reload, but because a bunch of |
|
// tests could run in the same event loop, we wouldn't notice. |
|
setTimeout(function () { |
|
clearContext() |
|
}, 0) |
|
} |
The clearContext function sets the reloadingContext flag and navigates away.
|
function clearContext () { |
|
reloadingContext = true |
|
|
|
navigateContextTo('about:blank') |
|
} |
Now, just before the browser navigates away, the reloadingContext is reset by the next execution:
|
reloadingContext = !self.config.clearContext |
The concequence of this, is that the error is reported here:
|
self.error('Some of your tests did a full page reload!') |
Some of your tests did a full page reload!
However, I didn't reload any page.
Workaround
If you set client.clearContext to false, the context will not be cleared and there is no risk of this race condition.
Expected behaviour
When running Karma with
singleRun = falseandautoWatch = falseI would expect to be able to schedule a new run as soon as the 'old' run is completed.Actual behaviour
A race condition in
clearContextcan report a false error in the new run.Environment Details
karma --version):4.4.1karma.config.jsfileSteps to reproduce the behaviour
It is difficult to reproduce since it is a race condition. Essentially it can happen in this scenario:
Triage
The problem is in the complete handler. Results are reported to the server before the context is cleared:
karma/client/karma.js
Lines 230 to 242 in 6235e68
The
clearContextfunction sets thereloadingContextflag and navigates away.karma/client/karma.js
Lines 148 to 152 in 6235e68
Now, just before the browser navigates away, the
reloadingContextis reset by the next execution:karma/client/karma.js
Line 266 in 6235e68
The concequence of this, is that the error is reported here:
karma/client/karma.js
Line 132 in 6235e68
However, I didn't reload any page.
Workaround
If you set
client.clearContextto false, the context will not be cleared and there is no risk of this race condition.