drop/server/test/setup.ts
BillyOutlast 51af9103bb
test(server): add H3 event stubs and createMockH3Event factory (#30)
* test(server): add H3 event stubs and createMockH3Event factory

Expanded test/setup.ts with 14 more H3/Nuxt globals:
- Request accessors: readFormDataBody, getCookie, getRequestURL, getRequestIP
- Response mutators: setHeader, setCookie, deleteCookie, setResponseStatus, setResponseHeaders
- Response senders: sendRedirect, sendStream, sendError, sendNoContent
- Plugin helpers: defineNitroPlugin, defineNitroErrorHandler

Added server/test/utils/h3.ts with createMockH3Event factory.
Returns a partial H3Event with vi.fn spies for setHeader/sendError/
setCookie/etc. so handlers can be tested without booting Nuxt.
Supports method/url/body/query/headers/cookies/routerParams overrides.

Added server/test/unit/h3-factory.test.ts with 2 tests for the
factory itself. 28 tests pass (up from 26).

Refs plan at .opencode/plans/hyperplan-dep-tdd-coverage.md PR5.

* test: prettier format setup.ts + fix await in h3-factory test (#35)

Co-authored-by: bot <ci@local>

* test: make 'accepts overrides' it() async for await resolves (#37)

Co-authored-by: bot <ci@local>

---------

Co-authored-by: bot <ci@local>
2026-07-25 14:18:52 -04:00

69 lines
2.3 KiB
TypeScript

import { afterAll, beforeAll } from "vitest";
import { setupAllMocks, teardownTestMocks } from "./mocks";
// Stub Nuxt globals that handlers may rely on.
// Real handler runs in Nuxt server context; tests don't have Nuxt loaded.
(globalThis as Record<string, unknown>).defineRouteMeta = () => {};
(globalThis as Record<string, unknown>).defineEventHandler = <T>(
handler: T,
): T => handler;
// Request accessors
(globalThis as Record<string, unknown>).getHeader = () => undefined;
(globalThis as Record<string, unknown>).getQuery = () => ({});
(globalThis as Record<string, unknown>).getRouterParam = () => undefined;
(globalThis as Record<string, unknown>).readBody = async () => ({});
(globalThis as Record<string, unknown>).readFormDataBody = async () =>
new FormData();
(globalThis as Record<string, unknown>).getCookie = () => undefined;
(globalThis as Record<string, unknown>).getRequestURL = () =>
new URL("http://localhost");
(globalThis as Record<string, unknown>).getRequestIP = () =>
({ ip: "127.0.0.1", ipv6: undefined }) as {
ip: string;
ipv6: string | undefined;
};
// Response mutators
(globalThis as Record<string, unknown>).setHeader = () => undefined;
(globalThis as Record<string, unknown>).setCookie = (
_name: string,
_value: string,
_opts?: unknown,
) => undefined;
(globalThis as Record<string, unknown>).deleteCookie = () => undefined;
(globalThis as Record<string, unknown>).setResponseStatus = (
_code: number,
_msg?: string,
) => undefined;
(globalThis as Record<string, unknown>).setResponseHeaders = (
_headers: Record<string, string>,
) => undefined;
// Response senders
(globalThis as Record<string, unknown>).sendRedirect = (
_url: string,
_code = 302,
) => undefined;
(globalThis as Record<string, unknown>).sendStream = () => undefined;
(globalThis as Record<string, unknown>).sendError = (
_code: number,
_msg?: string,
) => undefined;
(globalThis as Record<string, unknown>).sendNoContent = () => undefined;
// Error helpers
(globalThis as Record<string, unknown>).createError = (err: unknown) => err;
(globalThis as Record<string, unknown>).defineNitroPlugin = <T>(plugin: T): T =>
plugin;
(globalThis as Record<string, unknown>).defineNitroErrorHandler = <T>(
handler: T,
): T => handler;
beforeAll(() => {
setupAllMocks();
});
afterAll(() => {
teardownTestMocks();
});