Coverage tools work in two phases: they instrument the source (insert a probe before every line), then, after the tests ran, turn the recorded hits into a report. Implement both, plus merging the hits of several test runs.
solve(op, ...args):
| Op | Args | Result |
|---|---|---|
'instrument' | (sourceLines) | the lines with a probe in front: line i becomes ` __hit(i); ${line} |
'merge' | (covA, covB) | the union of two coverage maps |
'report' | (sourceLines, cov) | { total, covered, percent, uncovered } |
A coverage map is an object whose keys are line indices (as strings, since it is JSON) and whose values are true: { "0": true, "2": true } means lines 0 and 2 ran.
Report rules:
total is the number of source lines; covered counts the lines of the source present in cov (keys outside the source are ignored).percent is covered / total * 100 rounded half up (12.5 → 13), and 100 for an empty file.uncovered lists the 0-based indices of the lines that never ran, ascending.solve('instrument', ['function add(a, b) {', ' return a + b;', '}'])
// → ['__hit(0); function add(a, b) {', '__hit(1); return a + b;', '__hit(2); }']
solve('report', ['line0', 'line1', 'line2'], { '0': true, '2': true })
// → { total: 3, covered: 2, percent: 67, uncovered: [1] }Sample tests