Skip to content

Commit 238eb1b

Browse files
committed
implement md reporter
Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent e29e731 commit 238eb1b

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ jobs:
1919
node-version: [18.x, 20.x, 21.x]
2020
os: [ubuntu-latest, windows-latest]
2121
steps:
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2323

2424
- name: Use Node.js
25-
uses: actions/setup-node@v2
25+
uses: actions/setup-node@v3
2626
with:
2727
node-version: ${{ matrix.node-version }}
2828

@@ -36,8 +36,8 @@ jobs:
3636
3737
- name: Run tests
3838
run: |
39-
npm run unit -- --reporter spec --reporter tap:report.tap
39+
npm run unit -- --reporter spec --reporter md:report.md
4040
41-
- uses: pcolby/tap-summary@v1
42-
with:
43-
path: report.tap
41+
- name: Upload report
42+
run: |
43+
echo report.md >> "$GITHUB_OUTPUT"

borp.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { finished } from 'node:stream/promises'
88
import { join, relative } from 'node:path'
99
import posix from 'node:path/posix'
1010
import runWithTypeScript from './lib/run.js'
11+
import { MarkdownReporter } from './lib/reporter.js'
1112
import { Report } from 'c8'
1213
import os from 'node:os'
1314
import { execa } from 'execa'
1415

15-
const reporters = {
16-
...Reporters,
17-
/* eslint new-cap: "off" */
18-
spec: new Reporters.spec()
19-
}
16+
process.on('unhandledRejection', (err) => {
17+
console.error(err)
18+
process.exit(1)
19+
})
2020

2121
const args = parseArgs({
2222
args: process.argv.slice(2),
@@ -87,6 +87,14 @@ const config = {
8787

8888
try {
8989
const pipes = []
90+
91+
const reporters = {
92+
...Reporters,
93+
md: new MarkdownReporter(config),
94+
/* eslint new-cap: "off" */
95+
spec: new Reporters.spec()
96+
}
97+
9098
for (const input of args.values.reporter) {
9199
const [name, dest] = input.split(':')
92100
const reporter = reporters[name]

lib/reporter.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Transform } from 'node:stream'
2+
import { fileURLToPath } from 'node:url'
3+
4+
function normalizeFile (file, cwd) {
5+
let res = file
6+
if (file.startsWith('file://')) {
7+
res = fileURLToPath(new URL(file))
8+
}
9+
res = res.replace(cwd, '')
10+
if (res.startsWith('/')) {
11+
res = res.slice(1)
12+
}
13+
return res
14+
}
15+
16+
function eventToLine (event) {
17+
return `* __${event.data.name}__, duration ${event.data.details.duration_ms}ms, line ${event.data.line} \n`
18+
}
19+
20+
export class MarkdownReporter extends Transform {
21+
constructor ({ cwd }) {
22+
super({
23+
objectMode: true
24+
})
25+
26+
this._files = {}
27+
this._cwd = cwd
28+
}
29+
30+
getFile (path) {
31+
const file = this._files[path] || {
32+
pass: [],
33+
fail: []
34+
}
35+
this._files[path] = file
36+
return file
37+
}
38+
39+
_transform (event, encoding, callback) {
40+
if (!event.data.file) {
41+
callback()
42+
return
43+
}
44+
45+
const path = normalizeFile(event.data.file, this._cwd)
46+
const file = this.getFile(path)
47+
switch (event.type) {
48+
case 'test:pass':
49+
file.pass.push(event)
50+
break
51+
case 'test:fail':
52+
file.fail.push(event)
53+
break
54+
}
55+
56+
callback()
57+
}
58+
59+
_flush (callback) {
60+
this.push('# Summary\n')
61+
for (const [path, file] of Object.entries(this._files)) {
62+
this.push(`# ${path}\n`)
63+
this.push('## Pass\n')
64+
for (const event of file.pass) {
65+
this.push(eventToLine(event))
66+
}
67+
this.push('## Fail\n')
68+
for (const event of file.fail) {
69+
this.push(eventToLine(event))
70+
}
71+
}
72+
this.push(null)
73+
callback()
74+
}
75+
}

0 commit comments

Comments
 (0)