开玩笑拒绝功能失败

时间:2020-09-27 15:13:27

标签: reactjs jestjs

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 | });

1 个答案:

答案 0 :(得分:2)

我认为您想要await expect(fetchDataPromise()).rejects.toEqual("error");

这将断言拒绝的值是“错误”。 toThrow方法断言已抛出Error,即throw new Error('fart bubble')