【问题标题】:Error: Unable to find an element with the role "alert" (react-testing-library example)错误:无法找到角色为“alert”的元素(react-testing-library 示例)
【发布时间】:2019-07-31 04:48:32
【问题描述】:

我正在使用来自 react-testing-library 的股票代码框示例,并且我添加了两个文件,Login.jsx__tests__/Login.js。这些文件直接从主 readme.md 上的 react-testing-library 示例复制而来。

但是,当测试套件运行时,我得到一个错误:

错误:无法找到具有“警报”角色的元素

当我对此进行调试时,html 输出似乎在任何 div 中都没有任何角色属性。我做错了什么?

代码沙盒:https://codesandbox.io/s/react-testing-library-examples-dh1e7

【问题讨论】:

  • 代码对我来说很有意义。它在本地工作吗?
  • 它没有。我在本地遇到同样的错误。不完全为什么,因为这是主文档页面中的股票示例。

标签: react-testing-library


【解决方案1】:

可能为时已晚。但无论如何。

您的组件没有设置它稍后依赖的 success 字段:

{state.success ? (
  <div role="alert">Congrats! You're signed in!</div>
) : null}

应该是state.resolved

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 2019-07-02
    • 2021-04-07
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2022-08-06
    • 2020-02-10
    相关资源
    最近更新 更多