Skip to content

Commit

Permalink
test rewriting coverage path based on source maps
Browse files Browse the repository at this point in the history
Generate coverage reports for files without source maps or with multiple
sources.

Refactor source map tests a bit now that three covered fixtures are loaded.
  • Loading branch information
novemberborn committed Dec 7, 2015
1 parent d4b6393 commit 27c72c4
Show file tree
Hide file tree
Showing 3 changed files with 405 additions and 37 deletions.
29 changes: 20 additions & 9 deletions test/fixtures/_generateCoverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ var path = require('path')

var _ = require('lodash')
var rimraf = require('rimraf')
var sourceMapFixtures = require('source-map-fixtures')

var NYC = require('../../')

// Load the 'branching' source map fixture.
var fixture = require('source-map-fixtures').inline('branching')
// Load source map fixtures.
var fixtures = {
bundle: sourceMapFixtures.inline('bundle'),
inline: sourceMapFixtures.inline('branching'),
none: sourceMapFixtures.none('branching')
}

// Inject nyc into this process.
var nyc = (new NYC({
Expand All @@ -24,7 +29,9 @@ nyc.exclude = []

// Require the fixture so nyc can instrument it, then run it so there's code
// coverage.
fixture.require().run()
fixtures.bundle.require().branching()
fixtures.inline.require().run()
fixtures.none.require().run()

// Copy NYC#writeCoverageFile() behavior to get the coverage object, before
// source maps have been applied.
Expand All @@ -36,13 +43,17 @@ if (!coverage) {
}

var reports = _.values(coverage)
if (reports.length !== 1) {
console.error('Expected 1 report to be generated, got ' + reports.length)
if (reports.length !== 3) {
console.error('Expected 3 reports to be generated, got ' + reports.length)
process.exit(1)
}

fs.writeFileSync(
path.join(__dirname, 'coverage.js'),
'// Generated using node test/fixtures/_generateCoverage.js\n' +
'exports[' + JSON.stringify(reports[0].path) + '] = ' + JSON.stringify(reports[0], null, 2) + '\n')
var out = fs.createWriteStream(path.join(__dirname, 'coverage.js'))
out.write('// Generated using node test/fixtures/_generateCoverage.js\n')
reports.forEach(function (coverage) {
out.write('exports[' + JSON.stringify(coverage.path) + '] = ' + JSON.stringify(coverage, null, 2) + '\n')
})
out.end()
out.on('finish', function () {
console.log('Written coverage report.')
})
326 changes: 326 additions & 0 deletions test/fixtures/coverage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,215 @@
// Generated using node test/fixtures/_generateCoverage.js
exports["./node_modules/source-map-fixtures/fixtures/bundle-inline.js"] = {
"path": "./node_modules/source-map-fixtures/fixtures/bundle-inline.js",
"s": {
"1": 1,
"2": 1,
"3": 0,
"4": 1,
"5": 0,
"6": 1,
"7": 1,
"8": 0,
"9": 1,
"10": 1,
"11": 1
},
"b": {
"1": [
0,
1
]
},
"f": {
"1": 0,
"2": 0,
"3": 1
},
"fnMap": {
"1": {
"name": "(anonymous_1)",
"line": 6,
"loc": {
"start": {
"line": 6,
"column": 8
},
"end": {
"line": 6,
"column": 20
}
}
},
"2": {
"name": "(anonymous_2)",
"line": 10,
"loc": {
"start": {
"line": 10,
"column": 8
},
"end": {
"line": 10,
"column": 20
}
}
},
"3": {
"name": "(anonymous_3)",
"line": 14,
"loc": {
"start": {
"line": 14,
"column": 8
},
"end": {
"line": 14,
"column": 21
}
}
}
},
"statementMap": {
"1": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 5,
"column": 3
}
},
"2": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 8,
"column": 2
}
},
"3": {
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 51
}
},
"4": {
"start": {
"line": 10,
"column": 0
},
"end": {
"line": 12,
"column": 2
}
},
"5": {
"start": {
"line": 11,
"column": 2
},
"end": {
"line": 11,
"column": 12
}
},
"6": {
"start": {
"line": 14,
"column": 0
},
"end": {
"line": 18,
"column": 2
}
},
"7": {
"start": {
"line": 15,
"column": 2
},
"end": {
"line": 17,
"column": 3
}
},
"8": {
"start": {
"line": 16,
"column": 4
},
"end": {
"line": 16,
"column": 16
}
},
"9": {
"start": {
"line": 20,
"column": 0
},
"end": {
"line": 20,
"column": 22
}
},
"10": {
"start": {
"line": 21,
"column": 0
},
"end": {
"line": 21,
"column": 19
}
},
"11": {
"start": {
"line": 22,
"column": 0
},
"end": {
"line": 22,
"column": 19
}
}
},
"branchMap": {
"1": {
"line": 15,
"type": "if",
"locations": [
{
"start": {
"line": 15,
"column": 2
},
"end": {
"line": 15,
"column": 2
}
},
{
"start": {
"line": 15,
"column": 2
},
"end": {
"line": 15,
"column": 2
}
}
]
}
}
}
exports["./node_modules/source-map-fixtures/fixtures/branching-inline.js"] = {
"path": "./node_modules/source-map-fixtures/fixtures/branching-inline.js",
"s": {
Expand Down Expand Up @@ -114,3 +325,118 @@ exports["./node_modules/source-map-fixtures/fixtures/branching-inline.js"] = {
}
}
}
exports["./node_modules/source-map-fixtures/fixtures/branching-none.js"] = {
"path": "./node_modules/source-map-fixtures/fixtures/branching-none.js",
"s": {
"1": 1,
"2": 1,
"3": 1,
"4": 0,
"5": 1
},
"b": {
"1": [
0,
1
]
},
"f": {
"1": 1
},
"fnMap": {
"1": {
"name": "(anonymous_1)",
"line": 6,
"loc": {
"start": {
"line": 6,
"column": 8
},
"end": {
"line": 6,
"column": 21
}
}
}
},
"statementMap": {
"1": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 5,
"column": 3
}
},
"2": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 10,
"column": 2
}
},
"3": {
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 9,
"column": 3
}
},
"4": {
"start": {
"line": 8,
"column": 4
},
"end": {
"line": 8,
"column": 16
}
},
"5": {
"start": {
"line": 11,
"column": 0
},
"end": {
"line": 11,
"column": 16
}
}
},
"branchMap": {
"1": {
"line": 7,
"type": "if",
"locations": [
{
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 2
}
},
{
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 2
}
}
]
}
}
}
Loading

0 comments on commit 27c72c4

Please sign in to comment.