How to test if exception is thrown in AngularJS
angularjs, angularjs-directive, jasmine, unit-testing
Solution
This is how I do it:
describe("myDirective", function() {
it("should throw an error", inject(function ($compile, $rootScope) {
function errorFunctionWrapper()
{
$compile(angular.element("<div my-directive></div>"))($rootScope);
}
expect(errorFunctionWrapper).toThrow();
}));
});
Problem
I need to test a directive, and it should throw an exception. How can I test that the exception was thrown, in jasmine? The directives link function: ``` link: function() { if(something) { throw new TypeError('Error message'); } } ``` I have not yet successfully implemented a test that actually catches the error and reports that the test was successful.