Skip to content Skip to sidebar Skip to footer

Expect Not ToThrow Function With Arguments - Jasmine

I have function that gets 3 arguments. I want to check that this function not throwing an error. I did something like this: expect(myFunc).not.toThrow(); The problem is myFunc nee

Solution 1:

toThrow matcher requires function to be passed as argument to expect so you can simply wrap your function call in anonymous function:

expect(function() {
    myFunc(arg1, arg2, arg3);
}).not.toThrow();

You can also use bind to create new 'version' of your function that when called will be passed provided arguments:

expect(myFunc.bind(null, arg1, arg2, arg3)).not.toThrow();

Solution 2:

You can use the .not method:

expect(() => actionToCheck).not.toThrow()

https://jestjs.io/docs/expect#not


Solution 3:

If your function throws an exception the test will fail anyways. So it's not necessary to explicitly have an expect statement for it. The problem is that if you don't have an expect statement in your test, it will give you a warning that you have no expectations. The solution is to have an empty expect statement.

myFunc();
expect().nothing();

This is just an alternative way to do it. This can also be useful if you want to make sure that myFunc() finishes at all or just hangs up.


Post a Comment for "Expect Not ToThrow Function With Arguments - Jasmine"