Jest + react-testing-library:警告更新未包含在 act() 中

iam*_*wtk 13 javascript reactjs jestjs react-testing-library

我正在用 react-testing-library 测试我的组件,并且测试运行良好。我只是无法摆脱这个警告,fireEvent 应该包装在开箱即用的行为中,但我试图再次包装它,但它没有帮助。

这是我的测试用例。

it.only("should start file upload if file is added to the field", async () => {
    jest.useFakeTimers();
    const { getByTestId } = wrapper;
    const file = new File(["filefilefile"], "videoFile.mxf");

    const fileInput = getByTestId("drop-zone").querySelector(
      "input[type='file']"
    );

    fireEvent.change(fileInput, { target: { files: [file] } });

    act(() => {
      jest.runAllTimers();
    });

    await wait(() => {
      expect(
        initialProps.uploadItemVideoFileConnect.calledWith(file, 123)
      ).toBe(true);
    });
  });
Run Code Online (Sandbox Code Playgroud)

这是警告

Warning: An update to UploadButtonGridComponent inside a test was not wrapped in act(...).

    When testing, code that causes React state updates should be wrapped into act(...):

    act(() => {
      /* fire events that update state */
    });
    /* assert on the output */
Run Code Online (Sandbox Code Playgroud)

chi*_*imo 16

所以这很难总结,但我会尝试。

act警告只是告诉您,您的功能组件中发生了一些您没有测试的事情。

假设我们正在渲染一个待办事项列表,如下所示

    <ul>
      {loading ? (
        <p>Fetching todos</p>
      ) : (
        <>
          {appData.todoList.slice(0, 15).map((item) => {
            const { id, title } = item;
            return (
              <li key={id} data-testid={id}>
                <Link to={`/item/${id}`}>{title}</Link>
              </li>
            );
          })}
        </>
      )}
    </ul>
Run Code Online (Sandbox Code Playgroud)

下面的测试用例将引发act警告

import { waitFor, screen, waitForElementToBeRemoved } from "@testing-library/react";

it("Renders <TodoList /> component", async () => {
    render(<TodoList />);
    await waitFor(() => expect(axios.get).toHaveBeenCalledTimes(1));
    await waitForElementToBeRemoved(() => screen.getByText(/Fetching todos/i));

    expect(axios.get).toHaveBeenCalledTimes(1);
    todos.slice(0, 15).forEach((td) => {
      expect(screen.getByText(td.title)).toBeInTheDocument();
    });
  });
Run Code Online (Sandbox Code Playgroud)

await但如果你像这样重新排列行

await waitForElementToBeRemoved(() => screen.getByText(/Fetching todos/i));
await waitFor(() => expect(axios.get).toHaveBeenCalledTimes(1));
Run Code Online (Sandbox Code Playgroud)

警告act消失。这是有道理的。您需要确保您的用户不再看到加载指示器。

还有其他情况,所以请继续阅读肯特·多兹 (kent Dodds) 的这篇文章。

https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning

  • 我不知道这是否对我有用,但这是一个很好的答案!“你必须确保你的用户不再看到加载指示器。” &lt;-- 是的 (3认同)

Sha*_*han 14

该问题是由 Component 内部的许多更新引起的。

我得到了同样的,这就是我解决问题的方法。

await act( async () => {
 fireEvent.change(fileInput, { target: { files: [file] } });
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您有两个彼此相邻的 fireEvent,则需要两个“act()”,每个一个。 (2认同)
  • 该解决方案解决了我在测试输入 Formik 表单的组件时遇到的问题。我收到此警告:警告:测试中对 Formik 的更新未包含在 act(...) (2认同)

jar*_*dsk 7

源代码中fireEvent已经包含在act().

该问题可能与此问题有关,其中异步逻辑(例如 a useEffect)正在触发 fireEvent 之外的状态更改:

https://github.com/kentcdodds/react-testing-library/issues/281

(如果没有看到您的组件实现,就很难确定这是否正是您的情况。)

显然有计划在未来的版本中包含异步处理,所以这不会成为问题。