Skip to content

Commit 48db9dd

Browse files
committed
feat: allow a custom storagePath
1 parent b3c12ca commit 48db9dd

4 files changed

Lines changed: 24 additions & 10 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], { storagePath: './.nyc_output' })
3737
await browser.close()
3838
})()
3939
```

index.js

Lines changed: 3 additions & 3 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)
6-
pti.writeIstanbulFormat()
4+
write: (puppeteerFormat, options) => {
5+
const pti = PuppeteerToIstanbul(puppeteerFormat, options)
6+
pti.writeIstanbulFormat(options)
77
}
88
}

lib/puppeteer-to-istanbul.js

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

99
class PuppeteerToIstanbul {
10-
constructor (coverageInfo) {
10+
constructor (coverageInfo, options) {
11+
options = options || {}
12+
options.storagePath = options.storagePath || './.nyc_output'
13+
1114
this.coverageInfo = coverageInfo
15+
this.options = options
1216
this.puppeteerToConverter = OutputFiles(coverageInfo).getTransformedCoverage()
1317
this.puppeteerToV8Info = PuppeteerToV8(this.puppeteerToConverter).convertCoverage()
1418
}
@@ -18,9 +22,9 @@ class PuppeteerToIstanbul {
1822
}
1923

2024
writeIstanbulFormat () {
21-
mkdirp.sync('./.nyc_output')
25+
mkdirp.sync(this.options.storagePath)
2226

23-
const outFilePath = './.nyc_output/out.json'
27+
const outFilePath = `${this.options.storagePath}/out.json`
2428

2529
fs.writeFileSync(outFilePath, '')
2630

@@ -61,8 +65,8 @@ function mergeCoverageData (obja, objb) {
6165
return obja
6266
}
6367

64-
function genPuppeteerToIstanbul (coverageInfo) {
65-
return new PuppeteerToIstanbul(coverageInfo)
68+
function genPuppeteerToIstanbul (coverageInfo, options) {
69+
return new PuppeteerToIstanbul(coverageInfo, options)
6670
}
6771

6872
genPuppeteerToIstanbul.resetJSONPart = function () {

test/puppeteer-to-istanbul.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const OutputFiles = require('../lib/output-files')
77
var PuppeteerToIstanbul = require('../lib/puppeteer-to-istanbul')
88

99
describe('puppeteer-to-istanbul', () => {
10-
it('outputs a valid out.json file', () => {
10+
it('outputs a valid out.json file, to the default location', () => {
1111
const fixture = require('./fixtures/two-inline.json')
1212
const pti = PuppeteerToIstanbul(fixture)
1313
pti.writeIstanbulFormat()
@@ -17,6 +17,16 @@ describe('puppeteer-to-istanbul', () => {
1717
fs.unlinkSync('.nyc_output/out.json')
1818
})
1919

20+
it('outputs a valid out.json file, in the custom location', () => {
21+
const fixture = require('./fixtures/two-inline.json')
22+
const pti = PuppeteerToIstanbul(fixture, { storagePath: '.nyc_output/custom' })
23+
pti.writeIstanbulFormat()
24+
const content = fs.readFileSync('.nyc_output/custom/out.json', 'utf8')
25+
const jsonObject = JSON.parse(content)
26+
should.exist(jsonObject)
27+
fs.unlinkSync('.nyc_output/custom/out.json')
28+
})
29+
2030
it('correctly sets coverage info', () => {
2131
const fixture = require('./fixtures/two-inline.json')
2232
const pti = PuppeteerToIstanbul(fixture)

0 commit comments

Comments
 (0)