Reputation: 1118
I have a file, parsePredicate.ts
, which I believe has no branches in it. However, Jest coverage report is saying that it only has 75% branch coverage. Where are the branches?
parsePredicate.ts
:
import parseIdentifier from "../parseIdentifier/parseIdentifier";
import parseOperator from "../parseOperator/parseOperator";
import { parseExpression } from "../parseExpression/parseExpression";
const parsePredicate = (str: string)=>{
let {identifier: left, rest:identRest} = parseIdentifier(str);
let {operator, rest:opRest} = parseOperator(identRest);
let {expression: right, rest:exprRest} = parseExpression(opRest);
return { left: left, operator: operator, right: right, rest: exprRest };
}
export default parsePredicate;
parsePredicate.test.ts
:
import parsePredicate from "./parsePredicate";
import parseIdentifier from "../parseIdentifier/parseIdentifier";
import parseOperator from "../parseOperator/parseOperator";
import { parseExpression } from "../parseExpression/parseExpression";
jest.mock("../parseIdentifier/parseIdentifier");
jest.mock("../parseOperator/parseOperator");
jest.mock("../parseExpression/parseExpression");
test("Parsing a predicate with number returns proper values", ()=>{
// @ts-ignore
parseIdentifier.mockReturnValueOnce({identifier: "x", rest:" = 1"});
// @ts-ignore
parseOperator.mockReturnValueOnce({operator: "=", rest:" 1"});
// @ts-ignore
parseExpression.mockReturnValueOnce({expression: "1", rest:""});
expect(parsePredicate("x = 1")).toStrictEqual({left:"x", operator:"=", right:"1", rest:""});
});
test("Parsing a predicate with math returns proper values", ()=>{
// @ts-ignore
parseIdentifier.mockReturnValueOnce({identifier: "x", rest:" = 1 + 1"});
// @ts-ignore
parseOperator.mockReturnValueOnce({operator: "=", rest:" 1 + 1"});
// @ts-ignore
parseExpression.mockReturnValueOnce({expression: "1 + 1", rest:""});
expect(parsePredicate("x = 1 + 1")).toStrictEqual({left:"x", operator:"=", right:"1 + 1", rest:""});
});
test("Parsing a predicate with string returns proper values", ()=>{
// @ts-ignore
parseIdentifier.mockReturnValueOnce({identifier: "x", rest:" = 'Hello World'"});
// @ts-ignore
parseOperator.mockReturnValueOnce({operator: "=", rest:" 'Hello World'"});
// @ts-ignore
parseExpression.mockReturnValueOnce({expression: "'Hello World'", rest:""});
expect(parsePredicate("x = 'Hello World'")).toStrictEqual({left:"x", operator:"=", right:"'Hello World'", rest:""});
});
test("Parsing a predicate with identifier returns proper values", ()=>{
// @ts-ignore
parseIdentifier.mockReturnValueOnce({identifier: "x", rest:" = ident"});
// @ts-ignore
parseOperator.mockReturnValueOnce({operator: "=", rest:" ident"});
// @ts-ignore
parseExpression.mockReturnValueOnce({expression: "ident", rest:""});
expect(parsePredicate("x = ident")).toStrictEqual({left:"x", operator:"=", right:"ident", rest:""});
});
When I run jest --coverage
I get the following output for my parsePredicate.ts
file:
---------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------|---------|----------|---------|---------|-------------------
parsePredicate | 100 | 75 | 100 | 100 |
parsePredicate.ts | 100 | 75 | 100 | 100 | 11
As you can see, it says that line #11 is uncovered. However, line 11 is just an export
statement:
export default parsePredicate;
The lcov report looks like this:
Note that nothing changes if I press n or j.
I have been looking into this, and it seems like it may have to do with the esmodule glue code that is generated by Babel, however I am not sure how to go about fixing that.
Please leave a comment if you need any more information. Any help would be greatly appreciated.
Upvotes: 5
Views: 3426
Reputation: 1118
After quite a long time of trying to figure out the problem on and off, I have come up to the following answer.
Basically, in my jest.config.js
I was using the v8
coverage provider.
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
I use node version 12.16.1, so this provider wasn't really great and had many issues.
Changing to the babel
provider fixed all the problems I was having, and gave this code 100% branch coverage.
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "babel",
Upvotes: 2