From 1cb5e2cbfe137acc71b4055d3de61a3164c71ac2 Mon Sep 17 00:00:00 2001 From: Francesco Saltori Date: Fri, 5 Aug 2022 12:56:35 +0200 Subject: [PATCH] Refactor tests a bit and fix typings Signed-off-by: Francesco Saltori --- .../builtin/publish/githubPullRequest.test.ts | 73 +++++++------------ 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts index bf857bc34f..dd493495d8 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts @@ -26,7 +26,6 @@ import { resolve as resolvePath } from 'path'; import { Writable } from 'stream'; import { ActionContext, TemplateAction } from '../../types'; import { - CreateGithubPullRequestClientFactoryInput, createPublishGithubPullRequestAction, OctokitWithPullRequestPluginClient, } from './githubPullRequest'; @@ -42,11 +41,12 @@ type GithubPullRequestActionInput = ReturnType< describe('createPublishGithubPullRequestAction', () => { let instance: TemplateAction; - let fakeClient: OctokitWithPullRequestPluginClient; - - let clientFactory: ( - input: CreateGithubPullRequestClientFactoryInput, - ) => Promise; + let fakeClient: { + createPullRequest: jest.Mock; + rest: { + pulls: { requestReviewers: jest.Mock }; + }; + }; beforeEach(() => { const integrations = ScmIntegrations.fromConfig(new ConfigReader({})); @@ -64,11 +64,13 @@ describe('createPublishGithubPullRequestAction', () => { }), rest: { pulls: { - requestReviewers: jest.fn(async (_: any) => ({ data: {} })) - } - } + requestReviewers: jest.fn(async (_: any) => ({ data: {} })), + }, + }, }; - clientFactory = jest.fn(async () => fakeClient); + const clientFactory = jest.fn( + async () => fakeClient as unknown as OctokitWithPullRequestPluginClient, + ); const githubCredentialsProvider: GithubCredentialsProvider = { getCredentials: jest.fn(), }; @@ -80,6 +82,11 @@ describe('createPublishGithubPullRequestAction', () => { }); }); + afterEach(() => { + mockFs.restore(); + jest.resetAllMocks(); + }); + describe('with no sourcePath', () => { let input: GithubPullRequestActionInput; let ctx: ActionContext; @@ -106,6 +113,7 @@ describe('createPublishGithubPullRequestAction', () => { workspacePath, }; }); + it('creates a pull request', async () => { await instance.handler(ctx); @@ -140,10 +148,6 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); }); describe('with sourcePath', () => { @@ -176,11 +180,6 @@ describe('createPublishGithubPullRequestAction', () => { }; }); - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); - it('creates a pull request with only relevant files', async () => { await instance.handler(ctx); @@ -272,10 +271,6 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); }); describe('with reviewers and teamReviewers', () => { @@ -289,7 +284,7 @@ describe('createPublishGithubPullRequestAction', () => { branchName: 'new-app', description: 'This PR is really good', reviewers: ['foobar'], - teamReviewers: ['team-foo'] + teamReviewers: ['team-foo'], }; ctx = { @@ -312,12 +307,14 @@ describe('createPublishGithubPullRequestAction', () => { pull_number: 123, reviewers: ['foobar'], team_reviewers: ['team-foo'], - }) + }); }); it('creates outputs for the pull request url and number even if requesting reviewers fails', async () => { - fakeClient.rest.pulls.requestReviewers.mockImplementation(() => { throw new Error('a random error') }) - + fakeClient.rest.pulls.requestReviewers.mockImplementation(() => { + throw new Error('a random error'); + }); + await instance.handler(ctx); expect(ctx.output).toHaveBeenCalledWith( @@ -326,12 +323,7 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); - }) + }); describe('with no reviewers and teamReviewers', () => { let input: GithubPullRequestActionInput; @@ -361,12 +353,7 @@ describe('createPublishGithubPullRequestAction', () => { expect(fakeClient.createPullRequest).toBeCalled(); expect(fakeClient.rest.pulls.requestReviewers).not.toBeCalled(); }); - - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); - }) + }); describe('with executable file mode 755', () => { let input: GithubPullRequestActionInput; @@ -431,10 +418,6 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); }); describe('with executable file mode 775', () => { @@ -500,9 +483,5 @@ describe('createPublishGithubPullRequestAction', () => { ); expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); - afterEach(() => { - mockFs.restore(); - jest.resetAllMocks(); - }); }); });