Code coverage đo phần trăm code được chạy bởi tests — generated bởi Istanbul (Jest), V8 (Vitest V8 provider).
- Metrics: Statement coverage (% statements executed — mỗi expression/assignment là statement); Branch coverage (% branches covered — mỗi if/else/ternary/&&/|| tạo 2 branches, phải test cả true và false case); Function coverage (% functions called ít nhất một lần); Line coverage (% lines executed — thường cao nhất, ít meaningful nhất).
- Branch coverage là metric quan trọng nhất — 100% statement nhưng 70% branch nghĩa là nhiều edge cases chưa được test. 100% coverage không đảm bảo bug-free: test có thể chạy code nhưng không assert correctly; test với wrong inputs không catch bugs với right inputs.
- Low coverage là red flag: dưới 60-70% thường là warning sign.
- Coverage thresholds trong jest.config.js: coverageThreshold: { global: { branches: 80, functions: 80 } } — fail CI nếu không đạt.
- Practical targets: 80-90% cho business logic; 70%+ overall; không chase 100% cho UI components và configuration files.
- Excluded from coverage: vendor code, generated files, test utilities — dùng coveragePathIgnorePatterns.
- Coverage reports: text (CI), html (visual inspection), lcov (SonarQube, Codecov).
- Mutation testing (Stryker): modify code và check nếu tests fail — đo test quality, không chỉ coverage quantity.
Code coverage measures the percentage of code executed by tests — generated by Istanbul (Jest) or V8 (Vitest V8 provider).
- Metrics: Statement coverage (% of statements executed — every expression/assignment is a statement); Branch coverage (% of branches covered — every if/else/ternary/&&/|| creates 2 branches; both true and false cases must be tested); Function coverage (% of functions called at least once); Line coverage (% of lines executed — usually the highest and least meaningful metric).
- Branch coverage is the most important metric — 100% statement coverage but 70% branch coverage means many edge cases are untested. 100% coverage does not guarantee bug-free code: tests may execute code without asserting correctly; tests with wrong inputs will not catch bugs triggered by correct inputs.
- Low coverage is a red flag: below 60-70% is generally a warning sign.
- Coverage thresholds in jest.config.js: coverageThreshold: { global: { branches: 80, functions: 80 } } — fails CI if not met.
- Practical targets: 80-90% for business logic; 70%+ overall; do not chase 100% for UI components and configuration files.
- Excluded from coverage: vendor code, generated files, test utilities — use coveragePathIgnorePatterns.
- Coverage reports: text (CI), html (visual inspection), lcov (SonarQube, Codecov).
- Mutation testing (Stryker): modifies code and checks whether tests fail — measures test quality, not just coverage quantity.