Chuyển đến nội dung
Skillpedia
Kiểm thử40k · #7391

react19-test-patterns

Skill này làm gì

Hướng dẫn này giúp bạn cập nhật test mã để chạy với phiên bản React mới (React 19). Nó chỉ cách sửa các phần mã test cụ thể đã ngừng hoạt động hoặc đã thay đổi.

Khi nào nên dùng

  • ✓Khi bạn cập nhật dự án lên React 19.
  • ✓Khi các test dùng React 18 bắt đầu lỗi sau khi nâng cấp.
  • ✓Khi bạn thấy thông báo lỗi về “act” hoặc “Simulate” trong test.

Liên kết skill

https://github.com/github/awesome-copilot/blob/main/skills/react19-test-patterns/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/react19-test-patterns/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm thế nào để cập nhật test cho React 19?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Hướng dẫn cung cấp ví dụ “trước” và “sau” cho từng thay đổi cần thiết. Bạn thay mã cũ bằng mã mới như trong ví dụ để sửa test.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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:

  1. act import fix first, it unblocks everything else
  2. Simulate → fireEvent fix immediately after act
  3. Full react-dom/test-utils cleanup remove remaining imports
  4. StrictMode call counts measure actual, don't guess
  5. Async act wrapping for remaining "not wrapped in act" warnings
  6. 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
Câu hỏi thường gặp

“act” là gì?

“act” là hàm dùng trong test để đảm bảo các cập nhật giao diện xảy ra trước khi bạn kiểm tra kết quả. Trong React 19, cách import “act” đã thay đổi.

“Simulate” là gì?

“Simulate” là cách giả lập hành động người dùng (như nhấp chuột hoặc gõ phím) trong test. Trong React 19, nó được thay bằng “fireEvent”.

Vì sao test của tôi gọi hàm hai lần trong React 19?

Trong React 19, “StrictMode” (chế độ giúp tìm lỗi) không còn gọi hai lần một số hàm (như `useEffect`) khi phát triển. Bạn cần điều chỉnh số lần gọi trong test để phản ánh điều này.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
40k
Vị trí xếp hạng
#7391
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự