Files
outline/server/onerror.test.ts
T
Tom Moor 5610df5a26 chore: Reduce no-explicit-any warnings in server directory (#12202)
* chore: Reduce no-explicit-any warnings in server directory

Tightens types across test response bodies, decorator signatures, the
TestServer wrapper, base class generics, and presenter Record types.
Where any is genuinely load-bearing (Sequelize model generics,
PropertyDescriptor decorator returns, plugin-registered template
classes, Fix mixin), keeps any with a targeted eslint-disable plus
reason rather than masking the constraint. Cuts server-only
no-explicit-any warnings from 162 to 70.

* fix: groups test asserts on first response instead of second

Caught by Copilot review on the no-explicit-any cleanup. Also fixes
the pre-existing getChangsetSkipped → getChangesetSkipped typo
surfaced while reviewing nearby decorator code.
2026-04-28 19:50:45 -04:00

125 lines
3.2 KiB
TypeScript

import type Koa from "koa";
import { requestErrorHandler } from "@server/logging/sentry";
import { InternalError, ValidationError, NotFoundError } from "./errors";
import onerror from "./onerror";
// Mock the requestErrorHandler from Sentry
jest.mock("@server/logging/sentry", () => ({
requestErrorHandler: jest.fn(),
}));
type MockCtx = {
headers: Record<string, string>;
headerSent: boolean;
writable: boolean;
accepts: jest.Mock;
set: jest.Mock;
res: { end: jest.Mock };
status: number | undefined;
type: string | undefined;
body: unknown;
};
type ReportableError = Error & {
status?: number;
isReportable?: boolean;
};
describe("onerror", () => {
let app: Koa;
let ctx: MockCtx;
beforeEach(() => {
// Create a mock Koa app
app = {
context: {},
} as unknown as Koa;
// Apply the onerror middleware
onerror(app);
// Create a mock context
ctx = {
headers: {},
headerSent: false,
writable: true,
accepts: jest.fn(() => "json"),
set: jest.fn(),
res: {
end: jest.fn(),
},
status: undefined,
type: undefined,
body: undefined,
};
// Clear mock calls
(requestErrorHandler as jest.Mock).mockClear();
});
it("should report InternalError to Sentry", () => {
const error = InternalError("Test internal error");
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).toHaveBeenCalledWith(error, ctx);
expect(ctx.status).toBe(500);
});
it("should not report ValidationError to Sentry", () => {
const error = ValidationError("Test validation error");
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).not.toHaveBeenCalled();
expect(ctx.status).toBe(400);
});
it("should not report NotFoundError to Sentry", () => {
const error = NotFoundError("Test not found error");
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).not.toHaveBeenCalled();
expect(ctx.status).toBe(404);
});
it("should report unknown errors without isReportable property to Sentry", () => {
const error = new Error("Unknown error") as ReportableError;
error.status = 500;
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).toHaveBeenCalledWith(error, ctx);
});
it("should report errors with invalid status codes to Sentry", () => {
const error = new Error("Invalid status error") as ReportableError;
error.status = 999;
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).toHaveBeenCalledWith(error, ctx);
});
it("should not report errors explicitly marked with isReportable: false", () => {
const error = new Error("Custom error") as ReportableError;
error.status = 500;
error.isReportable = false;
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).not.toHaveBeenCalled();
});
it("should report errors explicitly marked with isReportable: true", () => {
const error = new Error("Custom error") as ReportableError;
error.status = 400;
error.isReportable = true;
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).toHaveBeenCalledWith(error, ctx);
});
});