Skip to content

Commit

Permalink
fix: work around broken console methods
Browse files Browse the repository at this point in the history
Sometimes console methods are broken, but this shouldn't break tests.
See angular/angular.js#16644 (comment)
  • Loading branch information
thorn0 authored and lusarz committed Oct 5, 2018
1 parent c097ecf commit 873e4f9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
6 changes: 5 additions & 1 deletion context/karma.js
Expand Up @@ -122,7 +122,11 @@ function ContextKarma (callParentKarmaMethod) {
}
localConsole[method] = function () {
self.log(method, arguments)
return Function.prototype.apply.call(orig, localConsole, arguments)
try {
return Function.prototype.apply.call(orig, localConsole, arguments)
} catch (error) {
self.log('warn', ['Console method ' + method + ' threw: ' + error])
}
}
}
for (var i = 0; i < logMethods.length; i++) {
Expand Down
82 changes: 51 additions & 31 deletions test/client/karma.spec.js
Expand Up @@ -289,6 +289,57 @@ describe('Karma', function () {
assert.equal(promptCalled, true)
assert.equal(promptResult, 'user-input')
})

it('should patch the console if captureConsole is true', function () {
sinon.spy(ck, 'log')
ck.config.captureConsole = true

var mockWindow = {
console: {
log: function () {}
}
}

ck.setupContext(mockWindow)
mockWindow.console.log('What?')
assert(ck.log.calledWith('log'))
assert(ck.log.args[0][1][0] === 'What?')
})

it('should not patch the console if captureConsole is false', function () {
sinon.spy(ck, 'log')
ck.config.captureConsole = false

var mockWindow = {
console: {
log: function () {}
}
}

ck.setupContext(mockWindow)
mockWindow.console.log('hello')
assert(!ck.log.called)
})

it('should not allow broken console methods to break tests (if captureConsole is true)', function () {
sinon.spy(ck, 'log')
ck.config.captureConsole = true

var mockWindow = {
console: {
log: function () {
throw new Error('I am a broken console.log method.')
}
}
}

ck.setupContext(mockWindow)
mockWindow.console.log('What?')
assert(ck.log.calledWith('log'))
assert.equal(ck.log.args[0][1][0], 'What?')
assert(ck.log.calledWith('warn'))
assert(/^Console method log threw:[\s\S]+I am a broken console\.log method/.test(ck.log.args[1][1][0]))
})
})

describe('complete', function () {
Expand Down Expand Up @@ -343,37 +394,6 @@ describe('Karma', function () {
clock.tick(10)
})

it('should patch the console if captureConsole is true', function () {
sinon.spy(ck, 'log')
ck.config.captureConsole = true

var mockWindow = {
console: {
log: function () {}
}
}

ck.setupContext(mockWindow)
mockWindow.console.log('What?')
assert(ck.log.calledWith('log'))
assert(ck.log.args[0][1][0] === 'What?')
})

it('should not patch the console if captureConsole is false', function () {
sinon.spy(ck, 'log')
ck.config.captureConsole = false

var mockWindow = {
console: {
log: function () {}
}
}

ck.setupContext(mockWindow)
mockWindow.console.log('hello')
assert(!ck.log.called)
})

it('should clear context window upon complete when clearContext config is true', function () {
var config = ck.config = {
clearContext: true
Expand Down

0 comments on commit 873e4f9

Please sign in to comment.