Search for a specific text inside component in Jest test cases

CodeyMaze
Mar 20, 2023

--

Here’s an example of a Jest test case that searches for a specific text within a rendered component:

In this example, we import the render function from the @testing-library/react package and our component MyComponent. We then use the getByText function from the render output to search for the text 'Hello, world!' within the rendered component.

Finally, we use the Jest expect function to assert that the specific text is present in the rendered component by using the toBeInTheDocument matcher.

--

--

CodeyMaze
CodeyMaze

Written by CodeyMaze

Crafting Solutions Through Code & Words https://codeymaze.com Feel free to follow me :)

No responses yet