测试subMenu组件时wait无法导入

来源:6-10 完美组件 - SubMenu 组件添加测试

Lucky雷

2022-09-24

it('should show dropdown items when hover on subMenu', async () => {
        // queryByText返回HTMLElement 和 null  getByText
        expect(wrapper.queryByText('test11')).not.toBeVisible()
        const dropdownElement = wrapper.getByText('test1')
        fireEvent.mouseEnter(dropdownElement)
        // expect(wrapper.queryByText('test11')).toBeVisible()
        // 断言类型不会等300ms后再跑  这个时候没有显示出来自然出现报错
        await waitFor(() => {
            // 添加断言
            expect(wrapper.queryByText('test11')).toBeVisible()
        })
        fireEvent.click(wrapper.getByText('test11'))
        expect(testProps.onSelect).toHaveBeenCalledWith('3-0')
        fireEvent.mouseLeave(dropdownElement)
        await waitFor(() => {
            expect(wrapper.queryByText('test11')).not.toBeVisible()
        })
    })

老师这里的wait 跟waitFor是一样的作用吧
使用waitfor的原因是 我使用wait时测试中一直提示wait错误 且import导入的时候查询不到wait
图片描述

写回答

1回答

张轩

2022-09-24

同学你好

说的没错 新版的 react-testing-library 已经用 waitFor 代替了 wait,所以后面的都使用 waitFor 就可以了

2
0

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2122 学习 · 959 问题

查看课程