Code coverage reporting shows which parts of your code are tested. It helps you find untested code to improve your tests.
Code coverage reporting in Deno
deno test --coverage=coverage_data deno coverage coverage_data --lcov > coverage.lcov deno coverage coverage_data --json > coverage.json
Use --coverage=coverage_data with deno test to collect coverage info.
Then use deno coverage to view or export reports in formats like lcov or json.
coverage_data.deno test --coverage=coverage_data
deno coverage coverage_data
lcov format for tools like codecov or CI systems.deno coverage coverage_data --lcov > coverage.lcov
deno coverage coverage_data --json > coverage.json
This simple test file checks the add function with positive and negative numbers. Running tests with coverage will show that both tests cover the add function.
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; function add(a: number, b: number): number { return a + b; } Deno.test("add positive numbers", () => { assertEquals(add(2, 3), 5); }); Deno.test("add negative numbers", () => { assertEquals(add(-1, -1), -2); });
Coverage data is stored in a folder you specify, so keep it clean or add to .gitignore.
Coverage reports help find untested code but do not guarantee code correctness.
Use coverage reports regularly to keep tests effective as code changes.
Code coverage shows which code your tests run.
Use deno test --coverage to collect coverage data.
Use deno coverage to view or export reports.
Practice
deno test --coverage command do?Solution
Step 1: Understand the purpose of
This command runs your test files and collects data on which parts of your code were executed during testing.deno test --coverageStep 2: Differentiate from other commands
Unlike just running tests, this command also tracks coverage. It does not generate reports directly; that is done bydeno coverage.Final Answer:
Runs tests and collects code coverage data -> Option DQuick Check:
deno test --coverage collects coverage data [OK]
- Thinking it only runs tests without coverage
- Confusing coverage data collection with report generation
- Assuming it installs tools instead of running tests
Solution
Step 1: Identify the command to view coverage
Thedeno coveragecommand is used to view or export coverage reports after running tests with coverage.Step 2: Recognize the correct flag for terminal output
The flag--reporter=texttells Deno to show the coverage report in the terminal.Final Answer:
deno coverage --reporter=text -> Option AQuick Check:
Use deno coverage with --reporter=text to view coverage [OK]
- Using deno test flags to view coverage report
- Assuming --show or --view-coverage are valid flags
- Confusing test and coverage commands
deno test --coverage=./cov_data deno coverage ./cov_data --reporter=lcov
What is the expected output?
Solution
Step 1: Run tests with coverage saved to a directory
The first command runs tests and saves coverage data into the folder./cov_data.Step 2: Generate coverage report in lcov format
The second command reads coverage data from./cov_dataand outputs a report in lcov format to a file (stdout is the report content, but typically redirected to a file).Final Answer:
A coverage report file in lcov format is generated -> Option BQuick Check:
Coverage data saved + lcov report generated [OK]
- Expecting coverage report without saving data first
- Thinking lcov generates a file automatically (use redirection)
- Assuming error if directory exists
deno test --coverage but deno coverage shows no data. What is the likely cause?Solution
Step 1: Understand coverage data flow
Coverage data is collected when tests run with coverage enabled and saved to a directory or default location.Step 2: Check command order
Ifdeno coverageis run before tests, no coverage data exists yet, so it shows no data.Final Answer:
You ran deno coverage before running tests -> Option CQuick Check:
Run tests first, then coverage report [OK]
- Assuming coverage directory is mandatory every time
- Thinking syntax errors cause no coverage data
- Believing coverage data auto-deletes after tests
Solution
Step 1: Run tests with coverage saved to a directory
Usedeno test --coverage=./cov_datato run tests and save coverage data in./cov_data.Step 2: Generate HTML report from saved coverage data
Rundeno coverage ./cov_data --reporter=htmlto create HTML files insidecoverage/directory.Step 3: Open the generated HTML report in browser
Useopen coverage/index.html(macOS) or equivalent to view the report.Final Answer:
deno test --coverage=./cov_data && deno coverage ./cov_data --reporter=html && open coverage/index.html -> Option AQuick Check:
Save coverage, generate HTML, then open file [OK]
- Generating report before running tests
- Not specifying coverage directory consistently
- Redirecting HTML output to file instead of directory
Ready to prove it?
Pass the test and this pattern moves unknown
