【问题标题】:How do i use a single render across multiple tests in React Testing Library.?如何在 React 测试库中的多个测试中使用单个渲染。?
【发布时间】:2020-07-09 19:46:39
【问题描述】:

在使用 Jest 和 React 测试库时,是否可以在多个测试中保持相同的 render()?我正在构建一个 Trivia 应用程序,并且有一个组件可以在用户完成测验时显示不同的问题。为了测试选择按钮、提交按钮的功能,并检查正确的屏幕是否在正确的时间显示,我需要在测验的不同阶段对相同的渲染组件执行测试。例如:

describe("Question Screen", () => {

    it("should render the first question when difficulty button is clicked", async () => {
        render(<TriviaBox/>);
        const btn = screen.getByRole("button", {name: /easy/i});
        fireEvent.click(btn);

        const heading = await screen.findByText("Question 1");
        expect(heading).toBeInTheDocument();
    });

    it("should display the next question when the current question is answered", async () => {
        render(<TriviaBox/>);
        const btn = screen.getByRole("button", {name: /easy/i});
        fireEvent.click(btn);

        const correctAnswer = await screen.findByRole("button", {name: /Nevada/i});
        const submit = await screen.findByRole("button", {name: /Submit/i});
        fireEvent.click(correctAnswer);
        fireEvent.click(submit);

        expect(wait screen.findByText("Question 2")).toBeInTheDocument();
        expect(wait screen.findByText("Which is the largest state?")).toBeInTheDocument();
        expect(wait screen.findAllByRole("radio")).toHaveLength(4);
        ...
    });
});

有没有办法保留第一个测试的相同渲染以用于第二个测试,而不是重新渲染相同的组件并再次遍历第一个问题以测试第二个问题?

【问题讨论】:

    标签: reactjs jestjs react-testing-library


    【解决方案1】:

    基本上,您需要禁用自动清理,因为它会在每次测试后卸载 React 树。 请参阅文档:https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup。 但在这种情况下,您应该注意手动调用 cleanup 以免影响下一个测试。

    这里是一个小的工作示例,说明如何通过导入“@testing-library/react/dont-cleanup-after-each”来做到这一点:

    import "@testing-library/react/dont-cleanup-after-each";
    import { render, screen, cleanup } from "@testing-library/react";
    
    function TestComponent() {
        return (
            <div>
                <p>First element</p>
                <p>Second element</p>
            </div>
        );
    }
    
    describe("TestComponent", () => {
        afterAll(() => {
            cleanup();
        });
        it("should contain `First element` text", () => {
            render(<TestComponent />);
            screen.getByText("First element");
        });
        it("should contain `Second element` text", () => {
            screen.getByText("Second element");
        });
    });
    

    【讨论】:

      【解决方案2】:

      一种方法是编写一个beforeAll 函数来初始化渲染。对于所有子测试,这只会初始化一次。

      describe("Question Screen", () => {
      
          beforeAll(() => {
              render(<TriviaBox/>);
          }) 
      
          it("should render the first question when difficulty button is clicked", async () => {
              const btn = screen.getByRole("button", {name: /easy/i});
              fireEvent.click(btn);
      
              const heading = await screen.findByText("Question 1");
              expect(heading).toBeInTheDocument();
          });
          ...
      });
      

      请参阅 JEST 文档https://jestjs.io/docs/en/setup-teardown#one-time-setup

      【讨论】:

      • 公平的建议,但如果没有额外的步骤就无法工作。默认情况下,RTLib 在每次测试后都有自动清理功能,需要禁用它才能使其工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      相关资源
      最近更新 更多