diff --git a/src/CoverageTransformer.js b/src/CoverageTransformer.js index c842bdb..0dd6551 100644 --- a/src/CoverageTransformer.js +++ b/src/CoverageTransformer.js @@ -56,38 +56,40 @@ export default class CoverageTransformer { return; } - /* coverage.json can sometimes include the code inline */ - let codeIsArray = true; - let codeFromFile = false; - let jsText = fileCoverage.code; - if (!jsText) { - jsText = this.readFile(filePath); - codeFromFile = true; - } - if (Array.isArray(jsText)) { /* sometimes the source is an array */ - jsText = jsText.join('\n'); - } else { - codeIsArray = false; - } - let match = sourceMapRegEx.exec(jsText); - let sourceMapDir = path.dirname(filePath); let rawSourceMap; - + let sourceMapDir = path.dirname(filePath); + let codeIsArray = true; if (fileCoverage.inputSourceMap) { rawSourceMap = fileCoverage.inputSourceMap; - } else if (!match && !codeFromFile) { - codeIsArray = false; - jsText = this.readFile(filePath); - match = sourceMapRegEx.exec(jsText); - } - - if (match) { - if (match[1]) { - rawSourceMap = JSON.parse((new Buffer(match[2], 'base64').toString('utf8'))); + } else { + /* coverage.json can sometimes include the code inline */ + let codeFromFile = false; + let jsText = fileCoverage.code; + if (!jsText) { + jsText = this.readFile(filePath); + codeFromFile = true; + } + if (Array.isArray(jsText)) { /* sometimes the source is an array */ + jsText = jsText.join('\n'); } else { - const sourceMapPath = path.join(sourceMapDir, match[2]); - rawSourceMap = this.readJSON(sourceMapPath); - sourceMapDir = path.dirname(sourceMapPath); + codeIsArray = false; + } + let match = sourceMapRegEx.exec(jsText); + + if (!match && !codeFromFile) { + codeIsArray = false; + jsText = this.readFile(filePath); + match = sourceMapRegEx.exec(jsText); + } + + if (match) { + if (match[1]) { + rawSourceMap = JSON.parse((new Buffer(match[2], 'base64').toString('utf8'))); + } else { + const sourceMapPath = path.join(sourceMapDir, match[2]); + rawSourceMap = this.readJSON(sourceMapPath); + sourceMapDir = path.dirname(sourceMapPath); + } } } diff --git a/tests/unit/lib/remap.js b/tests/unit/lib/remap.js index 9ede572..f8307c8 100644 --- a/tests/unit/lib/remap.js +++ b/tests/unit/lib/remap.js @@ -81,6 +81,12 @@ define([ assert.instanceOf(coverage, Collector, 'Return values should be instance of Collector'); assert(coverage.store.map['tests/unit/support/basic.ts'] || coverage.store.map['tests\\unit\\support\\basic.ts']); }, + + 'coverage includes sourcemap': function () { + var coverage = remap(loadCoverage('tests/unit/support/inline-coverage-inputSourceMap-customSourceRoot.json')); + assert(coverage.store.map['tests/unit/support/customSourceRoot/basic.ts'] || coverage.store.map['tests\\unit\\support\\customSourceRoot\\basic.ts'], + 'The inputSourceMap in coverage should have been used'); + }, 'empty options': function () { assert.throws(remap, TypeError); diff --git a/tests/unit/support/inline-coverage-inputSourceMap-customSourceRoot.json b/tests/unit/support/inline-coverage-inputSourceMap-customSourceRoot.json new file mode 100644 index 0000000..f838b2a --- /dev/null +++ b/tests/unit/support/inline-coverage-inputSourceMap-customSourceRoot.json @@ -0,0 +1,2 @@ +{ +"tests/unit/support/inline.js":{"path":"tests/unit/support/inline.js","s":{"1":1,"2":1,"3":0,"4":0,"5":0,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":1,"14":1,"15":1,"16":2,"17":2,"18":2,"19":2,"20":2,"21":1,"22":1,"23":1,"24":1,"25":5,"26":1,"27":5,"28":1,"29":4,"30":3,"31":1,"32":1,"33":0,"34":1,"35":2,"36":1,"37":1,"38":1,"39":1},"b":{"1":[0,1],"2":[1,0],"3":[0,0],"4":[1,0],"5":[1,1],"6":[1,1],"7":[1,4],"8":[5,4],"9":[3,1],"10":[1,0],"11":[1,1]},"f":{"1":1,"2":1,"3":1,"4":1,"5":2,"6":1,"7":5,"8":2},"fnMap":{"1":{"name":"(anonymous_1)","line":1,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":53}},"skip":true},"2":{"name":"(anonymous_2)","line":8,"loc":{"start":{"line":8,"column":27},"end":{"line":8,"column":55}}},"3":{"name":"(anonymous_3)","line":9,"loc":{"start":{"line":9,"column":5},"end":{"line":9,"column":22}}},"4":{"name":"(anonymous_4)","line":16,"loc":{"start":{"line":16,"column":15},"end":{"line":16,"column":27}}},"5":{"name":"Foo","line":17,"loc":{"start":{"line":17,"column":8},"end":{"line":17,"column":30}}},"6":{"name":"(anonymous_6)","line":25,"loc":{"start":{"line":25,"column":31},"end":{"line":25,"column":43}}},"7":{"name":"(anonymous_7)","line":31,"loc":{"start":{"line":31,"column":25},"end":{"line":31,"column":41}}},"8":{"name":"(anonymous_8)","line":46,"loc":{"start":{"line":46,"column":32},"end":{"line":46,"column":49}}}},"statementMap":{"1":{"start":{"line":1,"column":0},"end":{"line":54,"column":3}},"2":{"start":{"line":2,"column":4},"end":{"line":7,"column":5},"skip":true},"3":{"start":{"line":3,"column":8},"end":{"line":3,"column":42},"skip":true},"4":{"start":{"line":3,"column":43},"end":{"line":3,"column":83},"skip":true},"5":{"start":{"line":3,"column":64},"end":{"line":3,"column":83},"skip":true},"6":{"start":{"line":5,"column":9},"end":{"line":7,"column":5},"skip":true},"7":{"start":{"line":6,"column":8},"end":{"line":6,"column":30},"skip":true},"8":{"start":{"line":9,"column":4},"end":{"line":14,"column":46}},"9":{"start":{"line":10,"column":8},"end":{"line":10,"column":36}},"10":{"start":{"line":11,"column":8},"end":{"line":11,"column":36}},"11":{"start":{"line":12,"column":8},"end":{"line":12,"column":36}},"12":{"start":{"line":13,"column":8},"end":{"line":13,"column":36}},"13":{"start":{"line":15,"column":4},"end":{"line":15,"column":30}},"14":{"start":{"line":16,"column":4},"end":{"line":50,"column":9}},"15":{"start":{"line":17,"column":8},"end":{"line":24,"column":9}},"16":{"start":{"line":18,"column":12},"end":{"line":18,"column":29}},"17":{"start":{"line":19,"column":12},"end":{"line":19,"column":28}},"18":{"start":{"line":20,"column":12},"end":{"line":20,"column":29}},"19":{"start":{"line":21,"column":12},"end":{"line":23,"column":13}},"20":{"start":{"line":22,"column":16},"end":{"line":22,"column":41}},"21":{"start":{"line":25,"column":8},"end":{"line":45,"column":10}},"22":{"start":{"line":26,"column":12},"end":{"line":26,"column":29}},"23":{"start":{"line":27,"column":12},"end":{"line":27,"column":26}},"24":{"start":{"line":28,"column":12},"end":{"line":30,"column":13}},"25":{"start":{"line":29,"column":16},"end":{"line":29,"column":45}},"26":{"start":{"line":31,"column":12},"end":{"line":44,"column":15}},"27":{"start":{"line":32,"column":16},"end":{"line":43,"column":17}},"28":{"start":{"line":33,"column":20},"end":{"line":33,"column":43}},"29":{"start":{"line":35,"column":21},"end":{"line":43,"column":17}},"30":{"start":{"line":36,"column":20},"end":{"line":36,"column":43}},"31":{"start":{"line":38,"column":21},"end":{"line":43,"column":17}},"32":{"start":{"line":39,"column":20},"end":{"line":39,"column":51}},"33":{"start":{"line":42,"column":20},"end":{"line":42,"column":55}},"34":{"start":{"line":46,"column":8},"end":{"line":48,"column":10}},"35":{"start":{"line":47,"column":12},"end":{"line":47,"column":73}},"36":{"start":{"line":49,"column":8},"end":{"line":49,"column":19}},"37":{"start":{"line":51,"column":4},"end":{"line":51,"column":22}},"38":{"start":{"line":52,"column":4},"end":{"line":52,"column":38}},"39":{"start":{"line":53,"column":4},"end":{"line":53,"column":26}}},"branchMap":{"1":{"line":2,"type":"if","locations":[{"start":{"line":2,"column":4},"end":{"line":2,"column":4},"skip":true},{"start":{"line":2,"column":4},"end":{"line":2,"column":4},"skip":true}]},"2":{"line":2,"type":"binary-expr","locations":[{"start":{"line":2,"column":8},"end":{"line":2,"column":34},"skip":true},{"start":{"line":2,"column":38},"end":{"line":2,"column":72},"skip":true}]},"3":{"line":3,"type":"if","locations":[{"start":{"line":3,"column":43},"end":{"line":3,"column":43},"skip":true},{"start":{"line":3,"column":43},"end":{"line":3,"column":43},"skip":true}]},"4":{"line":5,"type":"if","locations":[{"start":{"line":5,"column":9},"end":{"line":5,"column":9},"skip":true},{"start":{"line":5,"column":9},"end":{"line":5,"column":9},"skip":true}]},"5":{"line":5,"type":"binary-expr","locations":[{"start":{"line":5,"column":13},"end":{"line":5,"column":41},"skip":true},{"start":{"line":5,"column":45},"end":{"line":5,"column":55},"skip":true}]},"6":{"line":14,"type":"binary-expr","locations":[{"start":{"line":14,"column":7},"end":{"line":14,"column":20}},{"start":{"line":14,"column":25},"end":{"line":14,"column":43}}]},"7":{"line":32,"type":"if","locations":[{"start":{"line":32,"column":16},"end":{"line":32,"column":16}},{"start":{"line":32,"column":16},"end":{"line":32,"column":16}}]},"8":{"line":32,"type":"binary-expr","locations":[{"start":{"line":32,"column":20},"end":{"line":32,"column":44}},{"start":{"line":32,"column":48},"end":{"line":32,"column":74}}]},"9":{"line":35,"type":"if","locations":[{"start":{"line":35,"column":21},"end":{"line":35,"column":21}},{"start":{"line":35,"column":21},"end":{"line":35,"column":21}}]},"10":{"line":38,"type":"if","locations":[{"start":{"line":38,"column":21},"end":{"line":38,"column":21}},{"start":{"line":38,"column":21},"end":{"line":38,"column":21}}]},"11":{"line":47,"type":"cond-expr","locations":[{"start":{"line":47,"column":47},"end":{"line":47,"column":57}},{"start":{"line":47,"column":60},"end":{"line":47,"column":72}}]}},"inputSourceMap":{"version":3,"file":"inline.js","sourceRoot":"customSourceRoot","sources":["basic.ts"],"names":["AType","Foo","Foo.constructor","Foo.method","Foo.ternary"],"mappings":";;;;;;;;IAEA,WAAY,KAAK;QACbA,2BAAKA,CAAAA;QACLA,2BAACA,CAAAA;QACDA,2BAACA,CAAAA;QACDA,2BAACA,CAAAA;IACLA,CAACA,EALW,aAAK,KAAL,aAAK,QAKhB;IALD,IAAY,KAAK,GAAL,aAKX,CAAA;IAED;QACIC,aAAaA,OAAYA;YAKzBC,QAAGA,GAAWA,KAAKA,CAACA;YACZA,UAAKA,GAAUA,EAAEA,CAACA;YAoB1BA,MAACA,GAAUA,KAAKA,CAACA,CAACA,CAACA;YAzBfA,GAAGA,CAACA,CAACA,GAAGA,CAACA,GAAGA,IAAIA,OAAOA,CAACA,CAACA,CAACA;gBACfA,IAAKA,CAACA,GAAGA,CAACA,GAAGA,OAAOA,CAACA,GAAGA,CAACA,CAACA;YACrCA,CAACA;QACLA,CAACA;QAGDD,oB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,"17":1,"18":2,"19":2,"20":2,"21":2,"22":2,"25":1,"26":1,"27":1,"28":1,"29":5,"31":1,"32":5,"33":1,"35":4,"36":3,"38":1,"39":1,"42":0,"46":1,"47":2,"49":1,"51":1,"52":1,"53":1}}}