HardPro challengeJavaScriptTypeScript

Implement Code Coverage Tracker

TestingJavaScriptUtilities

Implement solve(op, ...args):

OpArgsDescription
'instrument'(sourceLines)Takes an array of source code lines. Returns an instrumented version: a string where each line is prefixed with __cov[lineIndex] = true;.
'run'(instrumentedSrc)Executes the instrumented source. Returns a __cov object mapping line index → true (only for executed lines).
'report'(sourceLines, cov)Returns { total, covered, percent, uncovered } where uncovered is an array of 0-based line indices that were NOT covered.
const lines = [
  'function add(a, b) {',
  '  return a + b',
  '}',
]
const src = solve('instrument', lines)
const cov = solve('run', src + '\nadd(1, 2)')
solve('report', lines, cov)
// { total: 3, covered: 3, percent: 100, uncovered: [] }

Blank lines and lines containing only } or { still count toward total.

Sample tests

Test #1instrument prefixes each line
Input: ["instrument",["function add(a,b){"," return a+b","}"]]
Output: "const __cov = {};\n__cov[0] = true; function add(a,b){\n__cov[1] = true; return a+b\n__cov[2] = true; }\n__cov"
Test #2report computes stats correctly
Input: ["report",["line0","line1","line2"],{"0":true,"2":true}]
Output: {"total":3,"covered":2,"percent":67,"uncovered":[1]}
Test #3100% coverage
Input: ["report",["a","b","c","d"],{"0":true,"1":true,"2":true,"3":true}]
Output: {"total":4,"covered":4,"percent":100,"uncovered":[]}