Reputation: 1074
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
need to get arguments. How can I send the arguments?
P.S I tried to pass it argument but I got error that myFunc(arg1, arg2, arg3)
is not a function.
Upvotes: 46
Views: 24923
Reputation: 6674
toThrow
matcher requires a function to be passed as an argument to expect
, so you can simply wrap your function call in an anonymous function:
expect(function() {
myFunc(arg1, arg2, arg3);
}).not.toThrow();
You can also use bind to create a new 'version' of your function that, when called, will be passed the provided arguments:
expect(myFunc.bind(null, arg1, arg2, arg3)).not.toThrow();
Upvotes: 67
Reputation: 609
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.
Upvotes: 0
Reputation: 2153
You can use the .not
method:
expect(() => actionToCheck).not.toThrow()
https://jestjs.io/docs/expect#not
Upvotes: 1