admin管理员组文章数量:1289557
I have a testcase. This project is using @testing-library/react
it('renders content if loading is false', async () => {
await waitFor(() => {
render(<Loader loading={false}><span>foo</span></Loader>);
});
const loaderElementText = screen.getByText('Loading');
expect(loaderElementText).not.toBeInTheDocument();
const contentText = screen.getByText('foo');
expect(contentText).toBeInTheDocument();
});
It does not allow me to run the line expect(loaderElementText).not.toBeInTheDocument();
with the following error:
TestingLibraryElementError: Unable to find an element with the text: Loading. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
The inverse testcase, with loading={true}
works as expected.
I'm at my wits end. What is the syntax to query the document for an element, and expect that the element is not found?
I have a testcase. This project is using @testing-library/react
it('renders content if loading is false', async () => {
await waitFor(() => {
render(<Loader loading={false}><span>foo</span></Loader>);
});
const loaderElementText = screen.getByText('Loading');
expect(loaderElementText).not.toBeInTheDocument();
const contentText = screen.getByText('foo');
expect(contentText).toBeInTheDocument();
});
It does not allow me to run the line expect(loaderElementText).not.toBeInTheDocument();
with the following error:
TestingLibraryElementError: Unable to find an element with the text: Loading. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
The inverse testcase, with loading={true}
works as expected.
I'm at my wits end. What is the syntax to query the document for an element, and expect that the element is not found?
Share Improve this question asked Jul 7, 2021 at 17:14 Our_BenefactorsOur_Benefactors 3,5393 gold badges23 silver badges29 bronze badges1 Answer
Reset to default 12Posting the answer for those curious: use screen.queryByText()
, which returns null on failure instead of throwing a runtime error.
本文标签: javascriptHow to allow screengetByText() to fail in jestreact testStack Overflow
版权声明:本文标题:javascript - How to allow screen.getByText() to fail in jest + react test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741411023a2377235.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论