Skip to content

Commit

Permalink
Merge pull request #1356 from cbayram/feature-clearContextConfig
Browse files Browse the repository at this point in the history
feat(config): Add a clearContext config to prevent clearing of context
  • Loading branch information
dignifiedquire committed Jan 4, 2016
2 parents a731968 + 5fc8ee7 commit e509f84
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
17 changes: 10 additions & 7 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var Karma = function (socket, iframe, opener, navigator, location) {
}

this.setupContext = function (contextWindow) {
if (hasError) {
if (self.config.clearContext && hasError) {
return
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 56 additions & 2 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
})

Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit e509f84

Please sign in to comment.