How do you test for the non-existence of an element using jest and react-testing-library?

后端 未结 6 695
旧时难觅i
旧时难觅i 2021-01-30 15:36

I have a component library that I\'m writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren\'t

6条回答
  •  终归单人心
    2021-01-30 16:04

    Another solution: you could also use a try/catch block

    try {
        // if the element is found, the following expect will fail the test
        expect(getByTestId('your-test-id')).not.toBeVisible();
    } catch (error) {
        // otherwise, the expect will throw, and the following expect will pass the test
        expect(true).toBeTruthy();
    }
    

提交回复
热议问题