Skip to content

Commit

Permalink
feat: allow the output file to not include the hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed May 31, 2020
1 parent b3c12ca commit 72a5622
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Convert coverage from the format outputted by [puppeteer](https://developers.goo
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
pti.write([...jsCoverage, ...cssCoverage])
pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true })
await browser.close()
})()
```
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const PuppeteerToIstanbul = require('./lib/puppeteer-to-istanbul')

module.exports = {
write: (puppeteerFormat) => {
const pti = PuppeteerToIstanbul(puppeteerFormat)
write: (puppeteerFormat, options) => {
const pti = PuppeteerToIstanbul(puppeteerFormat, options)
pti.writeIstanbulFormat()
}
}
9 changes: 5 additions & 4 deletions lib/output-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const storagePath = './.nyc_output/js'
let iterator = {}

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

let postProtocolPath = urlPath.pathname.substring(1)

if (urlPath.hostname) {
if (urlPath.hostname && this.includeHostname) {
let hostnameAndPort = urlPath.hostname
if (urlPath.port) {
hostnameAndPort = hostnameAndPort + '_' + urlPath.port
Expand Down Expand Up @@ -90,8 +91,8 @@ class OutputFiles {
}
}

function genOutputFiles (coverageInfo) {
return new OutputFiles(coverageInfo)
function genOutputFiles (coverageInfo, options) {
return new OutputFiles(coverageInfo, options)
}

genOutputFiles.resetIterator = function () {
Expand Down
8 changes: 4 additions & 4 deletions lib/puppeteer-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const v8toIstanbul = require('v8-to-istanbul')
let jsonPart = {}

class PuppeteerToIstanbul {
constructor (coverageInfo) {
constructor (coverageInfo, options) {
this.coverageInfo = coverageInfo
this.puppeteerToConverter = OutputFiles(coverageInfo).getTransformedCoverage()
this.puppeteerToConverter = OutputFiles(coverageInfo, options).getTransformedCoverage()
this.puppeteerToV8Info = PuppeteerToV8(this.puppeteerToConverter).convertCoverage()
}

Expand Down Expand Up @@ -61,8 +61,8 @@ function mergeCoverageData (obja, objb) {
return obja
}

function genPuppeteerToIstanbul (coverageInfo) {
return new PuppeteerToIstanbul(coverageInfo)
function genPuppeteerToIstanbul (coverageInfo, options) {
return new PuppeteerToIstanbul(coverageInfo, options)
}

genPuppeteerToIstanbul.resetJSONPart = function () {
Expand Down
10 changes: 10 additions & 0 deletions test/output-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ describe('output-files', () => {
coverageInfo[3].url.should.include('js/views/record.js')
})

it('appropriately handles modules required via http/https, with hostName excluded', () => {
const fixture = require('./fixtures/http-es6-modules.json')
const coverageInfo = OutputFiles(fixture, { includeHostname: false }).getTransformedCoverage().map(info => ({...info, url: info.url.replace(/\\/g, '/')}))

coverageInfo[0].url.should.include('js/index.js')
coverageInfo[1].url.should.include('js/utils/doc_ready.js')
coverageInfo[2].url.should.include('js/models/record.js')
coverageInfo[3].url.should.include('js/views/record.js')
})

it('maintains original url in output', () => {
const fixture = require('./fixtures/http-es6-modules.json')
const coverageInfo = OutputFiles(fixture).getTransformedCoverage()
Expand Down

0 comments on commit 72a5622

Please sign in to comment.