Skip to content

Commit 05dc288

Browse files
authoredOct 6, 2020
fix(context): do not error when karma is navigating (#3565)
Change the flag name to karmaNavigating and set it along all paths where karma deliberately navigates. Other paths must be wrong. Fixes #3560
1 parent e5086fc commit 05dc288

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed
 

‎client/karma.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var util = require('../common/util')
44

55
function Karma (socket, iframe, opener, navigator, location, document) {
66
var startEmitted = false
7-
var reloadingContext = false
7+
var karmaNavigating = false
88
var self = this
99
var queryParams = util.parseQueryParams(location.search)
1010
var browserId = queryParams.id || util.generateId('manual-')
@@ -80,6 +80,7 @@ function Karma (socket, iframe, opener, navigator, location, document) {
8080

8181
var childWindow = null
8282
function navigateContextTo (url) {
83+
karmaNavigating = true
8384
if (self.config.useIframe === false) {
8485
// run in new window
8586
if (self.config.runInParent === false) {
@@ -89,9 +90,11 @@ function Karma (socket, iframe, opener, navigator, location, document) {
8990
childWindow.close()
9091
}
9192
childWindow = opener(url)
93+
karmaNavigating = false
9294
// run context on parent element (client_with_context)
9395
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
9496
} else if (url !== 'about:blank') {
97+
karmaNavigating = false
9598
var loadScript = function (idx) {
9699
if (idx < window.__karma__.scriptUrls.length) {
97100
var parser = new DOMParser()
@@ -123,20 +126,19 @@ function Karma (socket, iframe, opener, navigator, location, document) {
123126
// run in iframe
124127
} else {
125128
iframe.src = policy.createURL(url)
129+
karmaNavigating = false
126130
}
127131
}
128132

129133
this.onbeforeunload = function () {
130-
if (!reloadingContext) {
134+
if (!karmaNavigating) {
131135
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
132136
self.error('Some of your tests did a full page reload!')
133137
}
134-
reloadingContext = false
138+
karmaNavigating = false
135139
}
136140

137141
function clearContext () {
138-
reloadingContext = true
139-
140142
navigateContextTo('about:blank')
141143
}
142144

‎static/karma.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var util = require('../common/util')
1414

1515
function Karma (socket, iframe, opener, navigator, location, document) {
1616
var startEmitted = false
17-
var reloadingContext = false
17+
var karmaNavigating = false
1818
var self = this
1919
var queryParams = util.parseQueryParams(location.search)
2020
var browserId = queryParams.id || util.generateId('manual-')
@@ -90,6 +90,7 @@ function Karma (socket, iframe, opener, navigator, location, document) {
9090

9191
var childWindow = null
9292
function navigateContextTo (url) {
93+
karmaNavigating = true
9394
if (self.config.useIframe === false) {
9495
// run in new window
9596
if (self.config.runInParent === false) {
@@ -99,9 +100,11 @@ function Karma (socket, iframe, opener, navigator, location, document) {
99100
childWindow.close()
100101
}
101102
childWindow = opener(url)
103+
karmaNavigating = false
102104
// run context on parent element (client_with_context)
103105
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
104106
} else if (url !== 'about:blank') {
107+
karmaNavigating = false
105108
var loadScript = function (idx) {
106109
if (idx < window.__karma__.scriptUrls.length) {
107110
var parser = new DOMParser()
@@ -133,20 +136,19 @@ function Karma (socket, iframe, opener, navigator, location, document) {
133136
// run in iframe
134137
} else {
135138
iframe.src = policy.createURL(url)
139+
karmaNavigating = false
136140
}
137141
}
138142

139143
this.onbeforeunload = function () {
140-
if (!reloadingContext) {
144+
if (!karmaNavigating) {
141145
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
142146
self.error('Some of your tests did a full page reload!')
143147
}
144-
reloadingContext = false
148+
karmaNavigating = false
145149
}
146150

147151
function clearContext () {
148-
reloadingContext = true
149-
150152
navigateContextTo('about:blank')
151153
}
152154

‎test/client/karma.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ describe('Karma', function () {
145145
})
146146

147147
it('should error out if a script attempted to reload the browser after setup', function (done) {
148+
// Perform setup
149+
var config = ck.config = {
150+
clearContext: false
151+
}
152+
socket.emit('execute', config)
153+
154+
setTimeout(function nextEventLoop () {
155+
var mockWindow = {}
156+
ck.setupContext(mockWindow)
157+
158+
// Spy on our error handler
159+
sinon.spy(k, 'error')
160+
161+
// Emulate an unload event
162+
mockWindow.onbeforeunload()
163+
164+
// Assert our spy was called
165+
assert(k.error.calledWith('Some of your tests did a full page reload!'))
166+
done()
167+
})
168+
})
169+
170+
it('should error out if a script attempted to reload the browser after setup with clearContext true', function (done) {
148171
// Perform setup
149172
var config = ck.config = {
150173
clearContext: true

0 commit comments

Comments
 (0)
Please sign in to comment.