Skip to content

Commit 5fc8ee7

Browse files
author
Cagdas Bayram
committed
feat(config): Add a clearContext config to prevent clearing of context.
Enable a clearContext config which when set to false: - prevents clearing of context window upon completion of running of the tests. - always (re)sets up context regardless of errors This configuration is useful when embedding the Jasmine html reporter within the context window.
1 parent a731968 commit 5fc8ee7

4 files changed

Lines changed: 78 additions & 10 deletions

File tree

client/karma.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var Karma = function (socket, iframe, opener, navigator, location) {
3636
}
3737

3838
this.setupContext = function (contextWindow) {
39-
if (hasError) {
39+
if (self.config.clearContext && hasError) {
4040
return
4141
}
4242

@@ -149,11 +149,13 @@ var Karma = function (socket, iframe, opener, navigator, location) {
149149
resultsBuffer = []
150150
}
151151

152-
// give the browser some time to breath, there could be a page reload, but because a bunch of
153-
// tests could run in the same event loop, we wouldn't notice.
154-
setTimeout(function () {
155-
clearContext()
156-
}, 0)
152+
if (self.config.clearContext) {
153+
// give the browser some time to breath, there could be a page reload, but because a bunch of
154+
// tests could run in the same event loop, we wouldn't notice.
155+
setTimeout(function () {
156+
clearContext()
157+
}, 0)
158+
}
157159

158160
socket.emit('complete', result || {}, function () {
159161
if (returnUrl) {
@@ -211,8 +213,9 @@ var Karma = function (socket, iframe, opener, navigator, location) {
211213
// reset hasError and reload the iframe
212214
hasError = false
213215
startEmitted = false
214-
reloadingContext = false
215216
self.config = cfg
217+
// if not clearing context, reloadingContext always true to prevent beforeUnload error
218+
reloadingContext = !self.config.clearContext
216219
navigateContextTo(constant.CONTEXT_URL)
217220

218221
// clear the console before run

docs/config/01-configuration-file.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ iFrame and may need a new window to run.
505505

506506
**Description:** Capture all console output and pipe it to the terminal.
507507

508+
## client.clearContext
509+
**Type:** Boolean
510+
511+
**Default:** `true`
512+
513+
**Description:** Clear the context window
514+
515+
If true, Karma clears the context window upon the completion of running the tests. If false, Karma does not clear the context window
516+
upon the completion of running the tests. Setting this to false is useful when embedding a Jasmine Spec Runner Template.
517+
508518
## urlRoot
509519
**Type:** String
510520

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ var Config = function () {
253253
this.defaultClient = this.client = {
254254
args: [],
255255
useIframe: true,
256-
captureConsole: true
256+
captureConsole: true,
257+
clearContext: true
257258
}
258259
this.browserDisconnectTimeout = 2000
259260
this.browserDisconnectTolerance = 0

test/client/karma.spec.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var Karma = require('../../client/karma')
77
var MockSocket = require('./mocks').Socket
88

99
describe('Karma', function () {
10-
var socket, k, windowNavigator, windowLocation, windowStub, startSpy
10+
var socket, k, windowNavigator, windowLocation, windowStub, startSpy, iframe
1111

1212
var setTransportTo = function (transportName) {
1313
socket._setTransportNameTo(transportName)
@@ -16,11 +16,12 @@ describe('Karma', function () {
1616

1717
beforeEach(function () {
1818
socket = new MockSocket()
19+
iframe = {}
1920
windowNavigator = {}
2021
windowLocation = {search: ''}
2122
windowStub = sinon.stub().returns({})
2223

23-
k = new Karma(socket, {}, windowStub, windowNavigator, windowLocation)
24+
k = new Karma(socket, iframe, windowStub, windowNavigator, windowLocation)
2425
startSpy = sinon.spy(k, 'start')
2526
})
2627

@@ -76,6 +77,12 @@ describe('Karma', function () {
7677
})
7778

7879
it('should not set up context if there was an error', function () {
80+
var config = {
81+
clearContext: true
82+
}
83+
84+
socket.emit('execute', config)
85+
7986
var mockWindow = {}
8087

8188
k.error('page reload')
@@ -86,6 +93,23 @@ describe('Karma', function () {
8693
expect(mockWindow.onerror).to.not.exist
8794
})
8895

96+
it('should setup context if there was error but clearContext config is false', function () {
97+
var config = {
98+
clearContext: false
99+
}
100+
101+
socket.emit('execute', config)
102+
103+
var mockWindow = {}
104+
105+
k.error('page reload')
106+
k.setupContext(mockWindow)
107+
108+
expect(mockWindow.__karma__).to.exist
109+
expect(mockWindow.onbeforeunload).to.exist
110+
expect(mockWindow.onerror).to.exist
111+
})
112+
89113
it('should report navigator name', function () {
90114
var spyInfo = sinon.spy(function (info) {
91115
expect(info.name).to.be.eql('Fake browser name')
@@ -303,5 +327,35 @@ describe('Karma', function () {
303327
mockWindow.console.log('hello')
304328
expect(k.log).to.not.have.been.called
305329
})
330+
331+
it('should clear context window upon complete when clearContext config is true', function () {
332+
var config = {
333+
clearContext: true
334+
}
335+
336+
socket.emit('execute', config)
337+
var CURRENT_URL = iframe.src
338+
339+
k.complete()
340+
341+
clock.tick(1)
342+
343+
expect(iframe.src).to.not.be.eql(CURRENT_URL)
344+
})
345+
346+
it('should not clear context window upon complete when clearContext config is false', function () {
347+
var config = {
348+
clearContext: false
349+
}
350+
351+
socket.emit('execute', config)
352+
var CURRENT_URL = iframe.src
353+
354+
k.complete()
355+
356+
clock.tick(1)
357+
358+
expect(iframe.src).to.be.eql(CURRENT_URL)
359+
})
306360
})
307361
})

0 commit comments

Comments
 (0)