Skip to content

Commit

Permalink
fix: failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliannemarik committed Oct 6, 2023
1 parent 54baa62 commit 2b4bad8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/common/src/utils/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ export const poll = async (
timeBetweenRequests?: number;
}
): Promise<any> => {
const { timeout = 30000, timeBetweenRequests = 3000 } = opts || {};
const options =
opts || /* istanbul ignore next - must pass in overrides for tests */ {};
const timeout = options.timeout || 30000;
const timeBetweenRequests =
options.timeBetweenRequests ||
/* istanbul ignore next - cannot delay by this much in tests */ 3000;

let resp: any;
let requestCount = 0;

do {
// on subsequent requests, check if the configured
// On subsequent requests, check if the configured
// timeout has been reached
// If YES: throw an error
// If NO: delay before the next request
Expand Down
10 changes: 4 additions & 6 deletions packages/common/test/utils/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { poll } from "../../src/utils/poll";

describe("poll", () => {
it("polls the request function until the validation function succeeds", async () => {
const validationFn = (resp) => resp && resp.id;
const validationFn = (resp: any) => resp && resp.id;
const requestFnSpy = jasmine
.createSpy()
.and.returnValues(Promise.resolve({}), Promise.resolve({ id: "00c" }));
Expand All @@ -17,21 +17,19 @@ describe("poll", () => {
expect(chk.id).toBe("00c");
});
it("throws an error when the timeout is reached", async () => {
const validationFn = (resp) => resp && resp.id;
const validationFn = (resp: any) => resp && resp.id;
const requestFnSpy = jasmine
.createSpy()
.and.returnValue(Promise.resolve({}));

let errorMessage = "Polling timeout";
try {
await poll(requestFnSpy, validationFn, {
timeBetweenRequests: 100,
timeout: 200,
});
} catch (error) {
errorMessage = error.message;
expect(requestFnSpy).toHaveBeenCalledTimes(2);
expect((error as Error).message).toBe("Polling timeout");
}
expect(requestFnSpy).toHaveBeenCalledTimes(2);
expect(errorMessage).toBe("Polling timeout");
});
});

0 comments on commit 2b4bad8

Please sign in to comment.