From b0527e16afea355e790899764bac504cb812a12e Mon Sep 17 00:00:00 2001 From: Matteo Dell'Acqua <82184604+MatteoH2O1999@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:31:48 +0100 Subject: [PATCH] Update type CamelCase and fix silent option --- packages/jest-core/src/TestScheduler.ts | 5 ++- .../src/GitHubActionsReporter.ts | 42 ++++++++----------- .../__tests__/GitHubActionsReporter.test.ts | 11 ++--- .../GithubActionsLogsReporter.test.js | 2 + 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/jest-core/src/TestScheduler.ts b/packages/jest-core/src/TestScheduler.ts index ad96154c2fbb..38dc47adedd4 100644 --- a/packages/jest-core/src/TestScheduler.ts +++ b/packages/jest-core/src/TestScheduler.ts @@ -347,7 +347,10 @@ class TestScheduler { : this.addReporter(new DefaultReporter(this._globalConfig)); break; case 'github-actions': - GITHUB_ACTIONS && this.addReporter(new GitHubActionsReporter()); + GITHUB_ACTIONS && + this.addReporter( + new GitHubActionsReporter(this._globalConfig, options), + ); break; case 'summary': summary = true; diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index 8d660bf665ec..0a8e4768f62c 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -33,31 +33,31 @@ type AnnotationOptions = { const titleSeparator = ' \u203A '; -type performaceInfo = { +type PerformanceInfo = { end: number; runtime: number; slow: boolean; start: number; }; -type resultTreeLeaf = { +type ResultTreeLeaf = { name: string; passed: boolean; duration: number; children: Array; }; -type resultTreeNode = { +type ResultTreeNode = { name: string; passed: boolean; - children: Array; + children: Array; }; -type resultTree = { - children: Array; +type ResultTree = { + children: Array; name: string; passed: boolean; - performanceInfo: performaceInfo; + performanceInfo: PerformanceInfo; }; export default class GitHubActionsReporter extends BaseReporter { @@ -66,17 +66,10 @@ export default class GitHubActionsReporter extends BaseReporter { constructor( _globalConfig: Config.GlobalConfig, - reporterOptions: Config.ReporterConfig, + reporterOptions: Record = {silent: false}, ) { super(); - if (reporterOptions === null || reporterOptions === undefined) { - reporterOptions = ['github-actions', {silent: false}]; - } - let options = reporterOptions[1]; - if (options === null || options === undefined) { - options = {silent: false}; - } - const silentOption = options.silent; + const silentOption = reporterOptions.silent; if (silentOption !== null && silentOption !== undefined) { this.silent = silentOption as boolean; } else { @@ -89,6 +82,7 @@ export default class GitHubActionsReporter extends BaseReporter { testResult: TestResult, aggregatedResults: AggregatedResult, ): void { + this.generateAnnotations(test, testResult); if (this.silent) { return; } @@ -101,7 +95,7 @@ export default class GitHubActionsReporter extends BaseReporter { } } - onTestFileResult({context}: Test, {testResults}: TestResult): void { + generateAnnotations({context}: Test, {testResults}: TestResult): void { testResults.forEach(result => { const title = [...result.ancestorTitles, result.title].join( titleSeparator, @@ -211,9 +205,9 @@ export default class GitHubActionsReporter extends BaseReporter { private getResultTree( suiteResult: Array, testPath: string, - suitePerf: performaceInfo, - ): resultTree { - const root: resultTree = { + suitePerf: PerformanceInfo, + ): ResultTree { + const root: ResultTree = { children: [], name: testPath, passed: true, @@ -264,8 +258,8 @@ export default class GitHubActionsReporter extends BaseReporter { private getResultChildren( suiteResult: Array, ancestors: Array, - ): resultTreeNode { - const node: resultTreeNode = { + ): ResultTreeNode { + const node: ResultTreeNode = { children: [], name: ancestors[ancestors.length - 1], passed: true, @@ -321,7 +315,7 @@ export default class GitHubActionsReporter extends BaseReporter { return node; } - private printResultTree(resultTree: resultTree): void { + private printResultTree(resultTree: ResultTree): void { let perfMs; if (resultTree.performanceInfo.slow) { perfMs = ` (${chalk.red.inverse( @@ -349,7 +343,7 @@ export default class GitHubActionsReporter extends BaseReporter { } private recursivePrintResultTree( - resultTree: resultTreeNode | resultTreeLeaf, + resultTree: ResultTreeNode | ResultTreeLeaf, alreadyGrouped: boolean, depth: number, ): void { diff --git a/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts index 19ced0720956..9d4bb73e68ba 100644 --- a/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts +++ b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts @@ -6,6 +6,7 @@ */ import type {Test, TestCaseResult, TestResult} from '@jest/test-result'; +import type {Config} from '@jest/types'; import GitHubActionsReporter from '../GitHubActionsReporter'; const mockedStderrWrite = jest @@ -16,7 +17,7 @@ afterEach(() => { jest.clearAllMocks(); }); -const reporter = new GitHubActionsReporter(); +const reporter = new GitHubActionsReporter({} as Config.GlobalConfig); const testMeta = { context: {config: {rootDir: '/user/project'}}, @@ -74,7 +75,7 @@ const testCaseResult = { describe('logs error annotation', () => { test('when an expectation fails to pass', () => { - reporter.onTestFileResult(testMeta, { + reporter.generateAnnotations(testMeta, { testResults: [ { ...testCaseResult, @@ -88,7 +89,7 @@ describe('logs error annotation', () => { }); test('when a test has reference error', () => { - reporter.onTestFileResult( + reporter.generateAnnotations( {...testMeta, path: '/user/project/__tests__/example.test.js:25:12'}, { testResults: [ @@ -105,7 +106,7 @@ describe('logs error annotation', () => { }); test('when test is wrapped in describe block', () => { - reporter.onTestFileResult(testMeta, { + reporter.generateAnnotations(testMeta, { testResults: [ { ...testCaseResult, @@ -121,7 +122,7 @@ describe('logs error annotation', () => { describe('logs warning annotation before logging errors', () => { test('when test result includes retry reasons', () => { - reporter.onTestFileResult(testMeta, { + reporter.generateAnnotations(testMeta, { testResults: [ { ...testCaseResult, diff --git a/packages/jest-reporters/src/__tests__/GithubActionsLogsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsLogsReporter.test.js index e2427f217e56..f3a5428f1b60 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsLogsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsLogsReporter.test.js @@ -362,6 +362,7 @@ describe('Reporter interface', () => { ` ${chalk.green(ySymbol)} test1 (10 ms)` + '::endgroup::'; const gha = new GhaReporter(); + gha.generateAnnotations = jest.fn(); gha.onTestResult(mockTest, mockTestResult, mockResults); @@ -406,6 +407,7 @@ describe('Reporter interface', () => { 'Failure message' + '::endgroup::'; const gha = new GhaReporter(); + gha.generateAnnotations = jest.fn(); gha.onTestResult(mockTest, mockTestResult, mockResults);