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.
describe groups related tests into a suite (can be nested multiple levels). it/test defines a test case — the test name should read like documentation: it('should return 404 when user not found'). expect creates assertions with matchers: toBe (strict ===, used for primitives), toEqual (deep equality, used for objects/arrays), toMatchObject (partial object match — only checks specified keys), toContain (array/string contains), toHaveLength, toBeNull/toBeUndefined/toBeTruthy/toBeFalsy, toThrow (function throws an error), toHaveBeenCalledWith (mock assertion).
Async matchers: await expect(promise).resolves.toBe(value) or await expect(promise).rejects.toThrow(). beforeEach/afterEach runs before/after each test in the describe block — used for setup/teardown (creating test data, cleaning the database, resetting mocks). beforeAll/afterAll runs once for the entire describe block — used for expensive setup (start a test server, database connection). test.only/describe.only: runs only the selected tests — useful for debugging but do not commit. test.skip: temporarily skips a test with a reason. test.each: parameterized tests with multiple input/output combinations.