Home > Net >  React Testing Library - can't find anchor element by role
React Testing Library - can't find anchor element by role

Time:10-27

I have the following code:

    <div className="button">
      <a href={SOME_URL}>
      ...
      ...
      </a>
    </div>

And in my test, if i try to do:

const link = screen.getByRole('link');

I'm getting the following error:

TestingLibraryElementError: Unable to find an accessible element with the role "link"

Running screen.debug(), i'm getting the following:

          <div
            class="button"
          >
            <a>

So how come it can't find it?

CodePudding user response:

The a[href] has a default role of link...

source

You'll need to add an href to the <a>.

  • Related