react18-enzyme-to-rtl
Skill này làm gì
Công cụ này giúp bạn cập nhật test mã. Nếu bạn từng dùng Enzyme, một công cụ cũ để test mã React, công cụ này giúp bạn chuyển sang React Testing Library, một công cụ mới phù hợp hơn với React 18.
Khi nào nên dùng
- ✓Khi bạn cần cập nhật các test Enzyme cũ để chạy với React 18.
- ✓Khi test Enzyme không còn chạy sau khi bạn cập nhật dự án React.
- ✓Khi bạn muốn test mã React bằng cách tập trung vào những gì người dùng nhìn thấy và làm, thay vì các chi tiết bên trong.
Liên kết skill
https://github.com/github/awesome-copilot/blob/main/skills/react18-enzyme-to-rtl/SKILL.mdSao 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
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm thế nào để chuyển một test Enzyme kiểm tra state bên trong của component sang dùng React Testing Library?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Công cụ ánh xạ các hàm Enzyme cũ sang các hàm của React Testing Library mới. Nó chỉ cách viết lại test để tập trung vào hành vi người dùng nhìn thấy, thay vì xem các chi tiết bên trong của mã.
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 18 Enzyme → RTL Migration
Enzyme has no React 18 adapter and no React 18 support path. All Enzyme tests must be rewritten using React Testing Library.
The Philosophy Shift (Read This First)
Enzyme tests implementation. RTL tests behavior.
// Enzyme: tests that the component has the right internal state
expect(wrapper.state('count')).toBe(3);
expect(wrapper.instance().handleClick).toBeDefined();
expect(wrapper.find('Button').prop('disabled')).toBe(true);
// RTL: tests what the user actually sees and can do
expect(screen.getByText('Count: 3')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /submit/i })).toBeDisabled();
This is not a 1:1 translation. Enzyme tests that verify internal state or instance methods don't have RTL equivalents - because RTL intentionally doesn't expose internals. Rewrite the test to assert the visible outcome instead.
API Map
For complete before/after code for each Enzyme API, read:
references/enzyme-api-map.md- full mapping: shallow, mount, find, simulate, prop, state, instance, configurereferences/async-patterns.md- waitFor, findBy, act(), Apollo MockedProvider, loading states, error states
Core Rewrite Template
// Every Enzyme test rewrites to this shape:
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('does the thing', async () => {
// 1. Render (replaces shallow/mount)
render(<MyComponent prop="value" />);
// 2. Query (replaces wrapper.find())
const button = screen.getByRole('button', { name: /submit/i });
// 3. Interact (replaces simulate())
await userEvent.setup().click(button);
// 4. Assert on visible output (replaces wrapper.state() / wrapper.prop())
expect(screen.getByText('Submitted!')).toBeInTheDocument();
});
});
RTL Query Priority (use in this order)
getByRole- matches accessible roles (button, textbox, heading, checkbox, etc.)getByLabelText- form fields linked to labelsgetByPlaceholderText- input placeholdersgetByText- visible text contentgetByDisplayValue- current value of input/select/textareagetByAltText- image alt textgetByTitle- title attributegetByTestId-data-testidattribute (last resort)
Prefer getByRole over getByTestId. It tests accessibility too.
Wrapping with Providers
// Enzyme with context:
const wrapper = mount(
<ApolloProvider client={client}>
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
</ApolloProvider>
);
// RTL equivalent (use your project's customRender or wrap inline):
import { render } from '@testing-library/react';
render(
<MockedProvider mocks={mocks} addTypename={false}>
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
</MockedProvider>
);
// Or use the project's customRender helper if it wraps providers
Câu hỏi thường gặp
Vì sao tôi không dùng được Enzyme nữa?
Enzyme không còn được cập nhật và không hoạt động với các phiên bản React gần đây (như React 18). React Testing Library hiện là công cụ được khuyến nghị.
“Test hành vi” thay vì “test chi tiết triển khai” nghĩa là gì?
Test chi tiết triển khai (Enzyme) xem các chi tiết bên trong của mã. Test hành vi (React Testing Library) tập trung vào những gì người dùng cuối thấy trên màn hình và cách họ tương tác với ứng dụng.
Tất cả test Enzyme cũ có thể chuyển trực tiếp không?
Không hoàn toàn. Vì cách tiếp cận khác nhau (hành vi so với triển khai), một số test cũ kiểm tra chi tiết bên trong sẽ không có bản dịch trực tiếp. Bạn cần viết lại để kiểm tra kết quả nhìn thấy được.
Chi tiết
Mô tả gốc của tác giả
Provides exact Enzyme → React Testing Library migration patterns for React 18 upgrades. Use this skill whenever Enzyme tests need to be rewritten - shallow, mount, wrapper.find(), wrapper.simulate(), wrapper.prop(), wrapper.state(), wrapper.instance(), Enzyme configure/Adapter calls, or any test file that imports from enzyme. This skill covers the full API mapping and the philosophy shift from imp
repo: github/awesome-copilot
Độ phổ biến
- Sao GitHub
- 40k
- Vị trí xếp hạng
- #7385
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
acquire-codebase-knowledge
40kGiúp agent của bạn hiểu và ghi tài liệu cho một dự án mã nguồn có sẵn. Hữu ích khi đón thành viên mới hoặc nắm tổng quan về kiến trúc.
acreadiness-assess
40kGiúp agent của bạn đánh giá mức độ sẵn sàng cho AI của dự án và tạo báo cáo. Hữu ích để biết dự án đã sẵn sàng dùng công cụ AI đến đâu.
acreadiness-generate-instructions
40kGiúp agent của bạn tạo hướng dẫn cho công cụ AI dựa trên kết quả đánh giá. Hữu ích để cải thiện cách AI làm việc với mã và khắc phục các khoảng trống.
acreadiness-policy
40kGiúp agent của bạn xác định hoặc áp dụng chính sách đánh giá dự án bằng AI. Hữu ích để chuẩn hóa cách đánh giá dự án và đảm bảo tuân thủ.
Skill tương tự
test-driven-development
311kDạy agent của bạn viết kiểm thử thất bại trước, xem nó thất bại, rồi viết đoạn mã ngắn nhất để vượt qua trước khi làm bất kỳ tính năng hay bản sửa nào.
verification-before-completion
311kDạy agent của bạn chạy kiểm tra thực tế và đọc kết quả đầu ra trước khi khẳng định một việc, bản sửa hoặc bản build đã hoàn thành hay vượt qua.
migrate-to-shoehorn
310kThay các ép kiểu rủi ro trong tệp kiểm thử bằng thư viện shoehorn, để các bài kiểm thử dùng dữ liệu một phần vẫn qua được kiểm tra kiểu.