Skip to content

Commit 15d80f4

Browse files
committed
fix: detect a full page reload, show error and recover
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
1 parent ab57064 commit 15d80f4

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

static/karma.src.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ var Karma = function(socket, context, navigator, location) {
5454
this.VERSION = VERSION;
5555

5656
this.setupContext = function(contextWindow) {
57+
if (hasError) {
58+
return;
59+
}
5760

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

78+
contextWindow.onbeforeunload = function(e, b) {
79+
if (context.src !== 'about:blank') {
80+
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
81+
contextWindow.__karma__.error('Some of your tests did a full page reload!');
82+
}
83+
};
84+
7585
// patch the console
7686
var localConsole = contextWindow.console = getConsole(contextWindow);
7787
var browserConsoleLog = localConsole.log;
@@ -170,7 +180,7 @@ var Karma = function(socket, context, navigator, location) {
170180
// we are not going to execute at all
171181
this.error = function(msg, url, line) {
172182
hasError = true;
173-
socket.emit('error', msg + '\nat ' + url + ':' + line);
183+
socket.emit('error', url ? msg + '\nat ' + url + (line ? ':' + line : '') : msg);
174184
this.complete();
175185
return false;
176186
};
@@ -180,8 +190,12 @@ var Karma = function(socket, context, navigator, location) {
180190
};
181191

182192
this.complete = function(result) {
183-
socket.emit('complete', result || {});
184-
clearContext();
193+
// give the browser some time to breath, there could be a page reload, but because a bunch of
194+
// tests could run in the same event loop, we wouldn't notice.
195+
setTimeout(function() {
196+
socket.emit('complete', result || {});
197+
clearContext();
198+
}, 0);
185199
};
186200

187201
this.info = function(info) {

test/client/karma.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ describe('karma', function() {
4545
});
4646

4747

48+
it('should not set up context if there was an error', function() {
49+
var mockWindow = {};
50+
51+
k.error('page reload');
52+
k.setupContext(mockWindow);
53+
54+
expect(mockWindow.__karma__).toBeUndefined();
55+
expect(mockWindow.onbeforeunload).toBeUndefined();
56+
expect(mockWindow.onerror).toBeUndefined();
57+
});
58+
59+
4860
it('should report navigator name', function() {
4961
var spyInfo = jasmine.createSpy('onInfo').andCallFake(function(info) {
5062
expect(info.name).toBe('Fake browser name');

0 commit comments

Comments
 (0)