-
Notifications
You must be signed in to change notification settings - Fork 92
feat: switch to using Node's built in coverage #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
const Exclude = require('test-exclude') | ||
const libCoverage = require('istanbul-lib-coverage') | ||
const libReport = require('istanbul-lib-report') | ||
const reports = require('istanbul-reports') | ||
const {readdirSync, readFileSync} = require('fs') | ||
const {resolve} = require('path') | ||
const { readdirSync, readFileSync } = require('fs') | ||
const { resolve } = require('path') | ||
const v8CoverageMerge = require('v8-coverage-merge') | ||
const v8toIstanbul = require('v8-to-istanbul') | ||
|
||
class Report { | ||
constructor ({reporter, coverageDirectory, watermarks}) { | ||
constructor ({ | ||
exclude, | ||
include, | ||
reporter, | ||
coverageDirectory, | ||
watermarks, | ||
resolve | ||
}) { | ||
this.reporter = reporter | ||
this.coverageDirectory = coverageDirectory | ||
this.watermarks = watermarks | ||
this.resolve = resolve | ||
this.exclude = Exclude({ | ||
exclude: exclude, | ||
include: include | ||
}) | ||
} | ||
run () { | ||
const map = this._getCoverageMapFromAllCoverageFiles() | ||
|
@@ -25,9 +40,34 @@ class Report { | |
} | ||
_getCoverageMapFromAllCoverageFiles () { | ||
const map = libCoverage.createCoverageMap({}) | ||
const mergedResults = {} | ||
this._loadReports().forEach((report) => { | ||
report.result.forEach((result) => { | ||
if (this.exclude.shouldInstrument(result.url)) { | ||
if (mergedResults[result.url]) { | ||
mergedResults[result.url] = v8CoverageMerge( | ||
mergedResults[result.url], | ||
result | ||
) | ||
} else { | ||
mergedResults[result.url] = result | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
this._loadReports().forEach(function (report) { | ||
map.merge(report) | ||
Object.keys(mergedResults).forEach((url) => { | ||
const result = mergedResults[url] | ||
// console.info(JSON.stringify(result, null, 2)) | ||
try { | ||
const path = resolve(this.resolve, result.url) | ||
const script = v8toIstanbul(path) | ||
script.applyCoverage(result.functions) | ||
map.merge(script.toIstanbul()) | ||
} catch (err) { | ||
// most likely this was an internal Node.js library. | ||
if (err.code !== 'ENOENT' && err.code !== 'EISDIR') throw err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TimothyGu any thoughts about how to easily tell whether the coverage outputted was for one of Node.js' built in libraries, e.g., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about checking whether the URL was an absolute path or not? |
||
} | ||
}) | ||
|
||
return map | ||
|
This file was deleted.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
const a = 'apple' ? 'banana' : 'grape' | ||
|
||
function cool () { | ||
|
||
} | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const {spawnSync} = require('child_process') | ||
{ | ||
const output = spawnSync(process.execPath, ['./test/fixtures/subprocess', | ||
'1']) | ||
console.info(output.stdout.toString('utf8')) | ||
} | ||
|
||
{ | ||
const output = spawnSync(process.execPath, ['./test/fixtures/subprocess', | ||
'2']) | ||
console.info(output.stdout.toString('utf8')) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require('./timeout') | ||
require('./async') | ||
|
||
console.info('i am a line of code') | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function first () { | ||
console.info('first') | ||
} | ||
|
||
function second () { | ||
console.info('second') | ||
} | ||
|
||
if (process.argv[2] === '1') { | ||
first() | ||
} | ||
|
||
if (process.argv[2] === '2') { | ||
second() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
/* global describe, it */ | ||
|
||
const {spawnSync} = require('child_process') | ||
const { spawnSync } = require('child_process') | ||
const c8Path = require.resolve('../bin/c8') | ||
|
||
require('chai').should() | ||
|
||
describe('c8', () => { | ||
it('reports coverage for script that exits normally', () => { | ||
const {output} = spawnSync(c8Path, [ | ||
const { output } = spawnSync(c8Path, [ | ||
'--exclude="test/*.js"', | ||
process.execPath, | ||
require.resolve('./fixtures/normal') | ||
], { | ||
env: process.env, | ||
cwd: process.cwd() | ||
}) | ||
]) | ||
output.toString('utf8').should.include(` | ||
------------|----------|----------|----------|----------|----------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | | ||
------------|----------|----------|----------|----------|----------------| | ||
All files | 91.67 | 100 | 80 | 91.67 | | | ||
normal.js | 85.71 | 100 | 50 | 85.71 | 14,15,16 | | ||
timeout.js | 100 | 100 | 100 | 100 | | | ||
------------|----------|----------|----------|----------|----------------|`) | ||
-----------|----------|----------|----------|----------|-------------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | | ||
-----------|----------|----------|----------|----------|-------------------| | ||
All files | 91.18 | 88.89 | 0 | 91.18 | | | ||
async.js | 100 | 100 | 100 | 100 | | | ||
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 | | ||
-----------|----------|----------|----------|----------|-------------------|`) | ||
}) | ||
|
||
it('merges reports from subprocesses together', () => { | ||
const { output } = spawnSync(c8Path, [ | ||
'--exclude="test/*.js"', | ||
process.execPath, | ||
require.resolve('./fixtures/multiple-spawn') | ||
]) | ||
output.toString('utf8').should.include(` | ||
-------------------|----------|----------|----------|----------|-------------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | | ||
-------------------|----------|----------|----------|----------|-------------------| | ||
All files | 100 | 77.78 | 100 | 100 | | | ||
multiple-spawn.js | 100 | 100 | 100 | 100 | | | ||
subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 | | ||
-------------------|----------|----------|----------|----------|-------------------|`) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be required for the Node.js codebase itself.