Files
sendou.ink/app/features/dev-tools/core/ServerConsole.server.test.ts
Kalle f7372324fe
Some checks failed
E2E Tests / e2e (push) Has been cancelled
Tests and checks on push / run-checks-and-tests (push) Has been cancelled
Updates translation progress / update-translation-progress-issue (push) Has been cancelled
E2E Tests / publish-docs-screenshots (push) Has been cancelled
Dev tools (#3471)
2026-10-25 11:43:36 +02:00

88 lines
2.3 KiB
TypeScript

/** biome-ignore-all lint/suspicious/noConsole: logs to the console under test */
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import * as RequestLog from "./RequestLog.server";
import * as ServerConsole from "./ServerConsole.server";
const originalError = console.error;
const originalWarn = console.warn;
const errorSpy = vi.fn();
describe("ServerConsole.install", () => {
beforeEach(() => {
errorSpy.mockClear();
console.error = errorSpy;
console.warn = vi.fn();
ServerConsole.install();
});
afterEach(() => {
console.error = originalError;
console.warn = originalWarn;
globalThis.__devToolsServerConsole = undefined;
});
test("records console errors and warnings, newest first", () => {
console.error("first");
console.warn("second");
expect(ServerConsole.recent()).toMatchObject([
{ level: "warn", message: "second", path: null },
{ level: "error", message: "first", path: null },
]);
});
test("still logs to the original console", () => {
console.error("boom");
expect(errorSpy).toHaveBeenCalledWith("boom");
});
test("counts a repeated message once", () => {
console.error("boom");
console.error("boom");
expect(ServerConsole.recent()).toMatchObject([
{ message: "boom", count: 2 },
]);
});
test("strips terminal colors", () => {
console.error("\u001b[31mred\u001b[39m");
expect(ServerConsole.recent()[0].message).toBe("red");
});
test("skips browser errors Vite forwards to the terminal", () => {
console.error(
"9:49:11 AM [vite] (client) [Unhandled error] Error: Thrown in the browser",
);
expect(ServerConsole.recent()).toEqual([]);
});
test("attaches the request being handled", async () => {
await RequestLog.track(new Request("http://localhost/builds"), async () => {
console.error("in a loader");
return new Response(null);
});
expect(ServerConsole.recent()[0].path).toBe("GET /builds");
});
test("patches the console once when installed again", () => {
ServerConsole.install();
console.error("boom");
expect(ServerConsole.recent()).toMatchObject([{ count: 1 }]);
});
test("attaches the request it is told about", () => {
ServerConsole.withRequest(
new Request("http://localhost/to/1.data?_routes=root"),
() => console.error("in handleError"),
);
expect(ServerConsole.recent()[0].path).toBe("GET /to/1");
});
});