Skip to content

Commit 9f8b135

Browse files
committed
refactor: create job summary options type rather than deriving from the main type
1 parent 0a9ef3e commit 9f8b135

1 file changed

Lines changed: 45 additions & 50 deletions

File tree

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

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,52 @@ export interface GithubActionsReporterOptions {
2323
*
2424
* When enabled, a markdown summary of test results is written to the path specified by `outputPath`.
2525
*/
26-
jobSummary?: {
26+
jobSummary?: Partial<JobSummaryOptions>
27+
}
28+
29+
interface JobSummaryOptions {
30+
/**
31+
* Whether to generate the summary.
32+
*
33+
* @default true
34+
*/
35+
enabled: boolean
36+
/**
37+
* File path to write the summary to.
38+
*
39+
* @default process.env.GITHUB_STEP_SUMMARY
40+
*/
41+
outputPath: string | undefined
42+
/**
43+
* Configuration for generating permalink URLs to source files in the GitHub repository.
44+
*
45+
* When all three values are available (either from this config or the defaults picked from environment variables), test names in the summary will link to the relevant source lines.
46+
*/
47+
fileLinks: {
2748
/**
28-
* Whether to generate the summary.
49+
* The GitHub repository in `owner/repo` format.
2950
*
30-
* @default true
51+
* @default process.env.GITHUB_REPOSITORY
3152
*/
32-
enabled?: boolean
53+
repository?: string | undefined
3354
/**
34-
* File path to write the summary to.
55+
* The commit SHA to use in permalink URLs.
3556
*
36-
* @default process.env.GITHUB_STEP_SUMMARY
57+
* @default process.env.GITHUB_SHA
3758
*/
38-
outputPath?: string | undefined
59+
commitHash?: string | undefined
3960
/**
40-
* Configuration for generating permalink URLs to source files in the GitHub repository.
61+
* The absolute path to the root of the repository on disk.
62+
*
63+
* This value is used to compute relative file paths for the permalink URLs.
4164
*
42-
* When all three values are available (either from this config or the defaults picked from environment variables), test names in the summary will link to the relevant source lines.
65+
* @default process.env.GITHUB_WORKSPACE
4366
*/
44-
fileLinks?: {
45-
/**
46-
* The GitHub repository in `owner/repo` format.
47-
*
48-
* @default process.env.GITHUB_REPOSITORY
49-
*/
50-
repository?: string | undefined
51-
/**
52-
* The commit SHA to use in permalink URLs.
53-
*
54-
* @default process.env.GITHUB_SHA
55-
*/
56-
commitHash?: string | undefined
57-
/**
58-
* The absolute path to the root of the repository on disk.
59-
*
60-
* This value is used to compute relative file paths for the permalink URLs.
61-
*
62-
* @default process.env.GITHUB_WORKSPACE
63-
*/
64-
workspacePath?: string | undefined
65-
}
67+
workspacePath?: string | undefined
6668
}
6769
}
6870

69-
type SummaryOptions = NonNullable<GithubActionsReporterOptions['jobSummary']>
70-
71-
interface ResolvedOptions extends Required<GithubActionsReporterOptions> {
72-
// only `enabled` is required as the other values can be `undefined` as they're env variables
73-
jobSummary: Required<Pick<SummaryOptions, 'enabled'>> & Omit<SummaryOptions, 'enabled'>
74-
}
71+
type ResolvedOptions = Required<GithubActionsReporterOptions>
7572

7673
const defaultOptions: ResolvedOptions = {
7774
onWritePath: defaultOnWritePath,
@@ -182,17 +179,15 @@ export class GithubActionsReporter implements Reporter {
182179
if (this.options.jobSummary.enabled === true && this.options.jobSummary.outputPath) {
183180
const summary = renderSummary(collectSummaryData(testModules), this.options.jobSummary.fileLinks)
184181

185-
if (summary !== null) {
186-
try {
187-
writeFileSync(
188-
this.options.jobSummary.outputPath,
189-
summary,
190-
{ flag: 'a' },
191-
)
192-
}
193-
catch (error) {
194-
this.ctx.logger.warn('Could not write summary to `options.summary.outputPath`', error)
195-
}
182+
try {
183+
writeFileSync(
184+
this.options.jobSummary.outputPath,
185+
summary,
186+
{ flag: 'a' },
187+
)
188+
}
189+
catch (error) {
190+
this.ctx.logger.warn('Could not write summary to `options.summary.outputPath`', error)
196191
}
197192
}
198193
}
@@ -351,7 +346,7 @@ function collectSummaryData(testModules: ReadonlyArray<TestModule>): SummaryData
351346
return summaryData
352347
}
353348

354-
function createGitHubFileLinkCreator(fileLinks: SummaryOptions['fileLinks']): (path: string, line?: number) => string | null {
349+
function createGitHubFileLinkCreator(fileLinks?: JobSummaryOptions['fileLinks']): (path: string, line?: number) => string | null {
355350
const repository = fileLinks?.repository
356351
const commitHash = fileLinks?.commitHash
357352
const workspacePath = fileLinks?.workspacePath
@@ -413,7 +408,7 @@ function renderStats(stats: SummaryData['stats']): string {
413408

414409
const SUMMARY_HEADER = '## Vitest Test Report\n'
415410

416-
function renderSummary(summaryData: SummaryData, fileLinks: SummaryOptions['fileLinks']): string | null {
411+
function renderSummary(summaryData: SummaryData, fileLinks?: JobSummaryOptions['fileLinks']): string {
417412
const fileLinkCreator = createGitHubFileLinkCreator(fileLinks)
418413

419414
let summary = `${SUMMARY_HEADER}${renderStats(summaryData.stats)}`

0 commit comments

Comments
 (0)