Skip to content

Commit

Permalink
Merge 1d9931e into 5947d2d
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed Jun 17, 2020
2 parents 5947d2d + 1d9931e commit afdfac9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/output-files.js
Expand Up @@ -14,6 +14,7 @@ let iterator = {}
class OutputFiles {
constructor (coverageInfo, options = {}) {
this.storagePath = options.storagePath || './.nyc_output/js'
this.filePathTransformer = options.transformPath || ((filePath) => filePath)
this.includeHostname = options.hasOwnProperty('includeHostname') ? options.includeHostname : true

// Clone coverageInfo to prevent mutating the passed in data
Expand Down
13 changes: 9 additions & 4 deletions lib/puppeteer-to-istanbul.js
Expand Up @@ -9,6 +9,7 @@ let jsonPart = {}
class PuppeteerToIstanbul {
constructor (coverageInfo, options = {}) {
this.storagePath = options.storagePath || './.nyc_output'
this.filePathTransformer = options.transformPath || ((filePath) => filePath)
this.includeHostname = options.hasOwnProperty('includeHostname') ? options.includeHostname : true

this.coverageInfo = coverageInfo
Expand Down Expand Up @@ -37,13 +38,17 @@ class PuppeteerToIstanbul {
let istanbulCoverage = script.toIstanbul()
let keys = Object.keys(istanbulCoverage)

if (jsonPart[keys[0]]) {
const istanbulKey = keys[0]
const filePath = this.filePathTransformer(istanbulKey)

if (jsonPart[filePath]) {
// Merge coverage records
mergeCoverageData(jsonPart[keys[0]].s, istanbulCoverage[keys[0]].s)
mergeCoverageData(jsonPart[filePath].s, istanbulCoverage[istanbulKey].s)
} else {
jsonPart[keys[0]] = istanbulCoverage[keys[0]]
jsonPart[filePath] = istanbulCoverage[istanbulKey]
}
jsonPart[keys[0]].originalUrl = jsFile.originalUrl
jsonPart[filePath].originalUrl = jsFile.originalUrl
jsonPart[filePath].path = filePath
})

fs.writeSync(fd, '{')
Expand Down
31 changes: 31 additions & 0 deletions test/puppeteer-to-istanbul.js
Expand Up @@ -27,6 +27,37 @@ describe('puppeteer-to-istanbul', () => {
fs.unlinkSync('.nyc_output/custom/out.json')
})

it('outputs a valid out.json file, with a custom path name', async () => {
const fixture = require('./fixtures/two-inline.json')

const ptiA = PuppeteerToIstanbul(fixture)
ptiA.writeIstanbulFormat()
const contentA = fs.readFileSync('.nyc_output/out.json', 'utf8')
const jsonObjectA = JSON.parse(contentA)
should.exist(jsonObjectA)
const keysA = Object.keys(jsonObjectA)
let firstKeyA = keysA[0]
if (firstKeyA.includes('\\')) firstKeyA = firstKeyA.replace(/\\/g, '/')
firstKeyA.should.include('.nyc_output/js/tmp/puppeteerTemp.htmlpuppeteerTemp-inline.js')
keysA[0].should.equal(jsonObjectA[keysA[0]].path)
fs.unlinkSync('.nyc_output/out.json')

PuppeteerToIstanbul.resetJSONPart()
OutputFiles.resetIterator()

const ptiB = PuppeteerToIstanbul(fixture, { transformPath: (path) => path.replace('.nyc_output', '.nyc_input') })
ptiB.writeIstanbulFormat()
const contentB = fs.readFileSync('.nyc_output/out.json', 'utf8')
const jsonObjectB = JSON.parse(contentB)
should.exist(jsonObjectB)
const keysB = Object.keys(jsonObjectB)
let firstKeyB = keysB[0]
if (firstKeyB.includes('\\')) firstKeyB = firstKeyB.replace(/\\/g, '/')
firstKeyB.should.include('.nyc_input/js/tmp/puppeteerTemp.htmlpuppeteerTemp-inline-1.js')
keysB[0].should.equal(jsonObjectB[keysB[0]].path)
fs.unlinkSync('.nyc_output/out.json')
})

it('correctly sets coverage info', () => {
const fixture = require('./fixtures/two-inline.json')
const pti = PuppeteerToIstanbul(fixture)
Expand Down

0 comments on commit afdfac9

Please sign in to comment.