SupercovBlog

Guide

Which test covers this line? Vitest, Jest and pytest

Each way to find the tests that ran a line in Vitest, Jest and pytest, measured on the same project.

Line 8 ran. Which test ran it? A card: vitest run --coverage, struck through, hits: 2, no names; pytest --cov-context=test names the 2 tests; supercov runs latest line names the 2 tests.

In pytest, run pytest --cov --cov-context=test, then coverage html --show-contexts: each line of the report lists the tests that ran it. Vitest and Jest do not record this. Their coverage adds every test into one count per line, so the only built-in way to tell is one coverage run per test.

A diagram of three tools. Vitest and Jest with --coverage store one count per line, so line 8 shows it ran 2 times without naming tests. coverage.py with --cov-context=test and Supercov store the tests per line, so both name the 2 tests that ran line 8.
What one coverage run stores, and whether it can name the tests.

Supercov, which we make, records coverage per test in one normal run of Vitest, Jest, pytest and the other supported runners, and supercov runs latest line <file:line> lists the tests that ran a line. See below.

At a glance

RunnerBuilt in?How
pytestYespytest --cov --cov-context=test, then coverage html --show-contexts or coverage json --show-contexts
VitestNoOne vitest run --coverage -t "<test name>" per test
JestNoOne jest --coverage -t "<test name>" per test
Any of them, with Supercovnpx supercov -- <test command> once, then npx supercov runs latest line <file:line>

We measured each on the same small project: a discount function, three tests for it, and one checkout test that calls it. Vitest 5.1.0 with the V8 provider, Jest 30.5.2, and coverage.py 7.17.2 with pytest-cov 7.1.0 and pytest 9.2.0. Every method that names tests gave the same answer: line 6, return cents - 1000;, runs in one test, and line 8, return cents;, in two.

A grid of five methods against four tests, HALF, TENOFF at least 10, TENOFF under 10 and checkout. Vitest or Jest --coverage names no tests. vitest related and --findRelatedTests name all four. One coverage run per test names TENOFF under 10 and checkout, in 4 runs. coverage.py --cov-context=test and supercov runs latest line name the same two in 1 run.
Which tests each method named for line 8 of discount.js.

pytest: coverage contexts

pytest --cov --cov-context=test
coverage html --show-contexts
coverage json --show-contexts -o contexts.json

--cov-context=test is pytest-cov’s option. It labels everything each test runs with the test’s id. With coverage.py alone, set dynamic_context = test_function under [run]. In the HTML report, click a line to see its tests. The JSON report lists them under each file’s contexts:

"5": ["tests/test_discount.py::test_tenoff_takes_ten_off_orders_of_ten_or_more|run"],
"6": ["tests/test_checkout.py::test_checkout_adds_up_the_items|run",
      "tests/test_discount.py::test_tenoff_does_nothing_under_ten|run"]

|run means the line ran in the test body. Setup and teardown get their own suffixes.

Vitest and Jest: one coverage run per test

coverage-final.json from either runner holds one hit count per statement for the whole run. Line 8 reported 2 hits, with nothing to say which tests made them. To find out, run coverage once per test name and check the line each time:

vitest run --coverage -t "^TENOFF does nothing under ten$"
jest --coverage -t "^TENOFF does nothing under ten$"

That took four runs for four tests: 1.7 s in Vitest and 1.5 s in Jest, against 0.23 s for one Vitest run of all four. The number of runs grows with the number of tests, so on a real suite this is a script you leave running, not a question you ask.

vitest related src/discount.js and jest --findRelatedTests src/discount.js answer a different question. They follow imports, so both picked all four tests, including two that never reach line 8.

Supercov: one run, any runner

npx supercov -- npx vitest run
npx supercov runs latest line src/discount.js:8
src/discount.js:8

Source
      8 | return cents;

Status
  COVERED
  Evidence: execution only

Covering tests
  TENOFF does nothing under ten [vitest:-1607981173_2] — unit/vitest
  checkout adds up the items [vitest:2006354534_0] — unit/vitest

The same two commands, with npx jest or pytest in the first, gave the same tests for Jest and pytest. The whole Vitest run under Supercov took 0.89 s.

Pass several lines at once, or --from-file with one per line, and add --json for a script or a coding agent. Each line also lists its branches and conditions with the tests for each. See the CLI reference and runner support for every runner and language, and Vitest per-test coverage for the same question from the test’s side.

More coverage guides