How to test background image styles with React testing library?

Sometimes, we want to test background image styles with React testing library.

In this article, we’ll look at how to test background image styles with React testing library.

How to test background image styles with React testing library?

To test background image styles with React testing library, we can use the toHaveStyle method.

For instance, we can use it by writing

expect(getByTestId('background')).toHaveStyle(`background-image: url(${props.image})`)

We get the component we want to test by its test ID with getByTestId('background').

Then we call toHaveStyle with the style string we want to check.

Conclusion

To test background image styles with React testing library, we can use the toHaveStyle method.