jest utils p2
This commit is contained in:
parent
a5d69f35fa
commit
ea6d09389a
7 changed files with 52 additions and 54 deletions
|
|
@ -19,6 +19,7 @@ export default {
|
|||
__dirname: path.dirname(fileURLToPath(import.meta.url)),
|
||||
__filename: path.resolve(fileURLToPath(import.meta.url)),
|
||||
},
|
||||
|
||||
globalSetup: "<rootDir>/jest.global-setup.ts",
|
||||
setupFilesAfterEnv: ["<rootDir>/jest.setup-after-env.ts"],
|
||||
maxWorkers: 1 // equivalent to CLI --runInBand
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// https://github.com/mswjs/msw/issues/1576#issuecomment-1482643055
|
||||
|
||||
import { TextDecoder, TextEncoder } from "util";
|
||||
|
||||
import { jest } from "@jest/globals";
|
||||
|
|
@ -10,6 +11,7 @@ if (process.env.DEBUG === "jest") {
|
|||
|
||||
const globalThis = global as any;
|
||||
|
||||
globalThis.jest = jest;
|
||||
globalThis.fetch = fetch;
|
||||
globalThis.Request = Request;
|
||||
globalThis.Response = Response;
|
||||
|
|
|
|||
8
core/package-lock.json
generated
8
core/package-lock.json
generated
|
|
@ -27,7 +27,7 @@
|
|||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.14.0",
|
||||
"diff": "^7.0.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"fastest-levenshtein": "^1.0.16",
|
||||
"follow-redirects": "^1.15.5",
|
||||
"google-auth-library": "^9.14.2",
|
||||
|
|
@ -7882,9 +7882,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/dotenv": {
|
||||
"version": "16.4.5",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz",
|
||||
"integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==",
|
||||
"version": "16.4.7",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz",
|
||||
"integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@
|
|||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.14.0",
|
||||
"diff": "^7.0.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"fastest-levenshtein": "^1.0.16",
|
||||
"follow-redirects": "^1.15.5",
|
||||
"google-auth-library": "^9.14.2",
|
||||
|
|
|
|||
|
|
@ -1,41 +1,35 @@
|
|||
// core/util/withExponentialBackoff.test.ts
|
||||
|
||||
import {
|
||||
withExponentialBackoff,
|
||||
RETRY_AFTER_HEADER,
|
||||
} from "./withExponentialBackoff";
|
||||
|
||||
// Helper function to simulate delay
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
const MAX_RETRIES = 4;
|
||||
const INITIAL_DELAY_SECONDS = 0.01;
|
||||
|
||||
describe("withExponentialBackoff", () => {
|
||||
it("should return result immediately if apiCall is successful", async () => {
|
||||
const mockApiCall = jest.fn().mockResolvedValue("success");
|
||||
// beforeEach(() => {
|
||||
// jest.useFakeTimers();
|
||||
// jest.clearAllMocks();
|
||||
// });
|
||||
|
||||
// afterEach(() => {
|
||||
// jest.useRealTimers();
|
||||
// });
|
||||
|
||||
it("should return result on first successful API call", async () => {
|
||||
const apiCall = jest.fn().mockResolvedValue("success");
|
||||
|
||||
const result = await withExponentialBackoff(
|
||||
apiCall,
|
||||
MAX_RETRIES,
|
||||
INITIAL_DELAY_SECONDS,
|
||||
);
|
||||
|
||||
const result = await withExponentialBackoff(mockApiCall);
|
||||
expect(result).toBe("success");
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(1);
|
||||
expect(apiCall).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should retry on API error 429 and eventually succeed", async () => {
|
||||
// Mock an API function that returns HTTP 429 twice then succeeds
|
||||
const mockApiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce({
|
||||
response: { status: 429, headers: new Map() },
|
||||
})
|
||||
.mockRejectedValueOnce({
|
||||
response: { status: 429, headers: new Map() },
|
||||
})
|
||||
.mockResolvedValue("success");
|
||||
|
||||
const result = await withExponentialBackoff(mockApiCall, 3, 0.001); // Use a short delay for testing
|
||||
expect(result).toBe("success");
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("should respect Retry-After header value for delay", async () => {
|
||||
const mockApiCall = jest
|
||||
it("should retry on API rate limit and succeed", async () => {
|
||||
const apiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce({
|
||||
response: {
|
||||
|
|
@ -45,32 +39,33 @@ describe("withExponentialBackoff", () => {
|
|||
})
|
||||
.mockResolvedValue("success");
|
||||
|
||||
const startTime = Date.now();
|
||||
const result = await withExponentialBackoff(mockApiCall, 2, 1);
|
||||
const endTime = Date.now();
|
||||
const result = await withExponentialBackoff(
|
||||
apiCall,
|
||||
MAX_RETRIES,
|
||||
INITIAL_DELAY_SECONDS,
|
||||
);
|
||||
|
||||
expect(result).toBe("success");
|
||||
expect(endTime - startTime).toBeGreaterThanOrEqual(1000); // At least 1 second delay
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(2);
|
||||
expect(apiCall).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("should throw error after maxRetries are exhausted", async () => {
|
||||
const mockApiCall = jest.fn().mockRejectedValue(new Error("Network Error"));
|
||||
it("should stop retrying after maxRetries and throw error", async () => {
|
||||
const apiCall = jest.fn().mockRejectedValue({
|
||||
response: { status: 429, headers: new Map([[RETRY_AFTER_HEADER, "1"]]) },
|
||||
});
|
||||
|
||||
await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow(
|
||||
"Network Error",
|
||||
);
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(3);
|
||||
await expect(
|
||||
withExponentialBackoff(apiCall, MAX_RETRIES, INITIAL_DELAY_SECONDS),
|
||||
).rejects.toThrow("Failed to make API call after multiple retries");
|
||||
expect(apiCall).toHaveBeenCalledTimes(MAX_RETRIES);
|
||||
});
|
||||
|
||||
it("should throw the error immediately if it is not rate limit reached error", async () => {
|
||||
const mockApiCall = jest
|
||||
.fn()
|
||||
.mockRejectedValue(new Error("Unexpected Error"));
|
||||
it("should throw on non-rate limit errors", async () => {
|
||||
const apiCall = jest.fn().mockRejectedValue(new Error("Server Error"));
|
||||
|
||||
await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow(
|
||||
"Unexpected Error",
|
||||
);
|
||||
expect(mockApiCall).toHaveBeenCalledTimes(1);
|
||||
await expect(
|
||||
withExponentialBackoff(apiCall, MAX_RETRIES, INITIAL_DELAY_SECONDS),
|
||||
).rejects.toThrow("Server Error");
|
||||
expect(apiCall).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
2
extensions/vscode/package-lock.json
generated
2
extensions/vscode/package-lock.json
generated
|
|
@ -112,7 +112,7 @@
|
|||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.14.0",
|
||||
"diff": "^7.0.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"fastest-levenshtein": "^1.0.16",
|
||||
"follow-redirects": "^1.15.5",
|
||||
"google-auth-library": "^9.14.2",
|
||||
|
|
|
|||
2
gui/package-lock.json
generated
2
gui/package-lock.json
generated
|
|
@ -121,7 +121,7 @@
|
|||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.14.0",
|
||||
"diff": "^7.0.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"fastest-levenshtein": "^1.0.16",
|
||||
"follow-redirects": "^1.15.5",
|
||||
"google-auth-library": "^9.14.2",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue