testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.25k stars 460 forks source link

`alt` attribute can't be empty #1313

Closed Tasiobg closed 1 month ago

Tasiobg commented 1 month ago

Since this update https://github.com/testing-library/dom-testing-library/pull/1241 the alt attribute for the img role can't be empty. My understanding is that the alt attribute should be present but in some cases like decorative images it should be empty, some info here

Is this an issue or an intended change?

Works

it(`testcase`, async () => {
    render(
      <div>
        <img src="url" alt="alt text" />
      </div>,
    );

    expect(screen.getByRole('img')).toBeInTheDocument();
  });

Fail

it(`testcase`, async () => {
    render(
      <div>
        <img src="url" alt="" />
      </div>,
    );

    expect(screen.getByRole('img')).toBeInTheDocument();
  });
MatanBobi commented 1 month ago

Hi @Tasiobg :) An image with an empty alt attribute has a role of none or presentation (based on the ARIA spec):

image

So this works as expected. Thanks for the report.