|
1 | 1 | import test from "ava"; |
2 | 2 | import { |
| 3 | + extractLineCoverage, |
3 | 4 | filterScriptUrl, |
4 | 5 | takeAndProcessSnapshot, |
5 | 6 | V8CoverageData, |
@@ -216,3 +217,93 @@ class Example {} |
216 | 217 | t.truthy(ast); |
217 | 218 | t.is(ast.type, "File"); |
218 | 219 | }); |
| 220 | + |
| 221 | +// --- extractLineCoverage --- |
| 222 | + |
| 223 | +test("extractLineCoverage: expands multi-line statements to all lines", (t) => { |
| 224 | + // res.json({ ... }) spanning lines 14-20, covered |
| 225 | + const statementMap = { |
| 226 | + "0": { start: { line: 14, column: 2 }, end: { line: 20, column: 4 } }, |
| 227 | + }; |
| 228 | + const statementCounts = { "0": 1 }; |
| 229 | + |
| 230 | + const lines = extractLineCoverage(statementMap, statementCounts); |
| 231 | + |
| 232 | + t.is(lines["14"], 1); |
| 233 | + t.is(lines["15"], 1); |
| 234 | + t.is(lines["16"], 1); |
| 235 | + t.is(lines["17"], 1); |
| 236 | + t.is(lines["18"], 1); |
| 237 | + t.is(lines["19"], 1); |
| 238 | + t.is(lines["20"], 1); |
| 239 | +}); |
| 240 | + |
| 241 | +test("extractLineCoverage: single-line statement works", (t) => { |
| 242 | + const statementMap = { |
| 243 | + "0": { start: { line: 5, column: 0 }, end: { line: 5, column: 30 } }, |
| 244 | + }; |
| 245 | + const statementCounts = { "0": 3 }; |
| 246 | + |
| 247 | + const lines = extractLineCoverage(statementMap, statementCounts); |
| 248 | + |
| 249 | + t.is(lines["5"], 3); |
| 250 | + t.false("4" in lines); |
| 251 | + t.false("6" in lines); |
| 252 | +}); |
| 253 | + |
| 254 | +test("extractLineCoverage: inner uncovered statement overrides outer covered statement", (t) => { |
| 255 | + // Simulates: try { ... } catch (error) { res.status(500).json({...}) } |
| 256 | + // Outer try-catch block (lines 25-40) is covered (count=1) |
| 257 | + // Inner catch body (lines 36-39) is uncovered (count=0) |
| 258 | + const statementMap = { |
| 259 | + "0": { start: { line: 25, column: 0 }, end: { line: 40, column: 1 } }, |
| 260 | + "1": { start: { line: 26, column: 4 }, end: { line: 27, column: 5 } }, |
| 261 | + "2": { start: { line: 36, column: 4 }, end: { line: 39, column: 5 } }, |
| 262 | + }; |
| 263 | + const statementCounts = { |
| 264 | + "0": 1, // try-catch executed |
| 265 | + "1": 1, // try body executed |
| 266 | + "2": 0, // catch body NOT executed |
| 267 | + }; |
| 268 | + |
| 269 | + const lines = extractLineCoverage(statementMap, statementCounts); |
| 270 | + |
| 271 | + // Try body lines should be covered |
| 272 | + t.is(lines["26"], 1); |
| 273 | + t.is(lines["27"], 1); |
| 274 | + |
| 275 | + // Catch body lines should be uncovered (inner statement overrides outer) |
| 276 | + t.is(lines["36"], 0); |
| 277 | + t.is(lines["37"], 0); |
| 278 | + t.is(lines["38"], 0); |
| 279 | + t.is(lines["39"], 0); |
| 280 | + |
| 281 | + // Outer try-catch structural lines are covered |
| 282 | + t.is(lines["25"], 1); |
| 283 | + t.is(lines["40"], 1); |
| 284 | +}); |
| 285 | + |
| 286 | +test("extractLineCoverage: same-size statements use max count (covered wins)", (t) => { |
| 287 | + // Two single-line statements on the same line: if (x) foo(); else bar(); |
| 288 | + // foo() was called (count=1), bar() was not (count=0) |
| 289 | + const statementMap = { |
| 290 | + "0": { start: { line: 10, column: 0 }, end: { line: 10, column: 40 } }, |
| 291 | + "1": { start: { line: 10, column: 7 }, end: { line: 10, column: 20 } }, |
| 292 | + "2": { start: { line: 10, column: 26 }, end: { line: 10, column: 40 } }, |
| 293 | + }; |
| 294 | + const statementCounts = { |
| 295 | + "0": 1, // if statement executed |
| 296 | + "1": 1, // foo() called |
| 297 | + "2": 0, // bar() not called |
| 298 | + }; |
| 299 | + |
| 300 | + const lines = extractLineCoverage(statementMap, statementCounts); |
| 301 | + |
| 302 | + // Line 10 should be covered because at least one statement on it was executed |
| 303 | + t.is(lines["10"], 1); |
| 304 | +}); |
| 305 | + |
| 306 | +test("extractLineCoverage: handles empty statement map", (t) => { |
| 307 | + const lines = extractLineCoverage({}, {}); |
| 308 | + t.deepEqual(lines, {}); |
| 309 | +}); |
0 commit comments