如何允许 screen.getByText() 在笑话+反应测试中失败

Our*_*ors 4 javascript testing automated-tests reactjs ts-jest

我有一个测试用例。该项目正在使用@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();
  });
Run Code Online (Sandbox Code Playgroud)

它不允许我运行expect(loaderElementText).not.toBeInTheDocument();带有以下错误的行:

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.
Run Code Online (Sandbox Code Playgroud)

反向测试用例按loading={true}预期工作。

我已经无计可施了。查询文档中的元素并期望找不到该元素的语法是什么?

Our*_*ors 10

为那些好奇的人发布答案: use screen.queryByText(),它在失败时返回 null 而不是抛出运行时错误。