Skip to content

Commit

Permalink
test: test backoff by mocking setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Souza committed Aug 18, 2020
1 parent 2032586 commit aabf783
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/eventHandlers/checkSuite/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import { useSetTimeoutImmediateInvocation } from '../../../test/utilities';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
Expand Down Expand Up @@ -388,6 +389,8 @@ describe('check Suite event handler', (): void => {
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

useSetTimeoutImmediateInvocation();

try {
await checkSuiteHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
Expand All @@ -399,8 +402,8 @@ describe('check Suite event handler', (): void => {
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 100...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 400...');
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 4000...');
}
});

Expand Down
22 changes: 8 additions & 14 deletions src/eventHandlers/pullRequest/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import { useSetTimeoutImmediateInvocation } from '../../../test/utilities';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
Expand Down Expand Up @@ -66,10 +67,7 @@ describe('pull request event handler', (): void => {
});
nock('https://api.github.com').post('/graphql').reply(OK);

await pullRequestHandle(octokit, 'dependabot-preview[bot]', {
maximumRetries: 2,
minimumWaitTime: 100,
});
await pullRequestHandle(octokit, 'dependabot-preview[bot]', 2);

expect(warningSpy).not.toHaveBeenCalled();
});
Expand All @@ -81,10 +79,7 @@ describe('pull request event handler', (): void => {
data: null,
});

await pullRequestHandle(octokit, 'dependabot-preview[bot]', {
maximumRetries: 2,
minimumWaitTime: 100,
});
await pullRequestHandle(octokit, 'dependabot-preview[bot]', 2);
});

it('does not approve an already approved pull request', async (): Promise<
Expand Down Expand Up @@ -136,10 +131,7 @@ describe('pull request event handler', (): void => {
})
.reply(OK);

await pullRequestHandle(octokit, 'dependabot-preview[bot]', {
maximumRetries: 2,
minimumWaitTime: 100,
});
await pullRequestHandle(octokit, 'dependabot-preview[bot]', 2);
});

it('retries up to two times before failing', async (): Promise<void> => {
Expand Down Expand Up @@ -189,6 +181,8 @@ describe('pull request event handler', (): void => {
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

useSetTimeoutImmediateInvocation();

try {
await pullRequestHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
Expand All @@ -200,8 +194,8 @@ describe('pull request event handler', (): void => {
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 100...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 400...');
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 4000...');
}
});

Expand Down
7 changes: 5 additions & 2 deletions src/eventHandlers/push/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import { useSetTimeoutImmediateInvocation } from '../../../test/utilities';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
Expand Down Expand Up @@ -300,6 +301,8 @@ describe('push event handler', (): void => {
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

useSetTimeoutImmediateInvocation();

try {
await pushHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
Expand All @@ -311,8 +314,8 @@ describe('push event handler', (): void => {
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 100...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 400...');
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
expect(logInfoSpy.mock.calls[4][0]).toStrictEqual('Retrying in 4000...');
}
});

Expand Down
10 changes: 10 additions & 0 deletions test/utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const useSetTimeoutImmediateInvocation = (): jest.SpyInstance<
NodeJS.Timeout,
[(...args: unknown[]) => void, number, ...unknown[]]
> =>
jest
.spyOn(global, 'setTimeout')
.mockImplementation(
(callback: () => void): NodeJS.Timeout =>
(callback() as unknown) as NodeJS.Timeout,
);

0 comments on commit aabf783

Please sign in to comment.