function processValue(value: number): string {
let result = "";
if (value > 0) {
result = "Positive";
} else {
result = "Non-positive";
}
return result;
}
// Jest Test
describe("processValue", () => {
it("handles positive values", () => {
expect(processValue(5)).toBe("Positive");
});
});
What is the statement coverage percentage for this code after running the test?
Statement coverage measures the percentage of executed statements. The function has five statements: variable declaration (let result = ""), the if condition, the result = "Positive" assignment, the result = "Non-positive" assignment, and the return statement. The test only exercises the value > 0 branch, executing four statements (missing the result = "Non-positive" assignment). Thus, 4/5 statements are covered, resulting in 80% statement coverage.
TypeScript function and its Jest test:
function categorizeNumber(num: number): string {
if (num > 0) {
if (num > 100) {
return "Large Positive";
}
return "Small Positive";
} else if (num === 0) {
return "Zero";
} else {
return "Negative";
}
}
// Jest Test
describe("categorizeNumber", () => {
it("handles small positive numbers", () => {
expect(categorizeNumber(50)).toBe("Small Positive");
});
it("handles zero", () => {
expect(categorizeNumber(0)).toBe("Zero");
});
});
What is the branch coverage percentage for this code?
Branch coverage measures the percentage of executed branches
in conditional logic. The function has four
branches: num > 0 && num > 100
, num > 0 && !(num > 100)
,
num === 0
, and num < 0
. The tests cover two
branches: num > 0 && !(num > 100)
(for num = 50
) and num === 0
.
The branches for num > 100
and num < 0
are not tested.
Thus, 2/4 branches are covered, resulting in 50% branch coverage.
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
// Jest Test
describe("math operations", () => {
it("tests addition", () => {
expect(add(2, 3)).toBe(5);
});
it("tests multiplication", () => {
expect(multiply(2, 3)).toBe(6);
});
});
What is the function coverage percentage for this code?
Function coverage measures the percentage of functions called during test execution. There are three functions: add, subtract, and multiply. The tests call add and multiply, but not subtract. Thus, 2/3 functions are covered, resulting in 66.67% function coverage.
function toggleStatus(status: boolean, priority: number): string {
if (status) { // Line 2
if (priority > 5) { // Line 3
return "High Priority Active"; // Line 4
}
return "Active"; // Line 6
} else { // Line 7
if (priority < 0) { // Line 8
return "Invalid Priority"; // Line 9
}
return "Inactive"; // Line 11
}
}
// Jest Test
describe("toggleStatus", () => {
it("tests active status with low priority", () => {
expect(toggleStatus(true, 3)).toBe("Active");
});
});
The coverage report indicates that lines 4, 8, 9, and 11 are uncovered. What is the line coverage percentage?
Line coverage measures the percentage of executed lines. The function has 10 lines of code (excluding empty lines or braces): lines 2, 3, 4, 6, 7, 8, 9, 11, plus the function signature and return statements. The test executes lines 2, 3, 6, and the function signature (4 lines), as it follows the status = true and priority <= 5 path. Lines 4, 8, 9, and 11 are uncovered, confirming 6 lines are not executed. Thus, 4/10 lines are covered, resulting in 40% line coverage.
TypeScript file getNewCheckedIds.ts shows the following:
File: getNewCheckedIds.ts
% Stmts: 80%
% Branch: 50%
% Funcs: 100%
% Lines: 80%
Uncovered Lines: 9, 10
Based on this report, which of the following statements is true?
The report shows 100% function coverage, meaning all functions are called (A is true). Branch coverage is only 50%, so not all branches are tested (B is false). Uncovered lines 9 and 10 are not executed (C is false). Statement coverage is 80%, not 100% (D is false).