Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`waitFor` utils are not wrapped in `act` so some state updates are not applied to the DOM

未关闭
#1,416 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript, react
领域
frontend, testing

调研方向

从传递给 DOM Testing Library 的 async wrapper 以及 App.test.tsx 中展示的 waitForElementToBeRemoved 用法开始。查看移除了直接 act 调用的关联 commit,然后使用 StackBlitz 复现来验证最终的状态更新是否会在没有显式 async act 的情况下应用到 DOM。

由索引模型根据 Issue 内容生成。

描述

  • @testing-library/react version: 16.3.0
  • Testing Framework and version: jest 30.1.3
  • DOM Environment: jsdom 26.1.0
Relevant code or config:
  test('it loads', async () => {
    render(<App />);

    await waitForElementToBeRemoved(screen.queryByRole('progressbar'));
    // Commenting this out fails the test
    // await act(() => new Promise((r) => setTimeout(r, 0)));

    expect(screen.getByText('Loaded')).toBeDefined();
  });
What you did:

Upgrading to React 18 along with react testing library v14, I found that some of my tests were now failing unless I add an explicit async act.

Digging into it, it seems to be this change to the async wrapper passed to the dom testing library where it no longer directly calls act, so state updates from the final iteration of the waitFor are not flushed to the DOM.

It looks like it only flushes the microtask queue, which won't give react an opportunity to process this state update.

Reproduction:

I'm using React 18 and RTL 14 but I've reproduced it on React 19 and RTL 16.

https://stackblitz.com/edit/rtl-template-4a3jt9jx?file=src%2FApp.test.tsx&view=editor

Suggested solution:

I don't have enough knowledge of the codebase to understand why the call to act was removed.

主要语言
JavaScript
星标
19.7k
派生
1.2k
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

testing-library/react-testing-library 的其他 Issue

查看 testing-library/react-testing-library 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。