Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Oct 24, 2019
1 parent d4134cb commit 64eee89
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ class Report {
return this._allCoverageFiles
}

/**
* Returns source-map and fake source file, if cached during Node.js'
* execution. This is used to support tools like ts-node, which transpile
* using runtime hooks.
*
* Note: requires Node.js 13+
*
* @return {Object} sourceMap and fake source file (created from line #s).
* @private
*/
_getSourceMap (v8ScriptCov) {
const sources = {}
if (this.sourceMapCache[`file://${v8ScriptCov.url}`]) {
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/ts-node-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface FooOptions {
x: number;
}

class Foo {
x: number;
constructor (options: FooOptions) {
this.x = options.x ? options.x : 99
if (this.x) {
console.info('covered')
} else {
console.info('uncovered')
}
this.methodC()
}
methodA (): number {
console.info('covered')
return 33
}
/* c8 ignore next 3 */
methodB () {
console.info('uncovered')
}
private methodC () {
console.info('covered')
}
methodD () {
console.info('uncovered')
}
}

const a = new Foo({x: 0})
const b = new Foo({x: 33})
a.methodA()
16 changes: 16 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,20 @@ describe('c8', () => {
})
})
})

describe('ts-node', () => {
beforeEach(cb => rimraf('tmp/source-map', cb))

it('reads source-map from cache, and applies to coverage', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
'./node_modules/.bin/ts-node',
require.resolve('./fixtures/ts-node-basic.ts')
])
output.toString('utf8').should.matchSnapshot()
})
})
})
15 changes: 15 additions & 0 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,18 @@ All files | 83.33 | 85.71 | 60 | 83.33 | |
-----------|----------|----------|----------|----------|-------------------|
,"
`;

exports[`c8 ts-node reads source-map from cache, and applies to coverage 1`] = `
",covered
covered
covered
covered
covered
------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files | 88.24 | 87.5 | 80 | 88.24 | |
ts-node-basic.ts | 88.24 | 87.5 | 80 | 88.24 | 12,13,28,29 |
------------------|----------|----------|----------|----------|-------------------|
,"
`;

0 comments on commit 64eee89

Please sign in to comment.