Vai trò của test
Test giúp phát hiện lỗi sớm, refactor an toàn và làm tài liệu hành vi. Unit test kiểm tra từng hàm/module cô lập; integration test kiểm tra nhiều phần kết hợp (API + DB, component + store).
Jest cơ bản
Jest là test runner phổ biến cho JavaScript/TypeScript. describe, it, expect. Mock với jest.fn(), jest.mock(). Setup/teardown với beforeEach, afterEach. Chạy test song song, coverage với --coverage.
Test React component
Dùng React Testing Library: render theo cách user tương tác (query by role, label), fire event, assert kết quả. Tránh test implementation detail. Mock API với MSW (Mock Service Worker) để integration test gần thật.
Bình luận (0)
Đang tải bình luận...