Fixing Jasmine “encountered a declaration exception”

If you ever come upon a Jasmine unit test that ends with “encountered a declaration exception”, it’s likely because you have committed a simple, subtle mistake. Every few weeks, I would get that error, and forgetting how I fixed it the previous time, spend another 30 minutes to fix it.

Here’s a screenshot of the Karma output:

enter image description here

Here’s the code that caused it:

describe('should be hidden when the scope is destroyed', () => {
  this.scope.$broadcast('$destroy');
  expect(this.scope.helpTooltip.hide).toHaveBeenCalledWith();
});

Can you spot the problem? It’s simple: I used describe instead of it. Let’s fix this…

it('should be hidden when the scope is destroyed', () => {
  this.scope.$broadcast('$destroy');
  expect(this.scope.helpTooltip.hide).toHaveBeenCalledWith();
});

VoilĂ ! No more error! Sometimes, the solution is that simple.

3 comments on “Fixing Jasmine “encountered a declaration exception”

Leave a Reply to jojo Cancel reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax