cpns / packages /api /src /lib /__tests__ /errors.test.ts
rogasper's picture
feat: enhance testing capabilities and introduce new components for question management. Add testing section in AGENTS.md with instructions for running tests and configuring the test environment. Update package.json files to include test scripts and integrate @electric-sql/pglite for in-memory database testing. Introduce CalloutCard and update FilterBar and SoalBrowser components to support visibility filters for private and public questions. Implement new test cases for various functionalities across the application.
92a1d83
Raw
History Blame Contribute Delete
2.3 kB
import { describe, expect, it } from "bun:test";
import { TRPCError } from "@trpc/server";
import {
throwUnauthorized,
throwForbidden,
throwNotFound,
throwBadRequest,
throwInternal,
} from "../errors";
function expectThrowsTRPCError(fn: () => never, code: string, message: string) {
try {
fn();
expect.unreachable("Should have thrown");
} catch (err) {
expect(err).toBeInstanceOf(TRPCError);
const trpcErr = err as TRPCError;
expect(trpcErr.code).toBe(code);
expect(trpcErr.message).toBe(message);
}
}
describe("throwUnauthorized", () => {
it("throws UNAUTHORIZED with default message", () => {
expectThrowsTRPCError(throwUnauthorized, "UNAUTHORIZED", "Unauthorized");
});
it("throws UNAUTHORIZED with custom message", () => {
expectThrowsTRPCError(() => throwUnauthorized("Custom message"), "UNAUTHORIZED", "Custom message");
});
});
describe("throwForbidden", () => {
it("throws FORBIDDEN with default message", () => {
expectThrowsTRPCError(throwForbidden, "FORBIDDEN", "Forbidden");
});
it("throws FORBIDDEN with custom message", () => {
expectThrowsTRPCError(() => throwForbidden("Access denied"), "FORBIDDEN", "Access denied");
});
});
describe("throwNotFound", () => {
it("throws NOT_FOUND with default resource message", () => {
expectThrowsTRPCError(throwNotFound, "NOT_FOUND", "Resource not found");
});
it("throws NOT_FOUND with custom resource name", () => {
expectThrowsTRPCError(() => throwNotFound("Question"), "NOT_FOUND", "Question not found");
});
});
describe("throwBadRequest", () => {
it("throws BAD_REQUEST with default message", () => {
expectThrowsTRPCError(throwBadRequest, "BAD_REQUEST", "Bad request");
});
it("throws BAD_REQUEST with custom message", () => {
expectThrowsTRPCError(() => throwBadRequest("Invalid input"), "BAD_REQUEST", "Invalid input");
});
});
describe("throwInternal", () => {
it("throws INTERNAL_SERVER_ERROR with default message", () => {
expectThrowsTRPCError(throwInternal, "INTERNAL_SERVER_ERROR", "Internal server error");
});
it("throws INTERNAL_SERVER_ERROR with custom message", () => {
expectThrowsTRPCError(() => throwInternal("DB connection failed"), "INTERNAL_SERVER_ERROR", "DB connection failed");
});
});