reactjs - act(() => promise.resolve()) 在我的测试中到底做了什么
问题描述
我有一些具有同步代码的测试,因为它使用获取响应。当我没有这行“await act(() => Promise.resolve());”时,我会收到此错误。但是一旦我添加了这一行,这个错误就会消失,我的测试会按照它的假设运行。我知道这不是误报,因为我检查了它登录到我的终端的快照。但我对这一行感到困惑。它如何针对我的应用程序中的承诺,或者它只是另一种编写强制 settimeout 函数的方式。
错误
console.error
Warning: An update to authenticator 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 */
测试
test('when click with no inputs will display a error message in the DOM', async () => {
const { getByText } = render(<CreateForm />, {wrapper: AuthProvider});
await act(() => Promise.resolve());
fireEvent.click(getByText('Create'));
expect(getByText('Please fill in all fields')).toBeInTheDocument();
});
解决方案
推荐阅读
- node.js - 将节点 api 连接到 cosmosdb
- c# - 没有 CleintSecret 的 OAuth EWS 身份验证
- python - 如何制作 3-D 球体簇?
- powershell - 如何使用powershell从文件名的开头删除空格
- excel - 将字符串作为 VBA 中子的参数传递?
- vulkan - Vulkan 没有 VK_KHR_ray_tracing 扩展
- javascript - 在混合的 javascript 2D 数组中,如何选择具有最大数字的元素?
- c# - “对象”不包含 Xamarin Forms 的定义
- asp.net-core - 需要在 IIS express 上扩展 url 长度,我收到 400 BadRequest
- javascript - 从列表中读取下拉列表