Skip to content

Commit 873e4f9

Browse files
thorn0lusarz
authored andcommittedOct 5, 2018
fix: work around broken console methods
Sometimes console methods are broken, but this shouldn't break tests. See angular/angular.js#16644 (comment)
1 parent c097ecf commit 873e4f9

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed
 

‎context/karma.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ function ContextKarma (callParentKarmaMethod) {
122122
}
123123
localConsole[method] = function () {
124124
self.log(method, arguments)
125-
return Function.prototype.apply.call(orig, localConsole, arguments)
125+
try {
126+
return Function.prototype.apply.call(orig, localConsole, arguments)
127+
} catch (error) {
128+
self.log('warn', ['Console method ' + method + ' threw: ' + error])
129+
}
126130
}
127131
}
128132
for (var i = 0; i < logMethods.length; i++) {

‎test/client/karma.spec.js

+51-31
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,57 @@ describe('Karma', function () {
289289
assert.equal(promptCalled, true)
290290
assert.equal(promptResult, 'user-input')
291291
})
292+
293+
it('should patch the console if captureConsole is true', function () {
294+
sinon.spy(ck, 'log')
295+
ck.config.captureConsole = true
296+
297+
var mockWindow = {
298+
console: {
299+
log: function () {}
300+
}
301+
}
302+
303+
ck.setupContext(mockWindow)
304+
mockWindow.console.log('What?')
305+
assert(ck.log.calledWith('log'))
306+
assert(ck.log.args[0][1][0] === 'What?')
307+
})
308+
309+
it('should not patch the console if captureConsole is false', function () {
310+
sinon.spy(ck, 'log')
311+
ck.config.captureConsole = false
312+
313+
var mockWindow = {
314+
console: {
315+
log: function () {}
316+
}
317+
}
318+
319+
ck.setupContext(mockWindow)
320+
mockWindow.console.log('hello')
321+
assert(!ck.log.called)
322+
})
323+
324+
it('should not allow broken console methods to break tests (if captureConsole is true)', function () {
325+
sinon.spy(ck, 'log')
326+
ck.config.captureConsole = true
327+
328+
var mockWindow = {
329+
console: {
330+
log: function () {
331+
throw new Error('I am a broken console.log method.')
332+
}
333+
}
334+
}
335+
336+
ck.setupContext(mockWindow)
337+
mockWindow.console.log('What?')
338+
assert(ck.log.calledWith('log'))
339+
assert.equal(ck.log.args[0][1][0], 'What?')
340+
assert(ck.log.calledWith('warn'))
341+
assert(/^Console method log threw:[\s\S]+I am a broken console\.log method/.test(ck.log.args[1][1][0]))
342+
})
292343
})
293344

294345
describe('complete', function () {
@@ -343,37 +394,6 @@ describe('Karma', function () {
343394
clock.tick(10)
344395
})
345396

346-
it('should patch the console if captureConsole is true', function () {
347-
sinon.spy(ck, 'log')
348-
ck.config.captureConsole = true
349-
350-
var mockWindow = {
351-
console: {
352-
log: function () {}
353-
}
354-
}
355-
356-
ck.setupContext(mockWindow)
357-
mockWindow.console.log('What?')
358-
assert(ck.log.calledWith('log'))
359-
assert(ck.log.args[0][1][0] === 'What?')
360-
})
361-
362-
it('should not patch the console if captureConsole is false', function () {
363-
sinon.spy(ck, 'log')
364-
ck.config.captureConsole = false
365-
366-
var mockWindow = {
367-
console: {
368-
log: function () {}
369-
}
370-
}
371-
372-
ck.setupContext(mockWindow)
373-
mockWindow.console.log('hello')
374-
assert(!ck.log.called)
375-
})
376-
377397
it('should clear context window upon complete when clearContext config is true', function () {
378398
var config = ck.config = {
379399
clearContext: true

0 commit comments

Comments
 (0)
Please sign in to comment.