使用 Jest + React 测试库,测试中 Provider 的更新未包含在 act(...) 中

An update to Provider inside a test was not wrapped in act(...) with Jest + React Testing Library

我正在尝试使用 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

感谢您的帮助。

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

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

我必须等待一个元素

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