Skip to content

Commit

Permalink
support local custom reporters (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Jul 25, 2024
1 parent 3433a43 commit 43bef5f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ borp --coverage

# with check coverage active
borp --coverage --check-coverage --lines 95

# with a node_modules located reporter
borp --reporter foo

# with a node_modules located reporter writing to stderr
borp --reporter foo:stderr

# with a local custom reporter
borp --reporter ./lib/some-reporter.mjs
```

Borp will automatically run all tests files matching `*.test.{js|ts}`.
Expand Down Expand Up @@ -98,7 +107,7 @@ Note the use of `incremental: true`, which speed up compilation massively.
* `--ignore` or `-i`, ignore a glob pattern, and not look for tests there
* `--expose-gc`, exposes the gc() function to tests
* `--pattern` or `-p`, run tests matching the given glob pattern
* `--reporter` or `-r`, set up a reporter, use a colon to set a file destination. Default: `spec`.
* `--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`.
* `--no-typescript` or `-T`, disable automatic TypeScript compilation if `tsconfig.json` is found.
* `--post-compile` or `-P`, the path to a file that will be executed after each typescript compilation.
* `--check-coverage`, enables c8 check coverage; default is false
Expand Down
14 changes: 13 additions & 1 deletion borp.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,19 @@ try {

for (const input of args.values.reporter) {
const [name, dest] = input.split(':')
const Ctor = reporters[name] || await import(name).then((m) => m.default || m)
let Ctor
if (Object.prototype.hasOwnProperty.call(reporters, name) === true) {
Ctor = reporters[name]
} else {
try {
// Try to load a custom reporter from a file relative to the process.
const modPath = join(process.cwd(), name.replace(/^['"]/, '').replace(/['"]$/, ''))
Ctor = await import(modPath).then((m) => m.default || m)
} catch {
// Fallback to trying to load the reporter from node_modules resolution.
Ctor = await import(name).then((m) => m.default || m)
}
}
const reporter = Ctor.prototype && Object.getOwnPropertyDescriptor(Ctor.prototype, 'constructor') ? new Ctor() : Ctor
let output = process.stdout
if (dest) {
Expand Down
3 changes: 3 additions & 0 deletions fixtures/relative-reporter/lib/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add (x, y) {
return x + y
}
24 changes: 24 additions & 0 deletions fixtures/relative-reporter/reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

import { Transform } from 'node:stream'

const testReporter = new Transform({
writableObjectMode: true,
transform (event, encoding, callback) {
switch (event.type) {
case 'test:pass': {
return callback(null, `passed: ${event.data.file}\n`)
}

case 'test:fail': {
return callback(null, `failed: ${event.data.file}\n`)
}

default: {
callback(null, null)
}
}
}
})

export default testReporter
7 changes: 7 additions & 0 deletions fixtures/relative-reporter/test/add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/relative-reporter/test/add2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add2', () => {
strictEqual(add(3, 2), 5)
})
13 changes: 13 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ test('reporter from node_modules', async () => {
strictEqual(stdout.indexOf('tests 2') >= 0, true)
})

test('reporter from relative path', async () => {
const cwd = join(import.meta.url, '..', 'fixtures', 'relative-reporter')
const { stdout } = await execa('node', [
borp,
'--reporter=./fixtures/relative-reporter/reporter.js'
], {
cwd
})

strictEqual(/passed:.+add\.test\.js/.test(stdout), true)
strictEqual(/passed:.+add2\.test\.js/.test(stdout), true)
})

test('gh reporter', async () => {
const cwd = join(import.meta.url, '..', 'fixtures', 'js-esm')
const { stdout } = await execa('node', [
Expand Down

0 comments on commit 43bef5f

Please sign in to comment.