Files
oneuptime/CLI/Tests/ErrorHandler.test.ts
Nawaz Dhandala d25a97fe17 Refactor components for improved readability and consistency
- Added missing newlines at the end of files in MarkdownContent.tsx and RootCauseCard.tsx
- Reformatted shadowColor and color properties in NotesSection.tsx, SegmentedControl.tsx, MainTabNavigator.tsx, HomeScreen.tsx for better readability
- Enhanced code formatting in SectionHeader.tsx and OnCallStackNavigator.tsx for consistency
- Improved readability of getEntityId function in useAllProjectOnCallPolicies.ts
- Refactored conditional rendering in AlertDetailScreen.tsx, AlertEpisodeDetailScreen.tsx, IncidentDetailScreen.tsx, and IncidentEpisodeDetailScreen.tsx for better clarity
2026-02-15 11:47:32 +00:00

99 lines
3.3 KiB
TypeScript

import { handleError, ExitCode } from "../Core/ErrorHandler";
import * as OutputFormatter from "../Core/OutputFormatter";
describe("ErrorHandler", () => {
let exitSpy: jest.SpyInstance;
let printErrorSpy: jest.SpyInstance;
beforeEach(() => {
exitSpy = jest.spyOn(process, "exit").mockImplementation((() => {
// no-op
}) as any);
printErrorSpy = jest
.spyOn(OutputFormatter, "printError")
.mockImplementation(() => {
// no-op
});
});
afterEach(() => {
exitSpy.mockRestore();
printErrorSpy.mockRestore();
});
it("should exit with AuthError for API key errors", () => {
handleError(new Error("Invalid API key provided"));
expect(printErrorSpy).toHaveBeenCalledWith(
"Authentication error: Invalid API key provided",
);
expect(exitSpy).toHaveBeenCalledWith(ExitCode.AuthError);
});
it("should exit with AuthError for credentials errors", () => {
handleError(new Error("No credentials found"));
expect(exitSpy).toHaveBeenCalledWith(ExitCode.AuthError);
});
it("should exit with AuthError for Unauthorized errors", () => {
handleError(new Error("Unauthorized access"));
expect(exitSpy).toHaveBeenCalledWith(ExitCode.AuthError);
});
it("should exit with AuthError for 401 errors", () => {
handleError(new Error("HTTP 401 response"));
expect(exitSpy).toHaveBeenCalledWith(ExitCode.AuthError);
});
it("should exit with NotFound for 404 errors", () => {
handleError(new Error("HTTP 404 response"));
expect(printErrorSpy).toHaveBeenCalledWith("Not found: HTTP 404 response");
expect(exitSpy).toHaveBeenCalledWith(ExitCode.NotFound);
});
it("should exit with NotFound for not found errors", () => {
handleError(new Error("Resource not found"));
expect(exitSpy).toHaveBeenCalledWith(ExitCode.NotFound);
});
it("should exit with GeneralError for API error messages", () => {
handleError(new Error("API error (500): Internal Server Error"));
expect(printErrorSpy).toHaveBeenCalledWith(
"API error (500): Internal Server Error",
);
expect(exitSpy).toHaveBeenCalledWith(ExitCode.GeneralError);
});
it("should exit with GeneralError for generic Error objects", () => {
handleError(new Error("Something went wrong"));
expect(printErrorSpy).toHaveBeenCalledWith("Error: Something went wrong");
expect(exitSpy).toHaveBeenCalledWith(ExitCode.GeneralError);
});
it("should handle non-Error objects", () => {
handleError("string error");
expect(printErrorSpy).toHaveBeenCalledWith("Error: string error");
expect(exitSpy).toHaveBeenCalledWith(ExitCode.GeneralError);
});
it("should handle null error", () => {
handleError(null);
expect(printErrorSpy).toHaveBeenCalledWith("Error: null");
expect(exitSpy).toHaveBeenCalledWith(ExitCode.GeneralError);
});
it("should handle number error", () => {
handleError(42);
expect(printErrorSpy).toHaveBeenCalledWith("Error: 42");
expect(exitSpy).toHaveBeenCalledWith(ExitCode.GeneralError);
});
describe("ExitCode enum", () => {
it("should have correct values", () => {
expect(ExitCode.Success).toBe(0);
expect(ExitCode.GeneralError).toBe(1);
expect(ExitCode.AuthError).toBe(2);
expect(ExitCode.NotFound).toBe(3);
});
});
});