describe nhóm các test liên quan thành suite, it/test định nghĩa từng test case, expect tạo assertion với matchers.
- describe — nhóm test, nest được nhiều cấp; tên test nên đọc như documentation:
it('should return 404 when user not found'). - Matchers thường dùng —
toBe(=== cho primitives),toEqual(so sánh sâu cho object/array),toMatchObject(khớp một phần),toContain,toHaveLength,toBeNull/toBeTruthy/toBeFalsy,toThrow,toHaveBeenCalledWith(mock assertion). - Async —
await expect(promise).resolves.toBe(value)hoặcawait expect(promise).rejects.toThrow(). - Hooks —
beforeEach/afterEachchạy quanh mỗi test (tạo test data, reset mock, cleanup);beforeAll/afterAllchạy một lần cho cả block (setup tốn kém: test server, DB connection). - Biến thể —
test.only/describe.onlychỉ chạy test được chọn (tiện khi debug, không commit);test.skipbỏ qua tạm thời;test.eachcho parameterized tests với nhiều bộ input/output.