首页 > 解决方案 > 开玩笑拒绝功能失败

问题描述

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

标签: reactjsjestjs

解决方案


我想你想要await expect(fetchDataPromise()).rejects.toEqual("error");

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


推荐阅读