Skip to content

Commit

Permalink
Add unit testing coverage for new action
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Jan 29, 2022
1 parent 9cf6b24 commit a9af7a0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
43 changes: 29 additions & 14 deletions .github/actions/awaitStagingDeploys/awaitStagingDeploys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ const _ = require('underscore');
const GitHubUtils = require('../../libs/GithubUtils');
const {promiseDoWhile} = require('../../libs/promiseWhile');

let currentStagingDeploys = [];
promiseDoWhile(
() => !_.isEmpty(currentStagingDeploys),
_.throttle(
() => GitHubUtils.octokit.actions.listWorkflowRuns({
owner: GitHubUtils.GITHUB_OWNER,
repo: GitHubUtils.APP_REPO,
workflow_id: 'platformDeploy.yml',
event: 'push',
})
.then(response => currentStagingDeploys = _.filter(response.data.workflow_runs, workflowRun => workflowRun.status !== 'completed')),
GitHubUtils.POLL_RATE,
),
);
function run() {
let currentStagingDeploys = [];
return promiseDoWhile(
() => !_.isEmpty(currentStagingDeploys),
_.throttle(
() => GitHubUtils.octokit.actions.listWorkflowRuns({
owner: GitHubUtils.GITHUB_OWNER,
repo: GitHubUtils.APP_REPO,
workflow_id: 'platformDeploy.yml',
event: 'push',
})
.then(response => currentStagingDeploys = _.filter(response.data.workflow_runs, workflowRun => workflowRun.status !== 'completed'))
.then(() => console.log(
_.isEmpty(currentStagingDeploys)
? 'No current staging deploys found'
: `Found ${currentStagingDeploys.length} staging deploy${currentStagingDeploys.length > 1 ? 's' : ''} still running...`,
)),

// Poll every 90 seconds instead of every 10 seconds
GitHubUtils.POLL_RATE * 9,
),
);
}

if (require.main === module) {
run();
}

module.exports = run;
64 changes: 64 additions & 0 deletions tests/unit/awaitStagingDeploysTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @jest-environment node
*/
const run = require('../../.github/actions/awaitStagingDeploys/awaitStagingDeploys');
const GitHubUtils = require('../../.github/libs/GithubUtils');

// Lower poll rate to speed up tests
const TEST_POLL_RATE = 1;
const COMPLETED_WORKFLOW = {status: 'completed'};
const INCOMPLETE_WORKFLOW = {status: 'in_progress'};

const mockListWorkflowRuns = jest.fn();

beforeAll(() => {
// Mock octokit module
const mocktokit = {
actions: {
listWorkflowRuns: mockListWorkflowRuns,
},
};
GitHubUtils.octokitInternal = mocktokit;
GitHubUtils.POLL_RATE = TEST_POLL_RATE;
});

describe('awaitStagingDeploys', () => {
test('Should wait for all running staging deploys to finish', () => {
mockListWorkflowRuns.mockResolvedValueOnce({
data: {
workflow_runs: [
COMPLETED_WORKFLOW,
INCOMPLETE_WORKFLOW,
INCOMPLETE_WORKFLOW,
],
},
});
mockListWorkflowRuns.mockResolvedValueOnce({
data: {
workflow_runs: [
COMPLETED_WORKFLOW,
COMPLETED_WORKFLOW,
INCOMPLETE_WORKFLOW,
],
},
});
mockListWorkflowRuns.mockResolvedValueOnce({
data: {
workflow_runs: [
COMPLETED_WORKFLOW,
COMPLETED_WORKFLOW,
COMPLETED_WORKFLOW,
],
},
});

const consoleSpy = jest.spyOn(console, 'log');
return run()
.then(() => {
expect(consoleSpy).toHaveBeenCalledTimes(3);
expect(consoleSpy).toHaveBeenNthCalledWith(1, 'Found 2 staging deploys still running...');
expect(consoleSpy).toHaveBeenNthCalledWith(2, 'Found 1 staging deploy still running...');
expect(consoleSpy).toHaveBeenLastCalledWith('No current staging deploys found');
});
});
});

0 comments on commit a9af7a0

Please sign in to comment.