Use `vitest` for backend tests: - `bench` script now uses [vitest's `bench`](https://vitest.dev/api/#bench) - e2e test fails with same error happening now with jest:`data-testid` attribute not present. vitest left, jest right <img width="1264" height="603" alt="image" src="https://github.com/user-attachments/assets/56fc43a6-e143-4208-bf14-7e016aae7fa9" />
86 lines
2.3 KiB
JavaScript
86 lines
2.3 KiB
JavaScript
// @ts-check
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import "@testing-library/jest-dom/vitest";
|
|
|
|
import { logger } from "../src/common/log.js";
|
|
import { retryer } from "../src/common/retryer.js";
|
|
|
|
const fetcher = vi.fn((variables, token) => {
|
|
logger.log(variables, token);
|
|
return new Promise((res) => res({ data: "ok" }));
|
|
});
|
|
|
|
const fetcherFail = vi.fn(() => {
|
|
return new Promise((res) =>
|
|
res({ data: { errors: [{ type: "RATE_LIMITED" }] } }),
|
|
);
|
|
});
|
|
|
|
const fetcherFailOnSecondTry = vi.fn((_vars, _token, retries) => {
|
|
return new Promise((res) => {
|
|
// faking rate limit
|
|
// @ts-ignore
|
|
if (retries < 1) {
|
|
return res({ data: { errors: [{ type: "RATE_LIMITED" }] } });
|
|
}
|
|
return res({ data: "ok" });
|
|
});
|
|
});
|
|
|
|
const fetcherFailWithMessageBasedRateLimitErr = vi.fn(
|
|
(_vars, _token, retries) => {
|
|
return new Promise((res) => {
|
|
// faking rate limit
|
|
// @ts-ignore
|
|
if (retries < 1) {
|
|
return res({
|
|
data: {
|
|
errors: [
|
|
{
|
|
type: "ASDF",
|
|
message: "API rate limit already exceeded for user ID 11111111",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
}
|
|
return res({ data: "ok" });
|
|
});
|
|
},
|
|
);
|
|
|
|
describe("Test Retryer", () => {
|
|
it("retryer should return value and have zero retries on first try", async () => {
|
|
let res = await retryer(fetcher, {});
|
|
|
|
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
expect(res).toStrictEqual({ data: "ok" });
|
|
});
|
|
|
|
it("retryer should return value and have 2 retries", async () => {
|
|
let res = await retryer(fetcherFailOnSecondTry, {});
|
|
|
|
expect(fetcherFailOnSecondTry).toHaveBeenCalledTimes(2);
|
|
expect(res).toStrictEqual({ data: "ok" });
|
|
});
|
|
|
|
it("retryer should return value and have 2 retries with message based rate limit error", async () => {
|
|
let res = await retryer(fetcherFailWithMessageBasedRateLimitErr, {});
|
|
|
|
expect(fetcherFailWithMessageBasedRateLimitErr).toHaveBeenCalledTimes(2);
|
|
expect(res).toStrictEqual({ data: "ok" });
|
|
});
|
|
|
|
it("retryer should throw specific error if maximum retries reached", async () => {
|
|
try {
|
|
await retryer(fetcherFail, {});
|
|
} catch (err) {
|
|
expect(fetcherFail).toHaveBeenCalledTimes(2);
|
|
// @ts-ignore
|
|
expect(err.message).toBe("Downtime due to GitHub API rate limiting");
|
|
}
|
|
});
|
|
});
|