Skip to content

Commit

Permalink
fix: detect a full page reload, show error and recover
Browse files Browse the repository at this point in the history
We need to make this even better, by providing which test caused the page reload. Before that, the adapters need to report spec_start. Even after that, the information which test caused the page reload does not have to be correct. Multiple tests can run within a single event loop (and they do, until there is an async test), so the developer has to set `jasmine.UPDATE_INTERVAL = -1` to know what test caused the page reload.

Closes #27
  • Loading branch information
vojtajina committed Jul 20, 2013
1 parent ab57064 commit 15d80f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 17 additions & 3 deletions static/karma.src.js
Expand Up @@ -54,6 +54,9 @@ var Karma = function(socket, context, navigator, location) {
this.VERSION = VERSION;

this.setupContext = function(contextWindow) {
if (hasError) {
return;
}

var getConsole = function(currentWindow) {
return currentWindow.console || {
Expand All @@ -72,6 +75,13 @@ var Karma = function(socket, context, navigator, location) {
return contextWindow.__karma__.error.apply(contextWindow.__karma__, arguments);
};

contextWindow.onbeforeunload = function(e, b) {

This comment has been minimized.

Copy link
@JemiloII

JemiloII Sep 23, 2015

Is there a way to disable this? I want to test the onbeforeunload method. However, whenever I call window.onbeforeunload() I received the error message.

This comment has been minimized.

Copy link
@dignifiedquire

dignifiedquire Sep 27, 2015

Member

No, but you should be able to use something like sinon to spy on it

This comment has been minimized.

Copy link
@sbvictory

sbvictory Jan 29, 2016

What if I want to trigger onbeforeunload? if i use window.onbeforeunload(); this will causes error. I am not talking about spy on this event.

if (context.src !== 'about:blank') {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
contextWindow.__karma__.error('Some of your tests did a full page reload!');
}
};

// patch the console
var localConsole = contextWindow.console = getConsole(contextWindow);
var browserConsoleLog = localConsole.log;
Expand Down Expand Up @@ -170,7 +180,7 @@ var Karma = function(socket, context, navigator, location) {
// we are not going to execute at all
this.error = function(msg, url, line) {
hasError = true;
socket.emit('error', msg + '\nat ' + url + ':' + line);
socket.emit('error', url ? msg + '\nat ' + url + (line ? ':' + line : '') : msg);
this.complete();
return false;
};
Expand All @@ -180,8 +190,12 @@ var Karma = function(socket, context, navigator, location) {
};

this.complete = function(result) {
socket.emit('complete', result || {});
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() {
socket.emit('complete', result || {});
clearContext();
}, 0);
};

this.info = function(info) {
Expand Down
12 changes: 12 additions & 0 deletions test/client/karma.spec.js
Expand Up @@ -45,6 +45,18 @@ describe('karma', function() {
});


it('should not set up context if there was an error', function() {
var mockWindow = {};

k.error('page reload');
k.setupContext(mockWindow);

expect(mockWindow.__karma__).toBeUndefined();
expect(mockWindow.onbeforeunload).toBeUndefined();
expect(mockWindow.onerror).toBeUndefined();
});


it('should report navigator name', function() {
var spyInfo = jasmine.createSpy('onInfo').andCallFake(function(info) {
expect(info.name).toBe('Fake browser name');
Expand Down

0 comments on commit 15d80f4

Please sign in to comment.