Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions docs/guide/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ export default defineConfig({
Output [workflow commands](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)
to provide annotations for test failures. This reporter is automatically enabled with a [`default`](#default-reporter) reporter when `process.env.GITHUB_ACTIONS === 'true'`.

<img alt="Github Actions" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/336cddc2-df6b-4b8a-8e72-4d00010e37f5">
<img alt="Github Actions" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/ce8447c1-0eab-4fe1-abef-d0d322290dca">

If you configure non-default reporters, you need to explicitly add `github-actions`.

```ts
Expand All @@ -507,8 +510,22 @@ export default defineConfig({
})
```

<img alt="Github Actions" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/336cddc2-df6b-4b8a-8e72-4d00010e37f5">
<img alt="Github Actions" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/ce8447c1-0eab-4fe1-abef-d0d322290dca">
You can customize the file paths that are printed in [GitHub's annotation command format](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions) by using the `onWritePath` option. This is useful when running Vitest in a containerized environment, such as Docker, where the file paths may not match the paths in the GitHub Actions environment.

```ts
export default defineConfig({
test: {
reporters: process.env.GITHUB_ACTIONS
? [
'default',
['github-actions', { onWritePath(path) {
return path.replace(/^\/app\//, `${process.env.GITHUB_WORKSPACE}/`)
} }],
]
: ['default'],
},
})
```

### Blob Reporter

Expand Down
17 changes: 16 additions & 1 deletion packages/vitest/src/node/reporters/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import { stripVTControlCharacters } from 'node:util'
import { getFullName, getTasks } from '@vitest/runner/utils'
import { capturePrintError } from '../printError'

export interface GithubActionsReporterOptions {
onWritePath?: (path: string) => string
}

export class GithubActionsReporter implements Reporter {
ctx: Vitest = undefined!
options: GithubActionsReporterOptions

constructor(options: GithubActionsReporterOptions = {}) {
this.options = options
}

onInit(ctx: Vitest): void {
this.ctx = ctx
Expand Down Expand Up @@ -48,6 +57,8 @@ export class GithubActionsReporter implements Reporter {
}
}

const onWritePath = this.options.onWritePath ?? defaultOnWritePath

// format errors via `printError`
for (const { project, title, error, file } of projectErrors) {
const result = capturePrintError(error, this.ctx, { project, task: file })
Expand All @@ -58,7 +69,7 @@ export class GithubActionsReporter implements Reporter {
const formatted = formatMessage({
command: 'error',
properties: {
file: stack.file,
file: onWritePath(stack.file),
title,
line: String(stack.line),
column: String(stack.column),
Expand All @@ -70,6 +81,10 @@ export class GithubActionsReporter implements Reporter {
}
}

function defaultOnWritePath(path: string): string {
return path
}

// workflow command formatting based on
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
// https://github.com/actions/toolkit/blob/f1d9b4b985e6f0f728b4b766db73498403fd5ca3/packages/core/src/command.ts#L80-L85
Expand Down
43 changes: 35 additions & 8 deletions test/reporters/tests/github-actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { sep } from 'node:path'
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { describe, expect, it } from 'vitest'
import { GithubActionsReporter } from '../../../packages/vitest/src/node/reporters'
import { runVitest } from '../../test-utils'

test(GithubActionsReporter, async () => {
let { stdout, stderr } = await runVitest(
{ reporters: new GithubActionsReporter(), root: './fixtures', include: ['**/some-failing.test.ts'] },
)
stdout = stdout.replace(resolve(__dirname, '..').replace(/:/g, '%3A'), '__TEST_DIR__')
expect(stdout).toMatchInlineSnapshot(`
describe(GithubActionsReporter, () => {
it('uses absolute path by default', async () => {
let { stdout, stderr } = await runVitest(
{ reporters: new GithubActionsReporter(), root: './fixtures', include: ['**/some-failing.test.ts'] },
)
stdout = stdout.replace(resolve(__dirname, '..').replace(/:/g, '%3A'), '__TEST_DIR__')
expect(stdout).toMatchInlineSnapshot(`
"
::error file=__TEST_DIR__/fixtures/some-failing.test.ts,title=some-failing.test.ts > 3 + 3 = 7,line=8,column=17::AssertionError: expected 6 to be 7 // Object.is equality%0A%0A- Expected%0A+ Received%0A%0A- 7%0A+ 6%0A%0A ❯ some-failing.test.ts:8:17%0A%0A
"
`)
expect(stderr).toBe('')
expect(stderr).toBe('')
})

it('uses onWritePath to format path', async () => {
const { stdout, stderr } = await runVitest(
{
reporters: new GithubActionsReporter({
onWritePath(path) {
const normalized = path
.replace(resolve(__dirname, '..'), '')
.replaceAll(sep, '/')

return `/some-custom-path${normalized}`
},
}),
root: './fixtures',
include: ['**/some-failing.test.ts'],
},
)
expect(stdout).toMatchInlineSnapshot(`
"
::error file=/some-custom-path/fixtures/some-failing.test.ts,title=some-failing.test.ts > 3 + 3 = 7,line=8,column=17::AssertionError: expected 6 to be 7 // Object.is equality%0A%0A- Expected%0A+ Received%0A%0A- 7%0A+ 6%0A%0A ❯ some-failing.test.ts:8:17%0A%0A
"
`)
expect(stderr).toBe('')
})
})
Loading