Skip to content

Commit 92ffe60

Browse files
devoto13Jonathan Ginsburg
authored and
Jonathan Ginsburg
committedNov 16, 2021
fix: restartOnFileChange option not restarting the test run
The problem was caused by a race condition in the client JS implementation. When the `restartOnFileChange` option is enabled, the server would send [two commands](https://github.com/karma-runner/karma/blob/b153355de7e05559d877a625c9b0c5d23a3548bd/lib/server.js#L377) to the browser: `stop` and `execute`. Both of these commands navigate the context. `stop` navigates to the blank page, while `execute` navigate to the `context.html` thus triggering a test run. The `execute` command would trigger the navigation synchronously, but the `stop` command would trigger it on the next event loop tick. As a result, if both commands arrive almost at the same time, their order is effectively reversed: first schedule new execution and then immediately stop it. The bug is resolved by handling both commands synchronously thus ensuring correct order. The setTimeout() call in question was introduced in 15d80f4 to solve #27 and was retained over time. It's not completely clear why the timeout was added, but it does not seem to be relevant. With `clearContext: true`, karma will reset the context and thus cancel any scheduled navigations as soon as the test framework has reported the `complete` event. As navigations are canceled they don't affect karma and thus karma does not care about them. It's true that the user may have a test with race conditions (e.g. missing `done` callback), but it is up to them to investigate and fix it. With `clearContext: false`, the same logic applies, that karma does not care about navigations after `complete` event as long as they don't break karma itself. Here it gets a bit tricky: - Navigation within the same origin is undesired, but it doesn't break karma and the next execution can proceed normally. - Navigation to a different origin however is a problem as after such navigation the iframe becomes cross-origin and karma client will not be able to navigate it to the `context.html` or interact with it at all for that matter until the page reload (see [this SO answer](https://stackoverflow.com/q/25098021/1377864)). This will break the watch mode, but IMO we can let it be given that the case is quite uncommon, the user will see the browser window displaying something unexpected and the error in the CLI output. The changes in this commit don't make this case any worse and it does not seem to be possible to prevent such problematic navigations with the current state of browsers per https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload: > To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with. Moreover, some don't display them at all. Fixes #3724
1 parent b153355 commit 92ffe60

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed
 

‎client/karma.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,15 @@ function Karma (updater, socket, iframe, opener, navigator, location, document)
232232
resultsBuffer = []
233233
}
234234

235-
// A test could have incorrectly issued a navigate. Wait one turn
236-
// to ensure the error from an incorrect navigate is processed.
237-
var config = this.config
238-
setTimeout(function () {
239-
socket.emit('complete', result || {})
240-
if (config.clearContext) {
241-
navigateContextTo('about:blank')
242-
} else {
243-
self.updater.updateTestStatus('complete')
244-
}
245-
if (returnUrl) {
246-
location.href = returnUrl
247-
}
248-
})
235+
socket.emit('complete', result || {})
236+
if (this.config.clearContext) {
237+
navigateContextTo('about:blank')
238+
} else {
239+
self.updater.updateTestStatus('complete')
240+
}
241+
if (returnUrl) {
242+
location.href = returnUrl
243+
}
249244
}
250245

251246
this.info = function (info) {

‎static/karma.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,15 @@ function Karma (updater, socket, iframe, opener, navigator, location, document)
242242
resultsBuffer = []
243243
}
244244

245-
// A test could have incorrectly issued a navigate. Wait one turn
246-
// to ensure the error from an incorrect navigate is processed.
247-
var config = this.config
248-
setTimeout(function () {
249-
socket.emit('complete', result || {})
250-
if (config.clearContext) {
251-
navigateContextTo('about:blank')
252-
} else {
253-
self.updater.updateTestStatus('complete')
254-
}
255-
if (returnUrl) {
256-
location.href = returnUrl
257-
}
258-
})
245+
socket.emit('complete', result || {})
246+
if (this.config.clearContext) {
247+
navigateContextTo('about:blank')
248+
} else {
249+
self.updater.updateTestStatus('complete')
250+
}
251+
if (returnUrl) {
252+
location.href = returnUrl
253+
}
259254
}
260255

261256
this.info = function (info) {

0 commit comments

Comments
 (0)
Please sign in to comment.