我有一个组件库,我正在使用Jest和react-testing-library编写单元测试。基于某些道具或事件,我想验证某些元素没有被渲染。
如果未找到元素,则在react-testing-library中抛出和错误,导致在expect函数触发之前测试失败。
如何使用react-testing-library测试不存在的东西?
我有一个组件库,我正在使用Jest和react-testing-library编写单元测试。基于某些道具或事件,我想验证某些元素没有被渲染。
如果未找到元素,则在react-testing-library中抛出和错误,导致在expect函数触发之前测试失败。
如何使用react-testing-library测试不存在的东西?
当前回答
对我来说(如果你想使用getbytestd):
expect(() => getByTestId('time-label')).toThrow()
其他回答
对我来说(如果你想使用getbytestd):
expect(() => getByTestId('time-label')).toThrow()
您可以使用react-native-testing-library "getAllByType",然后检查该组件是否为空。优点是不必设置testd,也应该与第三方组件一起工作
it('should contain Customer component', () => {
const component = render(<Details/>);
const customerComponent = component.getAllByType(Customer);
expect(customerComponent).not.toBeNull();
});
我最近为一个笑话黄瓜项目写了一个检查元素可见性的方法。
希望对大家有用。
public async checknotVisibility(page:Page,location:string) :Promise<void>
{
const element = await page.waitForSelector(location);
expect(element).not.toBe(location);
}
希望这对你有所帮助
该表显示了函数错误的原因/时间
哪些函数是异步的
函数的return语句是什么
const submitButton = screen.queryByText('submit')
expect(submitButton).toBeNull() // it doesn't exist
expect(submitButton).not.toBeNull() // it exist