diff --git a/client/karma.js b/client/karma.js index edefcdb14..24af159d4 100644 --- a/client/karma.js +++ b/client/karma.js @@ -131,6 +131,13 @@ function Karma (socket, iframe, opener, navigator, location, document) { // TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL) self.error('Some of your tests did a full page reload!') } + reloadingContext = false + } + + function clearContext () { + reloadingContext = true + + navigateContextTo('about:blank') } this.log = function (type, args) { @@ -145,12 +152,6 @@ function Karma (socket, iframe, opener, navigator, location, document) { this.stringify = stringify - function clearContext () { - reloadingContext = true - - navigateContextTo('about:blank') - } - function getLocation (url, lineno, colno) { var location = '' @@ -234,11 +235,10 @@ function Karma (socket, iframe, opener, navigator, location, document) { } 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) + // A test could have incorrectly issued a navigate. To clear the context + // we will navigte the iframe. Delay ours to ensure the error from an + // incorrect navigate is processed. + setTimeout(clearContext) } socket.emit('complete', result || {}, function () { @@ -259,24 +259,26 @@ function Karma (socket, iframe, opener, navigator, location, document) { } socket.on('execute', function (cfg) { - // reset startEmitted and reload the iframe - startEmitted = false - self.config = cfg - // if not clearing context, reloadingContext always true to prevent beforeUnload error - reloadingContext = !self.config.clearContext - navigateContextTo(constant.CONTEXT_URL) - - if (self.config.clientDisplayNone) { - [].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) { - el.style.display = 'none' - }) - } + // Delay our navigation to the next event in case the clearContext has not completed. + setTimeout(function allowClearContextToComplete () { + // reset startEmitted and reload the iframe + startEmitted = false + self.config = cfg + + navigateContextTo(constant.CONTEXT_URL) + + if (self.config.clientDisplayNone) { + [].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) { + el.style.display = 'none' + }) + } - // clear the console before run - // works only on FF (Safari, Chrome do not allow to clear console from js source) - if (window.console && window.console.clear) { - window.console.clear() - } + // clear the console before run + // works only on FF (Safari, Chrome do not allow to clear console from js source) + if (window.console && window.console.clear) { + window.console.clear() + } + }) }) socket.on('stop', function () { this.complete() diff --git a/package-lock.json b/package-lock.json index 1db4189de..180435572 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2015,8 +2015,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=", - "dev": true, - "optional": true + "dev": true }, "constants-browserify": { "version": "1.0.0", @@ -4238,8 +4237,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", - "dev": true, - "optional": true + "dev": true }, "is-fullwidth-code-point": { "version": "1.0.0", @@ -4268,7 +4266,6 @@ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4719,9 +4716,9 @@ "dev": true }, "grunt": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/grunt/-/grunt-1.0.4.tgz", - "integrity": "sha512-PYsMOrOC+MsdGEkFVwMaMyc6Ob7pKmq+deg1Sjr+vvMWp35sztfwKE7qoN51V+UEtHsyNuMcGdgMLFkBHvMxHQ==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/grunt/-/grunt-1.1.0.tgz", + "integrity": "sha512-+NGod0grmviZ7Nzdi9am7vuRS/h76PcWDsV635mEXF0PEQMUV6Kb+OjTdsVxbi0PZmfQOjCMKb3w8CVZcqsn1g==", "dev": true, "requires": { "coffeescript": "~1.10.0", @@ -4735,9 +4732,9 @@ "grunt-legacy-log": "~2.0.0", "grunt-legacy-util": "~1.1.1", "iconv-lite": "~0.4.13", - "js-yaml": "~3.13.0", + "js-yaml": "~3.13.1", "minimatch": "~3.0.2", - "mkdirp": "~0.5.1", + "mkdirp": "~1.0.3", "nopt": "~3.0.6", "path-is-absolute": "~1.0.0", "rimraf": "~2.6.2" @@ -4769,6 +4766,12 @@ "resolve": "~1.1.0" } }, + "mkdirp": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.3.tgz", + "integrity": "sha512-6uCP4Qc0sWsgMLy1EOqqS/3rjDHOEnsStVr/4vtAIK2Y5i2kA7lFFejYrpIyiN9w0pYf4ckeCYT9f1r1P9KX5g==", + "dev": true + }, "resolve": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz", @@ -6648,7 +6651,6 @@ "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.3.5.tgz", "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==", "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -9702,8 +9704,7 @@ "version": "3.0.3", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==", - "dev": true, - "optional": true + "dev": true }, "yaml": { "version": "1.7.2", diff --git a/package.json b/package.json index 177839995..dc7ae40e0 100644 --- a/package.json +++ b/package.json @@ -416,12 +416,12 @@ "ua-parser-js": "0.7.21" }, "devDependencies": { + "@commitlint/cli": "^8.3.4", + "@commitlint/config-conventional": "^8.3.4", "browserify": "^16.2.3", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "chai-subset": "^1.2.2", - "@commitlint/cli": "^8.3.4", - "@commitlint/config-conventional": "^8.3.4", "cucumber": "^3.1.0", "eslint": "^5.16.0", "eslint-config-standard": "^12.0.0", @@ -429,7 +429,7 @@ "eslint-plugin-node": "^9.0.1", "eslint-plugin-promise": "^4.1.1", "eslint-plugin-standard": "^4.0.0", - "grunt": "^1.0.4", + "grunt": "^1.1.0", "grunt-auto-release": "^0.0.7", "grunt-browserify": "^5.0.0", "grunt-bump": "^0.8.0", diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index 3920fe3c0..bc3ad81f2 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -44,31 +44,40 @@ describe('Karma', function () { assert(startSpy.calledWith(config)) }) - it('should open a new window when useIFrame is false', function () { + it('should open a new window when useIFrame is false', function (done) { var config = ck.config = { useIframe: false, runInParent: false } socket.emit('execute', config) - assert(!ck.start.called) + setTimeout(function nextEventLoop () { + assert(!ck.start.called) - ck.loaded() - assert(startSpy.calledWith(config)) - assert(windowStub.calledWith('context.html')) + ck.loaded() + assert(startSpy.calledWith(config)) + assert(windowStub.calledWith('context.html')) + done() + }) }) - it('should not set style on elements', function () { + it('should not set style on elements', function (done) { var config = {} socket.emit('execute', config) - assert(Object.keys(elements[0].style).length === 0) + setTimeout(function nextEventLoop () { + assert(Object.keys(elements[0].style).length === 0) + done() + }) }) - it('should set display none on elements if clientDisplayNone', function () { + it('should set display none on elements if clientDisplayNone', function (done) { var config = { clientDisplayNone: true } socket.emit('execute', config) - assert(elements[0].style.display === 'none') - assert(elements[1].style.display === 'none') + setTimeout(function nextEventLoop () { + assert(elements[0].style.display === 'none') + assert(elements[1].style.display === 'none') + done() + }) }) it('should stop execution', function () { @@ -97,55 +106,65 @@ describe('Karma', function () { assert.notStrictEqual(k.start, ADAPTER_START_FN) }) - it('should not set up context if there was an error', function () { + it('should not set up context if there was an error', function (done) { var config = ck.config = { clearContext: true } socket.emit('execute', config) - var mockWindow = {} + setTimeout(function nextEventLoop () { + var mockWindow = {} - ck.error('page reload') - ck.setupContext(mockWindow) + ck.error('page reload') + ck.setupContext(mockWindow) - assert(mockWindow.onbeforeunload == null) - assert(mockWindow.onerror == null) + assert(mockWindow.onbeforeunload == null) + assert(mockWindow.onerror == null) + done() + }) }) - it('should setup context if there was error but clearContext config is false', function () { + it('should setup context if there was error but clearContext config is false', function (done) { var config = ck.config = { clearContext: false } socket.emit('execute', config) - var mockWindow = {} + setTimeout(function nextEventLoop () { + var mockWindow = {} - ck.error('page reload') - ck.setupContext(mockWindow) + ck.error('page reload') + ck.setupContext(mockWindow) - assert(mockWindow.onbeforeunload != null) - assert(mockWindow.onerror != null) + assert(mockWindow.onbeforeunload != null) + assert(mockWindow.onerror != null) + done() + }) }) - it('should error out if a script attempted to reload the browser after setup', function () { + it('should error out if a script attempted to reload the browser after setup', function (done) { // Perform setup var config = ck.config = { clearContext: true } socket.emit('execute', config) - var mockWindow = {} - ck.setupContext(mockWindow) - // Spy on our error handler - sinon.spy(k, 'error') + setTimeout(function nextEventLoop () { + var mockWindow = {} + ck.setupContext(mockWindow) + + // Spy on our error handler + sinon.spy(k, 'error') - // Emulate an unload event - mockWindow.onbeforeunload() + // Emulate an unload event + mockWindow.onbeforeunload() - // Assert our spy was called - assert(k.error.calledWith('Some of your tests did a full page reload!')) + // Assert our spy was called + assert(k.error.calledWith('Some of your tests did a full page reload!')) + done() + }) }) it('should report navigator name', function () { @@ -439,12 +458,10 @@ describe('Karma', function () { } socket.emit('execute', config) + clock.tick(1) var CURRENT_URL = iframe.src - ck.complete() - clock.tick(1) - assert.strictEqual(iframe.src, CURRENT_URL) }) @@ -455,6 +472,7 @@ describe('Karma', function () { } socket.emit('execute', config) + clock.tick(1) assert(!startSpy.called) ck.loaded()