Skip to content

Commit

Permalink
Add tests for reports
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 cae5f9f commit 9eb610d
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 8 deletions.
9 changes: 8 additions & 1 deletion borp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { finished } from 'node:stream/promises'
import { join, relative } from 'node:path'
import posix from 'node:path/posix'
import runWithTypeScript from './lib/run.js'
import { MarkdownReporter, GithubWorkflowFailuresReporter } from './lib/reporter.js'
import { MarkdownReporter, GithubWorkflowFailuresReporter } from './lib/reporters.js'
import { Report } from 'c8'
import os from 'node:os'
import { execa } from 'execa'

/* c8 ignore next 4 */
process.on('unhandledRejection', (err) => {
console.error(err)
process.exit(1)
Expand Down Expand Up @@ -96,6 +97,12 @@ try {
spec: new Reporters.spec()
}

// If we're running in a GitHub action, adds the gh reporter
// by default so that we can report failures to GitHub
if (process.env.GITHUB_ACTION) {
args.values.reporter.push('gh')
}

for (const input of args.values.reporter) {
const [name, dest] = input.split(':')
const reporter = reporters[name]
Expand Down
6 changes: 3 additions & 3 deletions lib/reporter.js → lib/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function normalizeFile (file, cwd) {
}

function eventToLine (event) {
return `* __${event.data.name}__, duration ${event.data.details.duration_ms}ms, line ${event.data.line} \n`
return `* __${event.data.name}__, duration ${event.data.details.duration_ms}ms, line ${event.data.line}\n`
}

export class MarkdownReporter extends Transform {
Expand All @@ -25,7 +25,7 @@ export class MarkdownReporter extends Transform {
})

this._files = {}
this._cwd = opts?.cwd || process.cwd()
this._cwd = opts?.cwd
}

getFile (path) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export class GithubWorkflowFailuresReporter extends Transform {
})

this._files = {}
this._cwd = opts?.cwd || process.cwd()
this._cwd = opts?.cwd
}

_transform (event, encoding, callback) {
Expand Down
1 change: 1 addition & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default async function runWithTypeScript (config) {
}
config.prefix = prefix
config.setup = (test) => {
/* c8 ignore next 12 */
if (test.reporter) {
for (const chunk of pushable) {
test.reporter.push(chunk)
Expand Down
4 changes: 0 additions & 4 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,3 @@ test('js-esm', async (t) => {

await completed
})

test('this fails', async (t) => {
throw new Error('this fails')
})
2 changes: 2 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { rejects } from 'node:assert'

const borp = join(import.meta.url, '..', 'borp.js')

delete process.env.GITHUB_ACTION

test('limit concurrency', async () => {
await execa('node', [
borp,
Expand Down
1 change: 1 addition & 0 deletions test/coverage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { match, doesNotMatch } from 'node:assert'
import { execa } from 'execa'
import { join } from 'desm'

delete process.env.GITHUB_ACTION
const borp = join(import.meta.url, '..', 'borp.js')

test('coverage', async () => {
Expand Down
169 changes: 169 additions & 0 deletions test/reporters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { MarkdownReporter, GithubWorkflowFailuresReporter } from '../lib/reporters.js'
import { test, describe } from 'node:test'
import { strictEqual } from 'node:assert'

describe('MarkdownReporter', async () => {
test('should write a report', async () => {
const reporter = new MarkdownReporter({ cwd: '/foo' })

// This is skipped
reporter.write({
type: 'test:start',
data: {}
})

reporter.write({
type: 'test:pass',
data: {
name: 'add',
file: 'file:///foo/test/add.test.ts',
line: 1,
details: {
duration_ms: 100
}
}
})

reporter.write({
type: 'test:pass',
data: {
name: 'add2',
file: 'file:///foo/test/add.test.ts',
line: 2,
details: {
duration_ms: 100
}
}
})

reporter.write({
type: 'test:fail',
data: {
name: 'add3',
file: 'file:///foo/test/add.test.ts',
line: 10,
details: {
duration_ms: 100
}
}
})
reporter.end()

let output = ''
for await (const chunk of reporter) {
output += chunk
}

strictEqual(output, `# Summary
## test/add.test.ts
### :white_check_mark: Pass
* __add__, duration 100ms, line 1
* __add2__, duration 100ms, line 2
### :x: Fail
* __add3__, duration 100ms, line 10
`)
})

test('skip fail heading if no failing tests', async () => {
const reporter = new MarkdownReporter({ cwd: '/foo' })

reporter.write({
type: 'test:pass',
data: {
name: 'add',
file: 'file:///foo/test/add.test.ts',
line: 1,
details: {
duration_ms: 100
}
}
})

reporter.write({
type: 'test:pass',
data: {
name: 'add2',
file: 'file:///foo/test/add.test.ts',
line: 2,
details: {
duration_ms: 100
}
}
})

reporter.end()

let output = ''
for await (const chunk of reporter) {
output += chunk
}

strictEqual(output, `# Summary
## test/add.test.ts
### :white_check_mark: Pass
* __add__, duration 100ms, line 1
* __add2__, duration 100ms, line 2
`)
})
})

describe('GithubWorkflowFailuresReporter', async () => {
test('should write error in github format', async () => {
const reporter = new GithubWorkflowFailuresReporter({ cwd: '/foo' })

// This is skipped
reporter.write({
type: 'test:start',
data: {}
})

reporter.write({
type: 'test:pass',
data: {
name: 'add',
file: 'file:///foo/test/add.test.ts',
line: 1,
details: {
duration_ms: 100
}
}
})

reporter.write({
type: 'test:fail',
data: {
name: 'add2',
file: 'file:///foo/test/add.test.ts',
line: 2,
details: {
duration_ms: 100
}
}
})

reporter.write({
type: 'test:fail',
data: {
name: 'add3',
file: 'file:///foo/test/add.test.ts',
line: 10,
details: {
duration_ms: 100
}
}
})
reporter.end()

let output = ''
for await (const chunk of reporter) {
output += chunk
}

const expected = [
'::error file=test/add.test.ts,line=2::add2\n',
'::error file=test/add.test.ts,line=10::add3\n'
].join('')

strictEqual(output, expected)
})
})

0 comments on commit 9eb610d

Please sign in to comment.