测试内部没有将null更新包装在act(...)中

时间:2020-09-07 12:46:23

标签: reactjs testing react-testing-library

当我运行这些Test时,我遇到了act(() => {问题。

如果我评论测试2可以正常工作,那么我不确定这个问题是在测试2还是在测试1中?

在大多数情况下,我在Google上看到的人们都在使用模拟函数,然后将其包装在act中,但是在我的特殊情况下,我猜不使用函数。

所有测试都通过了,只是抛出了错误。所以我想我只是没有将act函数正确放置在需要的地方。

describe('Test', () => {
    it('Test 1', async () => {
        const { container } = render(<TestApp />)
        expect(await axe(container)).toHaveNoViolations()
    })

    it('Test 2', () => {
        const { getByTestId } = render(<TestApp />)
        const input = getByTestId(selectors.Input)
        user.type(input, '1231')
        expect(input).toHaveValue('1231')
    })
})
  console.error
    Warning: An update to null 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 
        in Unknown
        in ThemeProvider (created by ThemeProvider)
        in ThemeProvider (created by withProviders)
        in withProviders

0 个答案:

没有答案