From 5fc8ee7feedfdc69b2e6253ce2414ca71b5db860 Mon Sep 17 00:00:00 2001 From: Cagdas Bayram Date: Mon, 4 Jan 2016 16:26:02 -0600 Subject: [PATCH] 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. --- client/karma.js | 17 ++++---- docs/config/01-configuration-file.md | 10 +++++ lib/config.js | 3 +- test/client/karma.spec.js | 58 +++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/client/karma.js b/client/karma.js index ac102e0c6..991f7e915 100644 --- a/client/karma.js +++ b/client/karma.js @@ -36,7 +36,7 @@ var Karma = function (socket, iframe, opener, navigator, location) { } this.setupContext = function (contextWindow) { - if (hasError) { + if (self.config.clearContext && hasError) { return } @@ -149,11 +149,13 @@ var Karma = function (socket, iframe, opener, navigator, location) { resultsBuffer = [] } - // 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) + 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) + } socket.emit('complete', result || {}, function () { if (returnUrl) { @@ -211,8 +213,9 @@ var Karma = function (socket, iframe, opener, navigator, location) { // reset hasError and reload the iframe hasError = false startEmitted = false - reloadingContext = false self.config = cfg + // if not clearing context, reloadingContext always true to prevent beforeUnload error + reloadingContext = !self.config.clearContext navigateContextTo(constant.CONTEXT_URL) // clear the console before run diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index 950670dfb..22b9199eb 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -505,6 +505,16 @@ iFrame and may need a new window to run. **Description:** Capture all console output and pipe it to the terminal. +## client.clearContext +**Type:** Boolean + +**Default:** `true` + +**Description:** Clear the context window + +If true, Karma clears the context window upon the completion of running the tests. If false, Karma does not clear the context window +upon the completion of running the tests. Setting this to false is useful when embedding a Jasmine Spec Runner Template. + ## urlRoot **Type:** String diff --git a/lib/config.js b/lib/config.js index c32036519..61d4d7fd0 100644 --- a/lib/config.js +++ b/lib/config.js @@ -253,7 +253,8 @@ var Config = function () { this.defaultClient = this.client = { args: [], useIframe: true, - captureConsole: true + captureConsole: true, + clearContext: true } this.browserDisconnectTimeout = 2000 this.browserDisconnectTolerance = 0 diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index ffd2a2e73..09ff93ff7 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -7,7 +7,7 @@ var Karma = require('../../client/karma') var MockSocket = require('./mocks').Socket describe('Karma', function () { - var socket, k, windowNavigator, windowLocation, windowStub, startSpy + var socket, k, windowNavigator, windowLocation, windowStub, startSpy, iframe var setTransportTo = function (transportName) { socket._setTransportNameTo(transportName) @@ -16,11 +16,12 @@ describe('Karma', function () { beforeEach(function () { socket = new MockSocket() + iframe = {} windowNavigator = {} windowLocation = {search: ''} windowStub = sinon.stub().returns({}) - k = new Karma(socket, {}, windowStub, windowNavigator, windowLocation) + k = new Karma(socket, iframe, windowStub, windowNavigator, windowLocation) startSpy = sinon.spy(k, 'start') }) @@ -76,6 +77,12 @@ describe('Karma', function () { }) it('should not set up context if there was an error', function () { + var config = { + clearContext: true + } + + socket.emit('execute', config) + var mockWindow = {} k.error('page reload') @@ -86,6 +93,23 @@ describe('Karma', function () { expect(mockWindow.onerror).to.not.exist }) + it('should setup context if there was error but clearContext config is false', function () { + var config = { + clearContext: false + } + + socket.emit('execute', config) + + var mockWindow = {} + + k.error('page reload') + k.setupContext(mockWindow) + + expect(mockWindow.__karma__).to.exist + expect(mockWindow.onbeforeunload).to.exist + expect(mockWindow.onerror).to.exist + }) + it('should report navigator name', function () { var spyInfo = sinon.spy(function (info) { expect(info.name).to.be.eql('Fake browser name') @@ -303,5 +327,35 @@ describe('Karma', function () { mockWindow.console.log('hello') expect(k.log).to.not.have.been.called }) + + it('should clear context window upon complete when clearContext config is true', function () { + var config = { + clearContext: true + } + + socket.emit('execute', config) + var CURRENT_URL = iframe.src + + k.complete() + + clock.tick(1) + + expect(iframe.src).to.not.be.eql(CURRENT_URL) + }) + + it('should not clear context window upon complete when clearContext config is false', function () { + var config = { + clearContext: false + } + + socket.emit('execute', config) + var CURRENT_URL = iframe.src + + k.complete() + + clock.tick(1) + + expect(iframe.src).to.be.eql(CURRENT_URL) + }) }) })