Haarlem, Netherlands

This is collection of common scenarios in testing react components using react testing library.


A react component is to being unit tested. The presence of an element after render is to be checked. In the following example, button with premium features is rendered conditionally.


Test cases:

  1. Check for presence

We can use the `getByTestId` method here.

2. Check for absence

When we use `getByTestId` to find an element and if it doesn’t exist, `getByTestId` yells out an error which cannot be used to assert the absence.

So, what can be used here is `queryByTestId`.

Would be glad to know it helped you.