codex-lv3-may-2025

Level Navigation: 1 (2ℹ️) 3 4 5 6 (7ℹ️) (8ℹ️) (9ℹ️) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 (37ℹ️) 38⚡ 39⚡ 40⚡ 41⚡ 42 43⚡ 44⚡ 45 46 (47ℹ️)

Level 42: Add Test Coverage

Keeping an eye on test coverage helps you understand which parts of your code are exercised by your suite. Let’s install the coverage peer dependency and add a script that makes running coverage painless.

Step 1: Install the coverage reporter

Vitest’s coverage command relies on the V8/Istanbul integration shipped in @vitest/coverage-v8. Install it as a dev dependency using npm:

npm install -D @vitest/coverage-v8

Step 2: Add the coverage script

Update your package.json scripts section so it includes a test:coverage command:

"scripts": {
  "test": "vitest",
  "test:coverage": "vitest run --coverage"
}

Tip: If you already have other scripts (like "dev" or "lint"), just add this line alongside them—keep the trailing commas consistent with the existing JSON.

Step 3: Run the coverage report

npm run test:coverage

Vitest will execute the full suite once and print a table showing statements, branches, functions, and lines covered. The report also lands in the coverage/ directory if you want to inspect the HTML output.

Example console output:

 % Coverage report from v8

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |      50 |      100 |      50 |      50 |
 utils.js |      50 |      100 |      50 |      50 | 2
----------|---------|----------|---------|---------|-------------------

Step 4: Review your results

Try it: Install the reporter, add the script, run coverage, and note any functions that still need tests before moving on.

Key Terms