Skip to content

Commit

Permalink
Add tests for retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KeKs0r committed Oct 31, 2024
1 parent 9e42628 commit 0449de9
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/util/retry-fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { describe, expect, it, beforeEach, afterEach, mock } from "bun:test";
import { retryFetch } from "./retry-fetch";
import { AbortError } from "p-retry";

// Mock the global fetch
const originalFetch = global.fetch;
let mockFetch: ReturnType<typeof mock>;

describe("retryFetch", () => {
beforeEach(() => {
// Reset mock before each test
mockFetch = mock(() => {});
global.fetch = mockFetch;
});

afterEach(() => {
mockFetch.mockReset();
});

it("should not retry on 401 unauthorized", async () => {
// Mock fetch to return 401
mockFetch.mockImplementation(() =>
Promise.resolve({
status: 401,
json: () => Promise.resolve({ message: "Unauthorized" }),
})
);

// Attempt the fetch
await expect(retryFetch("/test", {})).rejects.toThrow();

// Verify fetch was only called once
expect(mockFetch).toHaveBeenCalledTimes(1);
});

it("should retry on 500 GET server error", async () => {
// Mock fetch to fail twice with 500, then succeed
mockFetch
.mockImplementationOnce(() =>
Promise.resolve({
status: 500,
json: () => Promise.resolve({ message: "Server Error" }),
})
)
.mockImplementationOnce(() =>
Promise.resolve({
status: 500,
json: () => Promise.resolve({ message: "Server Error" }),
})
)
.mockImplementationOnce(() =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({ message: "Success" }),
})
);

// Attempt the fetch
await retryFetch("/test", {});

// Verify fetch was called multiple times
expect(mockFetch).toHaveBeenCalledTimes(3);
});

it("should retry on 500 POST server error", async () => {
// Mock fetch to fail twice with 500, then succeed
mockFetch
.mockImplementationOnce(() =>
Promise.resolve({
status: 500,
json: () => Promise.resolve({ message: "Server Error" }),
})
)
.mockImplementationOnce(() =>
Promise.resolve({
status: 500,
json: () => Promise.resolve({ message: "Server Error" }),
})
)
.mockImplementationOnce(() =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({ message: "Success" }),
})
);

// Attempt the fetch
await retryFetch("/test", {
method: "POST",
});

// Verify fetch was called multiple times
expect(mockFetch).toHaveBeenCalledTimes(3);
});

it("should succeed immediately on 200", async () => {
// Mock fetch to return 200
mockFetch.mockImplementation(() =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({ message: "Success" }),
})
);

// Attempt the fetch
await retryFetch("/test", {});

// Verify fetch was only called once
expect(mockFetch.mock.calls.length).toBe(1);
});
});

0 comments on commit 0449de9

Please sign in to comment.