Skip to content

Commit ca1d8fd

Browse files
committed
fix: make specific JSON reporter that respects --show-uncovered param
1 parent 7ced56a commit ca1d8fd

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/cli/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { program } from './program.js'
55
import { read } from './file-reader.js'
66
import { print as pretty } from './reporters/pretty.js'
77
import { print as tap } from './reporters/tap.js'
8+
import { print as json } from './reporters/json.js'
89
import { help } from './help.js'
910

1011
async function cli(cli_args: string[]) {
@@ -30,8 +31,7 @@ async function cli(cli_args: string[]) {
3031
return tap(report, params)
3132
}
3233
if (params.reporter === 'json') {
33-
let log = JSON.stringify(report)
34-
return report.report.ok ? console.log(log) : console.error(log)
34+
return json(report, params)
3535
}
3636
return pretty(report, params)
3737
}

src/cli/reporters/json.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { CliArguments } from '../arguments.js'
2+
import type { Report } from '../program.js'
3+
4+
function prepare({ report, context }: Report, params: CliArguments) {
5+
context.coverage.coverage_per_stylesheet = context.coverage.coverage_per_stylesheet.filter((sheet) => {
6+
// Include if the user wanted min-file-coverage and this file coverage is too low
7+
if (
8+
params['show-uncovered'] === 'violations' &&
9+
report.min_file_line_coverage.expected !== undefined &&
10+
sheet.line_coverage_ratio < report.min_file_line_coverage.expected
11+
) {
12+
return true
13+
}
14+
15+
// Include if show=all and coverage isn't 100%
16+
if (params['show-uncovered'] === 'all' && sheet.line_coverage_ratio < 1) {
17+
return true
18+
}
19+
20+
// Skip the sheet if show=none or coverage is higher than requested
21+
return false
22+
})
23+
24+
return {
25+
report,
26+
context,
27+
}
28+
}
29+
30+
export function print({ report, context }: Report, params: CliArguments): void {
31+
let logger = report.ok ? console.log : console.error
32+
let data = prepare({ context, report }, params)
33+
logger(JSON.stringify(data))
34+
}

0 commit comments

Comments
 (0)