Expect not toThrow function with arguments - Jasmine

17,720

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

Share:
17,720
Sagie
Author by

Sagie

Updated on June 06, 2022

Comments

  • Sagie
    Sagie almost 2 years

    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.

  • Sagie
    Sagie about 7 years
    Thanks that what I looked for!
  • ccheney
    ccheney almost 6 years
    or with an arrow function: expect(() => myFunc(arg1, arg2, arg3)).not.toThrow();