【问题标题】:An update to Provider inside a test was not wrapped in act(...) with Jest + React Testing Library使用 Jest + React 测试库对测试中 Provider 的更新未包含在 act(...) 中
【发布时间】:2020-04-17 13:34:51
【问题描述】:

我正在尝试使用 React 测试库和 Jest 测试我的 React 应用程序,但我总是遇到以下错误。

An update to StoreProvider inside a test was not wrapped in act(...)

我在测试中尝试了很多方法,例如 put act() 函数,但无法删除警告并获得正确的结果。

这是一个演示问题的简化项目: https://codesandbox.io/s/adoring-haslett-6rq34?file=/src/PageContent.test.js

感谢您的帮助。

【问题讨论】:

    标签: javascript reactjs testing jestjs react-testing-library


    【解决方案1】:

    这是一个可行的解决方案。

    https://github.com/testing-library/react-testing-library/issues/641#event-3244842083

    我必须等待一个元素

    const { findByText } = render(<App />)
    expect(await findByText('STATUS = 0')).toBeInTheDocument();
    

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 2019-08-06
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多