Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context): do not error when karma is navigating #3565

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var util = require('../common/util')

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

var childWindow = null
function navigateContextTo (url) {
karmaNavigating = true
if (self.config.useIframe === false) {
// run in new window
if (self.config.runInParent === false) {
Expand All @@ -89,9 +90,11 @@ function Karma (socket, iframe, opener, navigator, location, document) {
childWindow.close()
}
childWindow = opener(url)
karmaNavigating = false
// run context on parent element (client_with_context)
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
} else if (url !== 'about:blank') {
karmaNavigating = false
var loadScript = function (idx) {
if (idx < window.__karma__.scriptUrls.length) {
var parser = new DOMParser()
Expand Down Expand Up @@ -123,20 +126,19 @@ function Karma (socket, iframe, opener, navigator, location, document) {
// run in iframe
} else {
iframe.src = policy.createURL(url)
karmaNavigating = false
}
}

this.onbeforeunload = function () {
if (!reloadingContext) {
if (!karmaNavigating) {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
self.error('Some of your tests did a full page reload!')
}
reloadingContext = false
karmaNavigating = false
}

function clearContext () {
reloadingContext = true

navigateContextTo('about:blank')
}

Expand Down
12 changes: 7 additions & 5 deletions static/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var util = require('../common/util')

function Karma (socket, iframe, opener, navigator, location, document) {
var startEmitted = false
var reloadingContext = false
var karmaNavigating = false
var self = this
var queryParams = util.parseQueryParams(location.search)
var browserId = queryParams.id || util.generateId('manual-')
Expand Down Expand Up @@ -90,6 +90,7 @@ function Karma (socket, iframe, opener, navigator, location, document) {

var childWindow = null
function navigateContextTo (url) {
karmaNavigating = true
if (self.config.useIframe === false) {
// run in new window
if (self.config.runInParent === false) {
Expand All @@ -99,9 +100,11 @@ function Karma (socket, iframe, opener, navigator, location, document) {
childWindow.close()
}
childWindow = opener(url)
karmaNavigating = false
// run context on parent element (client_with_context)
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
} else if (url !== 'about:blank') {
karmaNavigating = false
var loadScript = function (idx) {
if (idx < window.__karma__.scriptUrls.length) {
var parser = new DOMParser()
Expand Down Expand Up @@ -133,20 +136,19 @@ function Karma (socket, iframe, opener, navigator, location, document) {
// run in iframe
} else {
iframe.src = policy.createURL(url)
karmaNavigating = false
}
}

this.onbeforeunload = function () {
if (!reloadingContext) {
if (!karmaNavigating) {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
self.error('Some of your tests did a full page reload!')
}
reloadingContext = false
karmaNavigating = false
}

function clearContext () {
reloadingContext = true

navigateContextTo('about:blank')
}

Expand Down
23 changes: 23 additions & 0 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ describe('Karma', function () {
})

it('should error out if a script attempted to reload the browser after setup', function (done) {
// Perform setup
var config = ck.config = {
clearContext: false
}
socket.emit('execute', config)

setTimeout(function nextEventLoop () {
var mockWindow = {}
ck.setupContext(mockWindow)

// Spy on our error handler
sinon.spy(k, 'error')

// Emulate an unload event
mockWindow.onbeforeunload()

// Assert our spy was called
assert(k.error.calledWith('Some of your tests did a full page reload!'))
done()
})
})

it('should error out if a script attempted to reload the browser after setup with clearContext true', function (done) {
// Perform setup
var config = ck.config = {
clearContext: true
Expand Down