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



我有以下代码:

<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>

那它怎么找不到呢?

a[href]的默认角色为link…

来源您需要在<a>中添加一个href