首页 > 解决方案 > 反应事件处理程序中引发的期望/忽略错误

问题描述

我如何期望事件处理程序中出现错误?

it("throws an error if [some cool condition]", () => {
    getCoolCheck().mockRejectedValue("You're not cool enough bro...");

    const screen = render(<CoolComponent />);
    const btn = screen.getByLabelText("Are you cool enough?");

    fireEvent.click(btn);
    
    // this throws an intended error... how do I check it?
});


function CoolComponent() {
    <button onClick={handleCoolCheck}>Are you cool enough?</button>

    async function handleCoolCheck() {
        try {
            await getCoolCheck();

            alert("So cool!")
        } catch (e) {
            alert(`error occurred while cool checking: ${e}`);

            throw e;
        }
    }
}

此外,这不是一个正常的错误,而是一个被拒绝的承诺。

标签: reactjstypescripttesting

解决方案


推荐阅读