Skip to content

Commit

Permalink
fix: JSON being output was not valid
Browse files Browse the repository at this point in the history
👍 thanks for the fix @SanterreJo.
  • Loading branch information
SanterreJo authored and bcoe committed Nov 5, 2019
1 parent c57bd74 commit de9109c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 4 additions & 2 deletions lib/puppeteer-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PuppeteerToIstanbul {
const fd = fs.openSync(outFilePath, 'a')
fs.writeSync(fd, '{')

this.puppeteerToV8Info.forEach(jsFile => {
this.puppeteerToV8Info.forEach((jsFile, index) => {
const script = v8toIstanbul(jsFile.url)
script.applyCoverage(jsFile.functions)

Expand All @@ -39,7 +39,9 @@ class PuppeteerToIstanbul {
let jsonStr = JSON.stringify(jsonPart)
.replace(/^{/, '')
.replace(/}$/, '')
fs.writeSync(fd, jsonStr)

const isLastIteration = index === (this.puppeteerToV8Info.length - 1)
fs.writeSync(fd, jsonStr + (isLastIteration ? '' : ','))
})

fs.writeSync(fd, '}')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"mocha": "^5.0.1",
"nyc": "^11.4.1",
"puppeteer": "^1.1.1",
"rimraf": "^3.0.0",
"standard": "^11.0.0",
"standard-version": "^4.3.0"
},
Expand Down
44 changes: 25 additions & 19 deletions test/puppeteer-to-istanbul.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// /* globals describe, it, before */

// var PuppeteerToIstanbul = require('../lib/puppeteer-to-istanbul')()

// require('chai').should()

// describe('puppeteer-to-istanbul', () => {

// const fixture = require('./fixtures/function-coverage-missing.json')

// before(() => {

// })

// it('translates ranges into lines for istanbul', () => {

// })

// })
/* globals describe, it */

const should = require('chai').should()
const fs = require('fs')

var PuppeteerToIstanbul = require('../lib/puppeteer-to-istanbul')

describe('puppeteer-to-istanbul', () => {
it('outputs a valid out.json file', () => {
const fixture = require('./fixtures/two-inline.json')
const pti = PuppeteerToIstanbul(fixture)
pti.writeIstanbulFormat()
const content = fs.readFileSync('.nyc_output/out.json', 'utf8')
const jsonObject = JSON.parse(content)
should.exist(jsonObject)
fs.unlinkSync('.nyc_output/out.json')
})

it('correctly sets coverage info', () => {
const fixture = require('./fixtures/two-inline.json')
const pti = PuppeteerToIstanbul(fixture)
pti.setCoverageInfo(fixture)
pti.coverageInfo.should.eql(fixture)
})
})

0 comments on commit de9109c

Please sign in to comment.