小编Dav*_*aun的帖子

为什么使用 findBy 时测试失败而使用 waitfor 时测试成功?

使用 react-testing-library,以下测试有效:

it( 'renders popular search terms, with links to search urls', () => {
      waitFor(() => {
        const popularSearch = screen.getByText( Copywriting.buyer.shop.popularSearch, {}, { timeout: 5000 })
        expect( popularSearch ).toBeInTheDocument()

        const popularSearchPills = screen.findAllByTestId( 'pill' )
        expect( popularSearchPills.length ).toBeGreaterThan( 0 )

        const hrefs = popularSearchPills.map( pill => pill.href )
        expect( hrefs.filter( urlIsValidAndNotBaseUrl )).not.toHaveLength( 0 )
      })
    })
Run Code Online (Sandbox Code Playgroud)

但是以下测试曾经有效,但现在失败了:

it( 'renders popular search terms, with links to search urls', async () => {
      const popularSearch = await screen.findByText( Copywriting.buyer.shop.popularSearch ) …
Run Code Online (Sandbox Code Playgroud)

testing reactjs redux-saga react-redux react-testing-library

6
推荐指数
1
解决办法
1876
查看次数