describe('<CustomTooltip />', () => {
it('should show tooltip text', async () => {
const { container, unmount } = render(<CustomTooltip text='Tooltip Text' />)
userEvent.hover(container.querySelector('svg'))
await screen.findByText('Tooltip Text')
screen.debug()
unmount() // ?? is it necessary to call unmount after each test cases?
})
it('check if there is an mounted component', () => {
screen.debug()
})
})是否有必要在每个测试用例之后调用unmount?因为我在useEffect组件中添加了CustomTooltip,并在第二个测试用例之前记录了卸载。甚至第二个测试用例screen.debug输出也是<body />。
useEffect(() => {
console.log('Mounted')
return () => console.log('Unmounted')
}, [])我之所以这样问是因为在每个测试用例之后,我在unmount组件的测试用例中看到了一个unmount的自定义实现,我很想知道这是否真的很重要。
let lastMount = null;
const _render = (...args) => {
lastMount = render(...args);
return lastMount;
};
afterEach(() => {
if (lastMount)
lastMount.unmount();
lastMount = null;
});发布于 2022-05-28 19:02:56
这取决于您正在使用的测试框架。例如,如果您正在使用jest,则不需要它。
以下是对此建议的参考https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-cleanup
很长一段时间以来,清理都是自动进行的(大多数主要测试框架都支持它),您不再需要担心它了,
https://stackoverflow.com/questions/72416736
复制相似问题