@messari/sdk
Version:
Messari SDK provides a type-safe, intuitive interface for accessing Messari's suite of crypto data and AI APIs.
58 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const error_1 = require("../error");
const vitest_1 = require("vitest");
(0, vitest_1.describe)("RequestTimeoutError", () => {
(0, vitest_1.describe)("constructor", () => {
(0, vitest_1.it)("should create an instance with the correct name and message", () => {
const errorMessage = "Test timeout message";
const error = new error_1.RequestTimeoutError(errorMessage);
(0, vitest_1.expect)(error.name).toBe("RequestTimeoutError");
(0, vitest_1.expect)(error.message).toBe(errorMessage);
(0, vitest_1.expect)(error).toBeInstanceOf(Error);
});
});
(0, vitest_1.describe)("rejectAfterTimeout", () => {
(0, vitest_1.it)("should resolve when the promise resolves before timeout", async () => {
const expectedValue = "success";
const promise = Promise.resolve(expectedValue);
const result = await error_1.RequestTimeoutError.rejectAfterTimeout(promise, 100);
(0, vitest_1.expect)(result).toBe(expectedValue);
});
(0, vitest_1.it)("should reject with RequestTimeoutError when timeout occurs", async () => {
// Create a promise that won't resolve until after the timeout
const promise = new Promise((resolve) => setTimeout(() => resolve("too late"), 200));
await (0, vitest_1.expect)(error_1.RequestTimeoutError.rejectAfterTimeout(promise, 50)).rejects.toThrow(error_1.RequestTimeoutError);
await (0, vitest_1.expect)(error_1.RequestTimeoutError.rejectAfterTimeout(promise, 50)).rejects.toMatchObject({
name: "RequestTimeoutError",
message: "Request timed out after 50ms",
});
});
(0, vitest_1.it)("should reject with original error when promise rejects before timeout", async () => {
const originalError = new Error("Original error");
const promise = Promise.reject(originalError);
await (0, vitest_1.expect)(error_1.RequestTimeoutError.rejectAfterTimeout(promise, 100)).rejects.toBe(originalError);
});
(0, vitest_1.it)("should clear the timeout when promise resolves", async () => {
vitest_1.vi.useFakeTimers();
const clearTimeoutSpy = vitest_1.vi.spyOn(global, "clearTimeout");
const promise = Promise.resolve("success");
const resultPromise = error_1.RequestTimeoutError.rejectAfterTimeout(promise, 1000);
await resultPromise;
(0, vitest_1.expect)(clearTimeoutSpy).toHaveBeenCalled();
clearTimeoutSpy.mockRestore();
vitest_1.vi.useRealTimers();
});
(0, vitest_1.it)("should clear the timeout when promise rejects", async () => {
vitest_1.vi.useFakeTimers();
const clearTimeoutSpy = vitest_1.vi.spyOn(global, "clearTimeout");
const promise = Promise.reject(new Error("Test error"));
const resultPromise = error_1.RequestTimeoutError.rejectAfterTimeout(promise, 1000);
await (0, vitest_1.expect)(resultPromise).rejects.toThrow("Test error");
(0, vitest_1.expect)(clearTimeoutSpy).toHaveBeenCalled();
clearTimeoutSpy.mockRestore();
vitest_1.vi.useRealTimers();
});
});
});
//# sourceMappingURL=error.test.js.map