Skip to content

Commit 72a5622

Browse files
committed
feat: allow the output file to not include the hostname
1 parent b3c12ca commit 72a5622

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Convert coverage from the format outputted by [puppeteer](https://developers.goo
3333
page.coverage.stopJSCoverage(),
3434
page.coverage.stopCSSCoverage(),
3535
]);
36-
pti.write([...jsCoverage, ...cssCoverage])
36+
pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true })
3737
await browser.close()
3838
})()
3939
```

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const PuppeteerToIstanbul = require('./lib/puppeteer-to-istanbul')
22

33
module.exports = {
4-
write: (puppeteerFormat) => {
5-
const pti = PuppeteerToIstanbul(puppeteerFormat)
4+
write: (puppeteerFormat, options) => {
5+
const pti = PuppeteerToIstanbul(puppeteerFormat, options)
66
pti.writeIstanbulFormat()
77
}
88
}

lib/output-files.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const storagePath = './.nyc_output/js'
1313
let iterator = {}
1414

1515
class OutputFiles {
16-
constructor (coverageInfo) {
16+
constructor (coverageInfo, options = {}) {
17+
this.includeHostname = options.hasOwnProperty('includeHostname') ? options.includeHostname : true
1718
// Clone coverageInfo to prevent mutating the passed in data
1819
this.coverageInfo = clone(coverageInfo)
1920
this._parseAndIsolate()
@@ -31,7 +32,7 @@ class OutputFiles {
3132

3233
let postProtocolPath = urlPath.pathname.substring(1)
3334

34-
if (urlPath.hostname) {
35+
if (urlPath.hostname && this.includeHostname) {
3536
let hostnameAndPort = urlPath.hostname
3637
if (urlPath.port) {
3738
hostnameAndPort = hostnameAndPort + '_' + urlPath.port
@@ -90,8 +91,8 @@ class OutputFiles {
9091
}
9192
}
9293

93-
function genOutputFiles (coverageInfo) {
94-
return new OutputFiles(coverageInfo)
94+
function genOutputFiles (coverageInfo, options) {
95+
return new OutputFiles(coverageInfo, options)
9596
}
9697

9798
genOutputFiles.resetIterator = function () {

lib/puppeteer-to-istanbul.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const v8toIstanbul = require('v8-to-istanbul')
77
let jsonPart = {}
88

99
class PuppeteerToIstanbul {
10-
constructor (coverageInfo) {
10+
constructor (coverageInfo, options) {
1111
this.coverageInfo = coverageInfo
12-
this.puppeteerToConverter = OutputFiles(coverageInfo).getTransformedCoverage()
12+
this.puppeteerToConverter = OutputFiles(coverageInfo, options).getTransformedCoverage()
1313
this.puppeteerToV8Info = PuppeteerToV8(this.puppeteerToConverter).convertCoverage()
1414
}
1515

@@ -61,8 +61,8 @@ function mergeCoverageData (obja, objb) {
6161
return obja
6262
}
6363

64-
function genPuppeteerToIstanbul (coverageInfo) {
65-
return new PuppeteerToIstanbul(coverageInfo)
64+
function genPuppeteerToIstanbul (coverageInfo, options) {
65+
return new PuppeteerToIstanbul(coverageInfo, options)
6666
}
6767

6868
genPuppeteerToIstanbul.resetJSONPart = function () {

test/output-files.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ describe('output-files', () => {
7979
coverageInfo[3].url.should.include('js/views/record.js')
8080
})
8181

82+
it('appropriately handles modules required via http/https, with hostName excluded', () => {
83+
const fixture = require('./fixtures/http-es6-modules.json')
84+
const coverageInfo = OutputFiles(fixture, { includeHostname: false }).getTransformedCoverage().map(info => ({...info, url: info.url.replace(/\\/g, '/')}))
85+
86+
coverageInfo[0].url.should.include('js/index.js')
87+
coverageInfo[1].url.should.include('js/utils/doc_ready.js')
88+
coverageInfo[2].url.should.include('js/models/record.js')
89+
coverageInfo[3].url.should.include('js/views/record.js')
90+
})
91+
8292
it('maintains original url in output', () => {
8393
const fixture = require('./fixtures/http-es6-modules.json')
8494
const coverageInfo = OutputFiles(fixture).getTransformedCoverage()

0 commit comments

Comments
 (0)