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に貼り付けるだけ。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」とは何ですか?
テストで使われる関数で、結果を確認する前にUIの更新が確実に完了するようにするためのものです。React 19では、「act」のインポート方法が変わりました。
「Simulate」とは何ですか?
テストでクリックや入力などのユーザー操作を再現するための方法でした。React 19では「fireEvent」に置き換えられています。
React 19でテストの関数が2回呼ばれるのはなぜですか?
React 19では、「StrictMode」(バグを見つけやすくするためのモード)が、開発時に`useEffect`などの特定の関数を2回呼び出さなくなりました。そのため、テストで呼び出し回数の期待値を調整する必要があります。
詳細
作者による元の説明
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プロジェクトの評価ポリシーの策定や適用を支援できるようにします。プロジェクトの評価方法を標準化し、コンプライアンスを確保するのに役立ちます。
似ているスキル
test-driven-development
311k機能や修正を作る前に、まず失敗するテストを書き、それが失敗するのを確認してから、合格させる最小のコードを書くようエージェントに教えます。
verification-before-completion
311kタスク、修正、ビルドが完了した、または合格したと主張する前に、実際のチェックを実行し、その出力を読むようエージェントに教えます。
migrate-to-shoehorn
310kテストファイル内の危険な型キャストを、shoehornライブラリに置き換え、テストが部分的なデータを使いつつ型チェックを通るようにします。