From 6b08f6e13d1ed36b08427898686076ad26f61a06 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 21 Feb 2024 17:00:25 +0000 Subject: [PATCH 1/5] [TS migration] Migrate checkDeployBlockersTest to Typescript --- ...kersTest.js => checkDeployBlockersTest.ts} | 82 ++++++++++--------- 1 file changed, 45 insertions(+), 37 deletions(-) rename tests/unit/{checkDeployBlockersTest.js => checkDeployBlockersTest.ts} (63%) diff --git a/tests/unit/checkDeployBlockersTest.js b/tests/unit/checkDeployBlockersTest.ts similarity index 63% rename from tests/unit/checkDeployBlockersTest.js rename to tests/unit/checkDeployBlockersTest.ts index 354ab132f601..976375f5986c 100644 --- a/tests/unit/checkDeployBlockersTest.js +++ b/tests/unit/checkDeployBlockersTest.ts @@ -2,12 +2,19 @@ * @jest-environment node */ import * as core from '@actions/core'; -import _ from 'underscore'; +import type {Writable} from 'type-fest'; import run from '../../.github/actions/javascript/checkDeployBlockers/checkDeployBlockers'; import GithubUtils from '../../.github/libs/GithubUtils'; +type Comment = {body: string}; +type Comments = { + data?: Comment[]; +}; + +type PullRequest = {url: string; isQASuccess: boolean}; + // Static mock function for core.getInput -const mockGetInput = jest.fn().mockImplementation((arg) => { +const mockGetInput = jest.fn().mockImplementation((arg: string): string | number | undefined => { if (arg === 'GITHUB_TOKEN') { return 'fake_token'; } @@ -21,10 +28,12 @@ const mockSetOutput = jest.fn(); const mockGetIssue = jest.fn(); const mockListComments = jest.fn(); +const asMutable = (value: T): Writable => value as Writable; + beforeAll(() => { // Mock core module - core.getInput = mockGetInput; - core.setOutput = mockSetOutput; + asMutable(core).getInput = mockGetInput; + asMutable(core).setOutput = mockSetOutput; // Mock octokit module const moctokit = { @@ -35,10 +44,12 @@ beforeAll(() => { }, }, }; + + // @ts-expect-error TODO: Remove this once GithubUtils (https://github.com/Expensify/App/issues/25382) is migrated to TypeScript. GithubUtils.internalOctokit = moctokit; }); -let baseComments = []; +let baseComments: Comments = {}; beforeEach(() => { baseComments = { data: [ @@ -65,11 +76,11 @@ afterAll(() => { jest.clearAllMocks(); }); -function checkbox(isClosed) { +function checkbox(isClosed: boolean): string { return isClosed ? '[x]' : '[ ]'; } -function mockIssue(prList, deployBlockerList) { +function mockIssue(prList: PullRequest[], deployBlockerList?: PullRequest[]) { return { data: { number: 1, @@ -79,25 +90,27 @@ function mockIssue(prList, deployBlockerList) { **Compare Changes:** https://github.com/Expensify/App/compare/production...staging **This release contains changes from the following pull requests:** -${_.map( - prList, - ({url, isQASuccess}) => ` +${prList + .map( + ({url, isQASuccess}) => ` - ${checkbox(isQASuccess)} ${url} `, -)} + ) + .join('\n')} ${ - !_.isEmpty(deployBlockerList) + !deployBlockerList || deployBlockerList.length < 0 ? ` **Deploy Blockers:**` : '' } -${_.map( - deployBlockerList, - ({url, isQASuccess}) => ` +${deployBlockerList + ?.map( + ({url, isQASuccess}) => ` - ${checkbox(isQASuccess)} ${url} `, -)} + ) + .join('\n')} cc @Expensify/applauseleads `, }, @@ -108,51 +121,46 @@ describe('checkDeployBlockers', () => { const allClearIssue = mockIssue([{url: 'https://github.com/Expensify/App/pull/6882', isQASuccess: true}]); describe('checkDeployBlockers', () => { - test('Test an issue with all checked items and :shipit:', () => { + test('Test an issue with all checked items and :shipit:', async () => { mockGetIssue.mockResolvedValue(allClearIssue); mockListComments.mockResolvedValue(baseComments); - return run().then(() => { - expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', false); - }); + await expect(run()).resolves.toBeUndefined(); + expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', false); }); - test('Test an issue with all boxes checked but no :shipit:', () => { + test('Test an issue with all boxes checked but no :shipit:', async () => { mockGetIssue.mockResolvedValue(allClearIssue); const extraComments = { - data: [...baseComments.data, {body: 'This issue either has unchecked QA steps or has not yet been stamped with a :shipit: comment. Reopening!'}], + data: [...(baseComments?.data ?? []), {body: 'This issue either has unchecked QA steps or has not yet been stamped with a :shipit: comment. Reopening!'}], }; mockListComments.mockResolvedValue(extraComments); - return run().then(() => { - expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); - }); + await expect(run()).resolves.toBeUndefined(); + expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); }); - test('Test an issue with all boxes checked but no comments', () => { + test('Test an issue with all boxes checked but no comments', async () => { mockGetIssue.mockResolvedValue(allClearIssue); mockListComments.mockResolvedValue({data: []}); - return run().then(() => { - expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); - }); + await expect(run()).resolves.toBeUndefined(); + expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); }); - test('Test an issue with all QA checked but not all deploy blockers', () => { + test('Test an issue with all QA checked but not all deploy blockers', async () => { mockGetIssue.mockResolvedValue( mockIssue([{url: 'https://github.com/Expensify/App/pull/6882', isQASuccess: true}], [{url: 'https://github.com/Expensify/App/pull/6883', isQASuccess: false}]), ); mockListComments.mockResolvedValue(baseComments); - return run().then(() => { - expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); - }); + await expect(run()).resolves.toBeUndefined(); + expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', true); }); - test('Test an issue with all QA checked and all deploy blockers resolved', () => { + test('Test an issue with all QA checked and all deploy blockers resolved', async () => { mockGetIssue.mockResolvedValue( mockIssue([{url: 'https://github.com/Expensify/App/pull/6882', isQASuccess: true}], [{url: 'https://github.com/Expensify/App/pull/6883', isQASuccess: true}]), ); mockListComments.mockResolvedValue(baseComments); - return run().then(() => { - expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', false); - }); + await expect(run()).resolves.toBeUndefined(); + expect(mockSetOutput).toHaveBeenCalledWith('HAS_DEPLOY_BLOCKERS', false); }); }); }); From 6c411a29bedd801984eda6c84eab1f522301a029 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Thu, 22 Feb 2024 09:27:08 +0000 Subject: [PATCH 2/5] [TS migration][checkDeployBlockersTest] Feedback --- src/types/utils/AsMutable.ts | 5 +++++ tests/unit/checkDeployBlockersTest.ts | 12 +++++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 src/types/utils/AsMutable.ts diff --git a/src/types/utils/AsMutable.ts b/src/types/utils/AsMutable.ts new file mode 100644 index 000000000000..57c49058cd14 --- /dev/null +++ b/src/types/utils/AsMutable.ts @@ -0,0 +1,5 @@ +import type {Writable} from 'type-fest'; + +const asMutable = (value: T): Writable => value as Writable; + +export default asMutable; diff --git a/tests/unit/checkDeployBlockersTest.ts b/tests/unit/checkDeployBlockersTest.ts index 976375f5986c..d77fa16b1dd5 100644 --- a/tests/unit/checkDeployBlockersTest.ts +++ b/tests/unit/checkDeployBlockersTest.ts @@ -2,13 +2,13 @@ * @jest-environment node */ import * as core from '@actions/core'; -import type {Writable} from 'type-fest'; +import asMutable from '@src/types/utils/AsMutable'; import run from '../../.github/actions/javascript/checkDeployBlockers/checkDeployBlockers'; import GithubUtils from '../../.github/libs/GithubUtils'; -type Comment = {body: string}; -type Comments = { - data?: Comment[]; +type CommentData = {body: string}; +type Comment = { + data?: CommentData[]; }; type PullRequest = {url: string; isQASuccess: boolean}; @@ -28,8 +28,6 @@ const mockSetOutput = jest.fn(); const mockGetIssue = jest.fn(); const mockListComments = jest.fn(); -const asMutable = (value: T): Writable => value as Writable; - beforeAll(() => { // Mock core module asMutable(core).getInput = mockGetInput; @@ -49,7 +47,7 @@ beforeAll(() => { GithubUtils.internalOctokit = moctokit; }); -let baseComments: Comments = {}; +let baseComments: Comment = {}; beforeEach(() => { baseComments = { data: [ From 21c390456f2b744922266062f6f2590533981df8 Mon Sep 17 00:00:00 2001 From: Ruben Rebelo <39693995+ruben-rebelo@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:37:40 +0000 Subject: [PATCH 3/5] Rename AsMutable.ts to asMutable.ts --- src/types/utils/{AsMutable.ts => asMutable.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/types/utils/{AsMutable.ts => asMutable.ts} (100%) diff --git a/src/types/utils/AsMutable.ts b/src/types/utils/asMutable.ts similarity index 100% rename from src/types/utils/AsMutable.ts rename to src/types/utils/asMutable.ts From 78ca6c66e354a244ae7132b28e8a26106c741381 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 28 Feb 2024 08:39:27 +0000 Subject: [PATCH 4/5] [TS migration][checkdeployBlockersTest] feedback --- tests/unit/checkDeployBlockersTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/checkDeployBlockersTest.ts b/tests/unit/checkDeployBlockersTest.ts index d77fa16b1dd5..5197c88616d4 100644 --- a/tests/unit/checkDeployBlockersTest.ts +++ b/tests/unit/checkDeployBlockersTest.ts @@ -2,7 +2,7 @@ * @jest-environment node */ import * as core from '@actions/core'; -import asMutable from '@src/types/utils/AsMutable'; +import asMutable from '@src/types/utils/asMutable'; import run from '../../.github/actions/javascript/checkDeployBlockers/checkDeployBlockers'; import GithubUtils from '../../.github/libs/GithubUtils'; From eae030d7ca81ba9d59eea3b692794024d0cb7469 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Tue, 12 Mar 2024 09:55:05 +0000 Subject: [PATCH 5/5] [TS migration][checkDeployBlockersTest] Styling --- tests/unit/checkDeployBlockersTest.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/unit/checkDeployBlockersTest.ts b/tests/unit/checkDeployBlockersTest.ts index 5197c88616d4..5a35fdf4f681 100644 --- a/tests/unit/checkDeployBlockersTest.ts +++ b/tests/unit/checkDeployBlockersTest.ts @@ -7,9 +7,8 @@ import run from '../../.github/actions/javascript/checkDeployBlockers/checkDeplo import GithubUtils from '../../.github/libs/GithubUtils'; type CommentData = {body: string}; -type Comment = { - data?: CommentData[]; -}; + +type Comment = {data?: CommentData[]}; type PullRequest = {url: string; isQASuccess: boolean}; @@ -101,7 +100,7 @@ ${ **Deploy Blockers:**` : '' -} +} ${deployBlockerList ?.map( ({url, isQASuccess}) => `