40

The documentation at https://github.com/pivotal/jasmine/wiki/Matchers includes the following:

expect(function(){fn();}).toThrow(e);

As discussed in this question, the following does not work, because we want to pass a function object to expect rather than the result of calling fn():

expect(fn()).toThrow(e);

Does the following work?

expect(fn).toThrow(e);

If I've defined an object thing with a method doIt, does the following work?

expect(thing.doIt).toThrow(e);

(If so, is there a way to pass arguments to the doIt method?)

Empirically the answer seems to be yes, but I don't trust my understanding of JavaScript scoping quite enough to be sure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brahn
  • 12,096
  • 11
  • 39
  • 49
  • Possible duplicate : http://stackoverflow.com/questions/4144686/jasmine-how-to-write-a-test-which-expects-an-error-to-be-thrown – Petrov Sep 23 '15 at 18:53
  • The first link is (effectively) broken as it redirects to [a generic page](https://github.com/jasmine/jasmine/wiki/). – Peter Mortensen Jan 08 '22 at 00:23

4 Answers4

50

We can do away with the anonymous function wrapper by using Function.bind, which was introduced in ECMAScript 5. This works in the latest versions of browsers, and you can patch older browsers by defining the function yourself. An example definition is given at the Mozilla Developer Network.

Here's an example of how bind can be used with Jasmine.

describe('using bind with jasmine', function() {

    var f = function(x) {
        if(x === 2) {
            throw new Error();
        }
    }

    it('lets us avoid using an anonymous function', function() {
        expect(f.bind(null, 2)).toThrow();
    });

});

The first argument provided to bind is used as the this variable when f is called. Any additional arguments are passed to f when it is invoked. Here 2 is being passed as its first and only argument.

Havvy
  • 1,471
  • 14
  • 27
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
  • 1
    That works for me. Note: remember the null in f.bind( or your parameters will be one off in the called function. I missed that the first time. (I linked to this to make it easier for me to find :http://geekswithblogs.net/Aligned/archive/2013/03/21/use-the-bind-method-for-jasmine-tothrow-tests.aspx) Thanks! – AlignedDev Mar 21 '13 at 19:12
6

Let’s take a look at the Jasmine source code:

try {
  this.actual();
} catch (e) {
  exception = e;
}
if (exception) {
  result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected));
}

This is the core part of the toThrow method. So all the method does is to execute the method you want to expect and check if a exception was thrown.

So in your examples, fn or thing.doIt will be called in the Jasmine will check if an error was thrown and if the type of this error is the one you passed into toThrow .

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
5

Sadly, it seems that if I need to test a function that takes parameters, then I will need to wrap it with a function.

I would rather have preferred,

expect(myTestFunction, arg1, arg2).toThrow();

But I am ok with explicitly doing

expect(function(){myTestFunction(arg1, arg2);}).toThrow("some error");

FYI, note that we can also use a regex match on error:

expect(function (){myTestFunction(arg1, arg2);}).toThrowError(/err/);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
5

If it is used like this:

expect(myTestFunction(arg)).toThrowAnyError(); // incorrect

Then the function myTestFunction(arg) is executed before expect(...) and throw an exception before Jasmine has any chance of doing anything and it crashes the test resulting in an automatic failure.

If the function myTestFunction(arg) is not throwing anything (i.e. the code is not working as expected), then Jasmine would only get the result of the function and check that for errors - which is incorrect.

To alleviate this, the code that is expected to throw an error is supposed to be wrapped in a function. This is passed to Jasmine which will execute it and check for the expected exception.

expect(() => myTestFunction(arg)).toThrowAnyError(); // correct

Neovibrant
  • 747
  • 8
  • 16