Files
moyosapp_beta.0.0.3.3_beta1/jest.setup.node.api.js

58 lines
2.4 KiB
JavaScript

// Node API-route test environment setup.
// We keep Web API polyfills, and (by default) bypass CSRF in API route unit tests.
// CSRF correctness is still covered by `src/lib/__tests__/csrf.test.ts` in the "lib" project.
// Reuse base node polyfills
require("./jest.setup.node.js");
// Provide minimal env for API route unit tests.
// Some routes (e.g. /api/health) explicitly check for these at runtime.
process.env.NODE_ENV = process.env.NODE_ENV || "test";
process.env.NEXT_PUBLIC_APP_URL = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000";
process.env.APP_URL = process.env.APP_URL || "http://localhost:3000";
process.env.SESSION_SECRET =
process.env.SESSION_SECRET || "test-session-secret-32-chars-minimum-0000000000000000";
process.env.ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || "test-admin-password-12345";
process.env.CSRF_SECRET = process.env.CSRF_SECRET || "test-csrf-secret";
process.env.DATABASE_URL =
process.env.DATABASE_URL || "postgresql://test:test@localhost:5432/test?schema=public";
// Default DB mock for route handler tests (prevents accidental Prisma initialization).
jest.mock("@/lib/db");
// Avoid opening Redis connections/timers in unit tests.
jest.mock("@/lib/rate-limit", () => ({
checkGuestbookRateLimit: jest.fn(async () => ({ allowed: true })),
checkGalleryRateLimit: jest.fn(async () => ({ allowed: true })),
checkMusicRateLimit: jest.fn(async () => ({ allowed: true })),
checkRSVPRateLimit: jest.fn(async () => ({ allowed: true })),
checkTributeRateLimit: jest.fn(async () => ({ allowed: true })),
checkAdminRateLimit: jest.fn(async () => ({ allowed: true })),
getRateLimitMetrics: jest.fn(async () => ({})),
}));
// Avoid real-time subsystems opening long-lived connections.
jest.mock("@/lib/realtime", () => ({
RealtimeManager: { sendEvent: jest.fn(async () => {}) },
}));
// Prevent real outbound network calls during unit tests (avoids open sockets/handles).
// Individual tests can override `global.fetch` as needed.
global.fetch = jest.fn(async () => {
return new Response(JSON.stringify({}), {
status: 200,
headers: { "Content-Type": "application/json" },
});
});
// Default CSRF bypass for route handler unit tests
jest.mock("@/lib/csrf", () => {
const actual = jest.requireActual("@/lib/csrf");
return {
...actual,
verifyCSRF: jest.fn(async () => true),
createCSRFProtectedResponse: jest.fn(async (response) => response),
};
});