Skip to content

Commit

Permalink
Try to test it
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Oct 8, 2024
1 parent 50d52fa commit ba78d35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MatcherFunction } from 'expect'

declare global {
namespace jest {
interface Matchers<R> {
Expand All @@ -6,6 +8,12 @@ declare global {
message: string
): R;
}
interface Expect {
matchesPredicate(predicate: (actual: any) => boolean): any
}
interface ExpectExtendMap {
matchesPredicate: MatcherFunction<[predicate: (actual: any) => boolean]>
}
}
}

Expand Down Expand Up @@ -59,6 +67,12 @@ But instead it was:
message: () => "Expected function to throw but it did not",
};
},
matchesPredicate(actual: any, predicate: (actual: any) => boolean) {
return {
pass: predicate(actual),
message: () => "Expected object that passes predicate",
};
},
});

export default undefined;
12 changes: 11 additions & 1 deletion src/domain/release-archive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ describe("fetch", () => {

expect(axiosRetry).toHaveBeenCalledWith(axios, {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
retryDelay: expect.matchesPredicate((retryDelayFn: Function) => {
// Make sure the retry delays follow exponential backoff
// and the final retry happens after at least 1 minute total.
let firstRetryDelay = retryDelayFn(0);
let secondRetryDelay = retryDelayFn(1);
let thirdRetryDelay = retryDelayFn(2);
return 10000 <= firstRetryDelay && firstRetryDelay <= 12000
&& 20000 <= secondRetryDelay && secondRetryDelay <= 24000
&& 40000 <= thirdRetryDelay && thirdRetryDelay <= 48000;
}),
shouldResetTimeout: true,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/domain/release-archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ReleaseArchive {
}
}

function exponentialDelay(retryCount: number, error: AxiosError): number {
function exponentialDelay(retryCount: number, error: AxiosError = undefined): number {
// Using exponential backoff with 3 retries and a delay factor of 10 seconds
// gives you at least 70 seconds to publish a release archive.
const tenSeconds = 10000;
Expand Down

0 comments on commit ba78d35

Please sign in to comment.