react19-test-patterns
它能做什么
这份指南可以帮你更新代码测试,使其适配新版本的 React(React 19)。它会告诉你如何修复测试代码中已经失效或发生变化的特定部分。
适用场景
- ✓当你要把项目升级到 React 19 时。
- ✓当你之前基于 React 18 的测试在升级后开始失败时。
- ✓当你在测试中看到关于 `act` 或 `Simulate` 的错误信息时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/react19-test-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何为 React 19 更新我的测试?”
每周 5 个新技能,1 分钟读完。
工作原理
指南会为每一处必要的修改提供“修改前”和“修改后”的示例。你只需把旧代码替换成示例中的新代码,就能修复测试。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
React 19 Test Migration Patterns
Reference for all test file migrations required by React 19.
Priority Order
Fix test files in this order; each layer depends on the previous:
actimport fix first, it unblocks everything elseSimulate→fireEventfix immediately after act- Full react-dom/test-utils cleanup remove remaining imports
- StrictMode call counts measure actual, don't guess
- Async act wrapping for remaining "not wrapped in act" warnings
- Custom render helper verify once per codebase, not per test
1. act() Import Fix
// Before REMOVED in React 19:
import { act } from 'react-dom/test-utils';
// After:
import { act } from 'react';
If mixed with other test-utils imports:
// Before:
import { act, Simulate, renderIntoDocument } from 'react-dom/test-utils';
// After split the imports:
import { act } from 'react';
import { fireEvent, render } from '@testing-library/react'; // replaces Simulate + renderIntoDocument
2. Simulate → fireEvent
// Before Simulate REMOVED in React 19:
import { Simulate } from 'react-dom/test-utils';
Simulate.click(element);
Simulate.change(input, { target: { value: 'hello' } });
Simulate.submit(form);
Simulate.keyDown(element, { key: 'Enter', keyCode: 13 });
// After:
import { fireEvent } from '@testing-library/react';
fireEvent.click(element);
fireEvent.change(input, { target: { value: 'hello' } });
fireEvent.submit(form);
fireEvent.keyDown(element, { key: 'Enter', keyCode: 13 });
3. react-dom/test-utils Full API Map
| Old (react-dom/test-utils) | New location |
|---|---|
act |
import { act } from 'react' |
Simulate |
fireEvent from @testing-library/react |
renderIntoDocument |
render from @testing-library/react |
findRenderedDOMComponentWithTag |
getByRole, getByTestId from RTL |
findRenderedDOMComponentWithClass |
getByRole or container.querySelector |
scryRenderedDOMComponentsWithTag |
getAllByRole from RTL |
isElement, isCompositeComponent |
Remove not needed with RTL |
isDOMComponent |
Remove |
4. StrictMode Call Count Fixes
React 19 StrictMode no longer double-invokes useEffect in development. Spy assertions counting effect calls must be updated.
Strategy always measure, never guess:
# Run the failing test, read the actual count from the error:
npm test -- --watchAll=false --testPathPattern="[filename]" --forceExit 2>&1 | grep -E "Expected|Received"
// Before (React 18 StrictMode effects ran twice):
expect(mockFn).toHaveBeenCalledTimes(2); // 1 call × 2 (strict double-invoke)
// After (React 19 StrictMode effects run once):
expect(mockFn).toHaveBeenCalledTimes(1);
// Render-phase calls (component body) still double-invoked in React 19 StrictMode:
expect(renderSpy).toHaveBeenCalledTimes(2); // stays at 2 for render body calls
常见问题
什么是 `act`?
`act` 是测试中使用的一个函数,用来确保界面更新完成后再检查结果。在 React 19 中,导入 `act` 的方式发生了变化。
什么是 `Simulate`?
`Simulate` 曾用于在测试中模拟用户操作,比如点击或输入。在 React 19 中,它已被 `fireEvent` 取代。
为什么我的测试在 React 19 中会把函数调用两次?
在 React 19 中,`StrictMode`(一种帮助发现问题的模式)在开发环境下不再对某些函数(比如 `useEffect`)进行两次调用。你需要相应调整测试中的调用次数断言。
详情
作者的原始描述
Provides before/after patterns for migrating test files to React 19 compatibility, including act() imports, Simulate removal, and StrictMode call count changes.
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7391
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。