Questions & Answers

In Jest coverage report, how can I find the test that was responsible for testing a specific line of code?

This is a general question, that may apply to any code coverage report.

enter image description here

In my example above, highlighted line was tested over 77x times, but I'm unable to find the test itself which is testing it (I'm working on a very big repo).

What would be the best way to know this? Is there any flag I'm missing to add this info on top?


Answers(1) :

I don't believe that this is possible. Under the hood, the Istanbul code coverage tool is simply instrumenting your code into something like

const container = document.getElementById('container');
camera = new THREE.PerspectiveCamera();

(It may also be able to use V8's built-in code coverage features, but I expect the behavior for those is similar.)

To get per-test coverage, you could probably maintain a separate set of code coverage statistics for every test, then aggregate all of those when done, but I'm not aware of any existing tool that does this.

Absent that, there are manual alternatives, such as setting a breakpoint on an interesting line and then running the entire test suite in a debugger, or adding a throw new Error("THIS LINE WAS HIT"); at an interesting line then seeing which tests fail.