Meaning of function coverage in Jest
Asked Answered
R

1

9

Here is the output from running jest on one of my components:

----------------------------|---------|----------|---------|---------|-------------------
File                        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------------------|---------|----------|---------|---------|-------------------
All files                   |     100 |      100 |      50 |     100 |                   
 search-suggestion-base.tsx |     100 |      100 |      50 |     100 |                   
----------------------------|---------|----------|---------|---------|-------------------
...
Jest: "global" coverage threshold for functions (100%) not met: 50%

Note that % Funcs is not 100 and the test fails. (The coverage threshold is not set by me. I wouldn't have set such a high threshold.)

The problem is, Jest is not telling me which are the uncovered lines. Besides, I'm also having trouble figuring out what's the meaning of % Funcs. I can't even find the official documentation for this % Funcs.

Any help is appreciated!

Resection answered 29/11, 2021 at 6:36 Comment(2)
Can you open .coverage/lcov-report/index.html in the browser to check if there are uncovered lines? About how to read the coverage reporter, see #26618743Panda
@slideshowp2 Thanks a lot! The report does show the uncovered lines!Resection
R
5

As pointed out by @slideshowp2, .coverage/lcov-report/index.html does show the uncovered lines.

The .coverage folder didn't exist at fist. I had to ran jest with the --coverage parameter. You can refer to How to get the code coverage report using Jest? for more information.

By the way, it turned out that a default event handler - which is a function - was not covered. So I guess % Funcs means literally the percentage of functions covered.

In my case, the default event handler is an empty function () => {}. I think that's why Jest told me that "statements, branches and lines" are all covered, but "functions" are not.

Resection answered 29/11, 2021 at 15:40 Comment(1)
can you please mention what should we pass as default value for the functions?Dodona

© 2022 - 2024 — McMap. All rights reserved.