Skip to content

Commit 43bef5f

Browse files
authored
support local custom reporters (#69)
1 parent 3433a43 commit 43bef5f

File tree

7 files changed

+77
-2
lines changed

7 files changed

+77
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ borp --coverage
1818

1919
# with check coverage active
2020
borp --coverage --check-coverage --lines 95
21+
22+
# with a node_modules located reporter
23+
borp --reporter foo
24+
25+
# with a node_modules located reporter writing to stderr
26+
borp --reporter foo:stderr
27+
28+
# with a local custom reporter
29+
borp --reporter ./lib/some-reporter.mjs
2130
```
2231

2332
Borp will automatically run all tests files matching `*.test.{js|ts}`.
@@ -98,7 +107,7 @@ Note the use of `incremental: true`, which speed up compilation massively.
98107
* `--ignore` or `-i`, ignore a glob pattern, and not look for tests there
99108
* `--expose-gc`, exposes the gc() function to tests
100109
* `--pattern` or `-p`, run tests matching the given glob pattern
101-
* `--reporter` or `-r`, set up a reporter, use a colon to set a file destination. Default: `spec`.
110+
* `--reporter` or `-r`, set up a reporter, use a colon to set a file destination. Reporter may either be a module name resolvable by standard `node_modules` resolution, or a path to a script relative to the process working directory (must be an ESM script). Default: `spec`.
102111
* `--no-typescript` or `-T`, disable automatic TypeScript compilation if `tsconfig.json` is found.
103112
* `--post-compile` or `-P`, the path to a file that will be executed after each typescript compilation.
104113
* `--check-coverage`, enables c8 check coverage; default is false

borp.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,19 @@ try {
114114

115115
for (const input of args.values.reporter) {
116116
const [name, dest] = input.split(':')
117-
const Ctor = reporters[name] || await import(name).then((m) => m.default || m)
117+
let Ctor
118+
if (Object.prototype.hasOwnProperty.call(reporters, name) === true) {
119+
Ctor = reporters[name]
120+
} else {
121+
try {
122+
// Try to load a custom reporter from a file relative to the process.
123+
const modPath = join(process.cwd(), name.replace(/^['"]/, '').replace(/['"]$/, ''))
124+
Ctor = await import(modPath).then((m) => m.default || m)
125+
} catch {
126+
// Fallback to trying to load the reporter from node_modules resolution.
127+
Ctor = await import(name).then((m) => m.default || m)
128+
}
129+
}
118130
const reporter = Ctor.prototype && Object.getOwnPropertyDescriptor(Ctor.prototype, 'constructor') ? new Ctor() : Ctor
119131
let output = process.stdout
120132
if (dest) {

fixtures/relative-reporter/lib/add.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function add (x, y) {
2+
return x + y
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
import { Transform } from 'node:stream'
4+
5+
const testReporter = new Transform({
6+
writableObjectMode: true,
7+
transform (event, encoding, callback) {
8+
switch (event.type) {
9+
case 'test:pass': {
10+
return callback(null, `passed: ${event.data.file}\n`)
11+
}
12+
13+
case 'test:fail': {
14+
return callback(null, `failed: ${event.data.file}\n`)
15+
}
16+
17+
default: {
18+
callback(null, null)
19+
}
20+
}
21+
}
22+
})
23+
24+
export default testReporter
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from 'node:test'
2+
import { add } from '../lib/add.js'
3+
import { strictEqual } from 'node:assert'
4+
5+
test('add', () => {
6+
strictEqual(add(1, 2), 3)
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from 'node:test'
2+
import { add } from '../lib/add.js'
3+
import { strictEqual } from 'node:assert'
4+
5+
test('add2', () => {
6+
strictEqual(add(3, 2), 5)
7+
})

test/cli.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ test('reporter from node_modules', async () => {
7474
strictEqual(stdout.indexOf('tests 2') >= 0, true)
7575
})
7676

77+
test('reporter from relative path', async () => {
78+
const cwd = join(import.meta.url, '..', 'fixtures', 'relative-reporter')
79+
const { stdout } = await execa('node', [
80+
borp,
81+
'--reporter=./fixtures/relative-reporter/reporter.js'
82+
], {
83+
cwd
84+
})
85+
86+
strictEqual(/passed:.+add\.test\.js/.test(stdout), true)
87+
strictEqual(/passed:.+add2\.test\.js/.test(stdout), true)
88+
})
89+
7790
test('gh reporter', async () => {
7891
const cwd = join(import.meta.url, '..', 'fixtures', 'js-esm')
7992
const { stdout } = await execa('node', [

0 commit comments

Comments
 (0)