Nick
Nick

Reputation: 2928

Jest rejects function fails

const fetchDataPromise = (cal) => Promise.reject("error");    

test("the fetch fails with an error2", async () => {
      await expect(fetchDataPromise()).rejects.toThrow("error");
    });
Expected substring: "error"

Received function did not throw

  25 | 
  26 | test("the fetch fails with an error2", async () => {
> 27 |   await expect(fetchDataPromise()).rejects.toThrow("error");
     |                                            ^
  28 | });

Upvotes: 0

Views: 229

Answers (1)

old greg
old greg

Reputation: 897

I think you want await expect(fetchDataPromise()).rejects.toEqual("error");

This will assert the rejected value is 'error'. the toThrow method asserts an Error was thrown, ie throw new Error('fart bubble').

Upvotes: 2

Related Questions