Cơ BảnTesting iconTesting

describe, it/test, expect trong Jest được dùng như thế nào?

describe nhóm related tests thành suite (có thể nest nhiều cấp). it/test định nghĩa test case — tên test nên đọc như documentation: it('should return 404 when user not found'). expect tạo assertion với matchers: toBe (strict ===, dùng cho primitives), toEqual (deep equality, dùng cho objects/arrays), toMatchObject (partial object match — chỉ check specified keys), toContain (array/string contains), toHaveLength, toBeNull/toBeUndefined/toBeTruthy/toBeFalsy, toThrow (function throw error), toHaveBeenCalledWith (mock assertion).

Async matchers: await expect(promise).resolves.toBe(value) hoặc await expect(promise).rejects.toThrow(). beforeEach/afterEach chạy trước/sau mỗi test trong describe block — dùng cho setup/teardown (tạo test data, cleanup database, reset mocks). beforeAll/afterAll chạy một lần cho cả describe block — dùng cho expensive setup (start test server, database connection). test.only/describe.only: chỉ chạy selected tests — hữu ích khi debug nhưng đừng commit. test.skip: bỏ qua test tạm thời với reason. test.each: parameterized tests với multiple input/output combinations.

Xem toàn bộ Testing cùng filter theo level & chủ đề con.

Mở danh sách Testing