Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
feat(jsonReporter): implement first iteration of json reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Schmidt committed Feb 11, 2019
1 parent 1d8ec88 commit 23f7f83
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const logError = require('./utils/logError')
program
.version(pkg.version)
.option('-t, --threshold <number>', 'Specify a custom threshold')
.option('-P, --pretty', 'Prettify the output (only with `json` reporter)')
.option('-r, --reporters <reporters>', 'Specify custom reporters (multiple allowed with comma)')
.option('-c, --concurrency <numberOfFiles>', 'Number of files that are handled at a time')
.option('-v, --verbose', 'Run `flow-cov` in verbose mode')
Expand All @@ -30,6 +31,10 @@ async function run() {
config.reporters = program.reporters.split(',')
}

if (program.pretty) {
config.pretty = program.pretty
}

const report = await runCoverageTool(config)

if (report.coverageSatisfied) {
Expand Down
36 changes: 36 additions & 0 deletions lib/reporters/__test__/json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const report = {
filesCoverage: [],
coverage: 100,
coverageSatisfied: true,
flowPassed: true,
config: { threshold: 90 },
}

describe('jsonReporter', () => {
beforeEach(() => {
global.console = { log: () => {} }
})

test('returns the right reporterName', () => {
const reporter = require('../json')
const response = reporter(report)

expect(response.reporterName).toBe('json')
})

test('returns valid stringified json', () => {
const reporter = require('../json')
const response = reporter(report)

expect(() => JSON.parse(response.contents)).not.toThrow()
expect(JSON.parse(response.contents).coverageSatisfied).toEqual(true)
})

test('works with a pretty option', () => {
const reporter = require('../json')
const response = reporter({ ...report, config: { ...report.config, pretty: true } })

expect(() => JSON.parse(response.contents)).not.toThrow()
expect(JSON.parse(response.contents).coverageSatisfied).toEqual(true)
})
})
3 changes: 3 additions & 0 deletions lib/reporters/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const textReporter = require('./text')
const jsonReporter = require('./json')

module.exports = function launchReporter(type, report) {
switch (type) {
case 'json':
return jsonReporter(report)
case 'text':
return textReporter(report)
default:
Expand Down
10 changes: 10 additions & 0 deletions lib/reporters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function jsonReporter(report) {
const contents = JSON.stringify(report, null, report.config.pretty ? 2 : 0)

console.log(contents)

return {
reporterName: 'json',
contents,
}
}

0 comments on commit 23f7f83

Please sign in to comment.