React 测试库 - 无法按角色找到锚点元素

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

我有以下代码:

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

在我的测试中,如果我尝试这样做:

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

我收到以下错误:

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

运行 screen.debug(),我得到以下信息:

          <div
            class="button"
          >
            <a>

怎么找不到呢?

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

source

您需要将 href 添加到 <a>