Testing Next.js Image Component

1 min read.
Tags: javascriptnextjsunit-testjest

When testing the Next.js Image component, you can ensure the correct image source is being rendered by using assertions. Here’s an example of how you might test it:

expect(
  screen.getByTestId('imageId').getAttribute('src')
).toContain(encodeURIComponent(imageSrc));

This assertion checks if the src attribute of the image component with the specified test ID (imageId) contains the encoded image source (imageSrc). This test helps verify that the correct image is being displayed by the Next.js Image component.

Related Posts
Latest Posts