Skip to content

Commit abd3bb7

Browse files
feat(reporter): add onWritePath option to github-actions (#8015)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 3f8159a commit abd3bb7

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

docs/guide/reporters.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ export default defineConfig({
497497
Output [workflow commands](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)
498498
to provide annotations for test failures. This reporter is automatically enabled with a [`default`](#default-reporter) reporter when `process.env.GITHUB_ACTIONS === 'true'`.
499499

500+
<img alt="Github Actions" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/336cddc2-df6b-4b8a-8e72-4d00010e37f5">
501+
<img alt="Github Actions" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/ce8447c1-0eab-4fe1-abef-d0d322290dca">
502+
500503
If you configure non-default reporters, you need to explicitly add `github-actions`.
501504

502505
```ts
@@ -507,8 +510,22 @@ export default defineConfig({
507510
})
508511
```
509512

510-
<img alt="Github Actions" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/336cddc2-df6b-4b8a-8e72-4d00010e37f5">
511-
<img alt="Github Actions" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/ce8447c1-0eab-4fe1-abef-d0d322290dca">
513+
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.
514+
515+
```ts
516+
export default defineConfig({
517+
test: {
518+
reporters: process.env.GITHUB_ACTIONS
519+
? [
520+
'default',
521+
['github-actions', { onWritePath(path) {
522+
return path.replace(/^\/app\//, `${process.env.GITHUB_WORKSPACE}/`)
523+
} }],
524+
]
525+
: ['default'],
526+
},
527+
})
528+
```
512529

513530
### Blob Reporter
514531

packages/vitest/src/node/reporters/github-actions.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ import { stripVTControlCharacters } from 'node:util'
66
import { getFullName, getTasks } from '@vitest/runner/utils'
77
import { capturePrintError } from '../printError'
88

9+
export interface GithubActionsReporterOptions {
10+
onWritePath?: (path: string) => string
11+
}
12+
913
export class GithubActionsReporter implements Reporter {
1014
ctx: Vitest = undefined!
15+
options: GithubActionsReporterOptions
16+
17+
constructor(options: GithubActionsReporterOptions = {}) {
18+
this.options = options
19+
}
1120

1221
onInit(ctx: Vitest): void {
1322
this.ctx = ctx
@@ -48,6 +57,8 @@ export class GithubActionsReporter implements Reporter {
4857
}
4958
}
5059

60+
const onWritePath = this.options.onWritePath ?? defaultOnWritePath
61+
5162
// format errors via `printError`
5263
for (const { project, title, error, file } of projectErrors) {
5364
const result = capturePrintError(error, this.ctx, { project, task: file })
@@ -58,7 +69,7 @@ export class GithubActionsReporter implements Reporter {
5869
const formatted = formatMessage({
5970
command: 'error',
6071
properties: {
61-
file: stack.file,
72+
file: onWritePath(stack.file),
6273
title,
6374
line: String(stack.line),
6475
column: String(stack.column),
@@ -70,6 +81,10 @@ export class GithubActionsReporter implements Reporter {
7081
}
7182
}
7283

84+
function defaultOnWritePath(path: string): string {
85+
return path
86+
}
87+
7388
// workflow command formatting based on
7489
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
7590
// https://github.com/actions/toolkit/blob/f1d9b4b985e6f0f728b4b766db73498403fd5ca3/packages/core/src/command.ts#L80-L85
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1+
import { sep } from 'node:path'
12
import { resolve } from 'pathe'
2-
import { expect, test } from 'vitest'
3+
import { describe, expect, it } from 'vitest'
34
import { GithubActionsReporter } from '../../../packages/vitest/src/node/reporters'
45
import { runVitest } from '../../test-utils'
56

6-
test(GithubActionsReporter, async () => {
7-
let { stdout, stderr } = await runVitest(
8-
{ reporters: new GithubActionsReporter(), root: './fixtures', include: ['**/some-failing.test.ts'] },
9-
)
10-
stdout = stdout.replace(resolve(__dirname, '..').replace(/:/g, '%3A'), '__TEST_DIR__')
11-
expect(stdout).toMatchInlineSnapshot(`
7+
describe(GithubActionsReporter, () => {
8+
it('uses absolute path by default', async () => {
9+
let { stdout, stderr } = await runVitest(
10+
{ reporters: new GithubActionsReporter(), root: './fixtures', include: ['**/some-failing.test.ts'] },
11+
)
12+
stdout = stdout.replace(resolve(__dirname, '..').replace(/:/g, '%3A'), '__TEST_DIR__')
13+
expect(stdout).toMatchInlineSnapshot(`
1214
"
1315
::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
1416
"
1517
`)
16-
expect(stderr).toBe('')
18+
expect(stderr).toBe('')
19+
})
20+
21+
it('uses onWritePath to format path', async () => {
22+
const { stdout, stderr } = await runVitest(
23+
{
24+
reporters: new GithubActionsReporter({
25+
onWritePath(path) {
26+
const normalized = path
27+
.replace(resolve(__dirname, '..'), '')
28+
.replaceAll(sep, '/')
29+
30+
return `/some-custom-path${normalized}`
31+
},
32+
}),
33+
root: './fixtures',
34+
include: ['**/some-failing.test.ts'],
35+
},
36+
)
37+
expect(stdout).toMatchInlineSnapshot(`
38+
"
39+
::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
40+
"
41+
`)
42+
expect(stderr).toBe('')
43+
})
1744
})

0 commit comments

Comments
 (0)