Skip to content

Commit

Permalink
Support reporters
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jan 30, 2024
1 parent 60d95a1 commit 2c70756
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ jobs:
run: |
npm install
- name: Lint
run: |
npm run lint
- name: Run tests
run: |
npm run test
mkdir reports
npm run unit -- --reporter spec --reporter junit:reports/${{GITHUB_ACTION$}}.xml
- name: Test Report
uses: dorny/test-reporter@v1
if: success() || failure() # run this step even if previous step failed
with:
name: Tests # Name of the check run which will be created
path: reports/*.xml # Path to test results
reporter: jest-junit # Format of test results
41 changes: 32 additions & 9 deletions borp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#! /usr/bin/env node

import { parseArgs } from 'node:util'
import { tap, spec } from 'node:test/reporters'
import Reporters from 'node:test/reporters'
import { mkdtemp, rm, readFile } from 'node:fs/promises'
import { createWriteStream } from 'node:fs'
import { finished } from 'node:stream/promises'
import { join, relative } from 'node:path'
import posix from 'node:path/posix'
Expand All @@ -11,13 +12,10 @@ import { Report } from 'c8'
import os from 'node:os'
import { execa } from 'execa'

let reporter
/* c8 ignore next 4 */
if (process.stdout.isTTY) {
const reporters = {
...Reporters,
/* eslint new-cap: "off" */
reporter = new spec()
} else {
reporter = tap
spec: new Reporters.spec()
}

const args = parseArgs({
Expand All @@ -32,7 +30,15 @@ const args = parseArgs({
'coverage-exclude': { type: 'string', short: 'X', multiple: true },
ignore: { type: 'string', short: 'i', multiple: true },
'expose-gc': { type: 'boolean' },
help: { type: 'boolean', short: 'h' }
help: { type: 'boolean', short: 'h' },
reporter: {
type: 'string',
short: 'r',
default: [
/* c8 ignore next 1 */
process.stdout.isTTY ? 'spec' : 'tap'
],
multiple: true },
},
allowPositionals: true
})
Expand Down Expand Up @@ -79,13 +85,30 @@ const config = {
}

try {
const pipes = []
for (const input of args.values.reporter) {
const [name, dest] = input.split(':')
const reporter = reporters[name]
if (!reporter) {
throw new Error(`Unknown reporter: ${name}`)
}
let output = process.stdout
if (dest) {
output = createWriteStream(dest)
}
pipes.push([reporter, output])
}

const stream = await runWithTypeScript(config)

stream.on('test:fail', () => {
process.exitCode = 1
})

stream.compose(reporter).pipe(process.stdout)

for (const [reporter, output] of pipes) {
stream.compose(reporter).pipe(output)
}

await finished(stream)

Expand Down

0 comments on commit 2c70756

Please sign in to comment.