在我的简单 React/ReactDOM/Enzyme 单元测试中,我收到来自 ReactDOM 的警告,关于将任何突变包装到act(). 如果我的测试仍然通过,为什么我需要这样做?我有 50 个左右的 React 组件,它们都使用钩子、自定义钩子等。我从不换行act(),它们都通过了。
我可以禁用此警告吗?我不想无缘无故地添加额外的代码。
警告:
Warning: An update to MyComponent inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at [redacted]
Run Code Online (Sandbox Code Playgroud)
我的测试:
const App …Run Code Online (Sandbox Code Playgroud)