Jest - how to test if a component does not exist?

后端 未结 5 2016
离开以前
离开以前 2021-02-04 23:15

How do I check if a component is not present, i.e. that a specific component has not been rendered?

5条回答
  •  情深已故
    2021-02-05 00:13

    If you're using react-testing-library (I know the OP wasn't but I found this question via web search) then this will work:

    expect(component.queryByText("Text I care about")).not.toBeInTheDocument();
    

    You can query by Text, Role, and several others. See docs for more info.

    Note: queryBy* will return null if it is not found. If you use getBy* then it will error out for elements not found.

提交回复
热议问题