From 5eaf7cb1b2743ed156e71aa851356de548d35049 Mon Sep 17 00:00:00 2001 From: Matteo Dell'Acqua <82184604+MatteoH2O1999@users.noreply.github.com> Date: Thu, 24 Nov 2022 11:50:41 +0100 Subject: [PATCH] Add unit tests for new default reporter --- .../src/__tests__/TestScheduler.test.js | 206 +++++++++++++++++- 1 file changed, 203 insertions(+), 3 deletions(-) diff --git a/packages/jest-core/src/__tests__/TestScheduler.test.js b/packages/jest-core/src/__tests__/TestScheduler.test.js index d50517f34104..8a5adc441361 100644 --- a/packages/jest-core/src/__tests__/TestScheduler.test.js +++ b/packages/jest-core/src/__tests__/TestScheduler.test.js @@ -10,6 +10,7 @@ import { CoverageReporter, DefaultReporter, GitHubActionsReporter, + GithubActionsLogsReporter, NotifyReporter, SummaryReporter, VerboseReporter, @@ -59,13 +60,203 @@ beforeEach(() => { spyShouldRunInBand.mockClear(); }); -describe('reporters', () => { +describe('reporters with GITHUB_ACTIONS = true', () => { const CustomReporter = require('/custom-reporter.js'); afterEach(() => { jest.clearAllMocks(); }); + test('works with default value', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: undefined, + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('does not enable any reporters, if empty list is passed', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(0); + }); + + test('sets up default reporters', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [['default', {}]], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('sets up verbose reporter', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [['default', {}]], + verbose: true, + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); + expect(VerboseReporter).toHaveBeenCalledTimes(1); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('sets up github actions reporter', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [ + ['default', {}], + ['github-actions', {}], + ], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(1); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('sets up notify reporter', async () => { + await createTestScheduler( + makeGlobalConfig({ + notify: true, + reporters: [['default', {}]], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(1); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('sets up coverage reporter', async () => { + await createTestScheduler( + makeGlobalConfig({ + collectCoverage: true, + reporters: [['default', {}]], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(1); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('allows enabling summary reporter separately', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [['summary', {}]], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + }); + + test('sets up custom reporter', async () => { + await createTestScheduler( + makeGlobalConfig({ + reporters: [ + ['default', {}], + ['/custom-reporter.js', {}], + ], + }), + {}, + {}, + ); + + expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(1); + expect(VerboseReporter).toHaveBeenCalledTimes(0); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); + expect(NotifyReporter).toHaveBeenCalledTimes(0); + expect(CoverageReporter).toHaveBeenCalledTimes(0); + expect(SummaryReporter).toHaveBeenCalledTimes(1); + expect(CustomReporter).toHaveBeenCalledTimes(1); + }); +}); + +describe('reporters with GITHUB_ACTIONS = false', () => { + const CustomReporter = require('/custom-reporter.js'); + + afterEach(() => { + jest.clearAllMocks(); + }); + + beforeAll(() => { + const ci = require('ci-info'); + ci.GITHUB_ACTIONS = false; + }); + + afterAll(() => { + const ci = require('ci-info'); + ci.GITHUB_ACTIONS = true; + }); + test('works with default value', async () => { await createTestScheduler( makeGlobalConfig({ @@ -76,6 +267,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -93,6 +285,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -110,6 +303,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -128,6 +322,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(1); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -135,7 +330,7 @@ describe('reporters', () => { expect(SummaryReporter).toHaveBeenCalledTimes(1); }); - test('sets up github actions reporter', async () => { + test('does not set up github actions reporter', async () => { await createTestScheduler( makeGlobalConfig({ reporters: [ @@ -148,8 +343,9 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); - expect(GitHubActionsReporter).toHaveBeenCalledTimes(1); + expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); expect(CoverageReporter).toHaveBeenCalledTimes(0); expect(SummaryReporter).toHaveBeenCalledTimes(1); @@ -166,6 +362,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(1); @@ -184,6 +381,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -201,6 +399,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(0); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0); @@ -221,6 +420,7 @@ describe('reporters', () => { ); expect(DefaultReporter).toHaveBeenCalledTimes(1); + expect(GithubActionsLogsReporter).toHaveBeenCalledTimes(0); expect(VerboseReporter).toHaveBeenCalledTimes(0); expect(GitHubActionsReporter).toHaveBeenCalledTimes(0); expect(NotifyReporter).toHaveBeenCalledTimes(0);