45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { execSync } from "node:child_process";
|
|
import { beforeAll, afterAll } from "vitest";
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || "test";
|
|
process.env.DATABASE_URL =
|
|
process.env.DATABASE_URL ||
|
|
"postgres://app:app@localhost:5432/skymoney";
|
|
process.env.PORT = process.env.PORT || "0"; // fastify can bind an ephemeral port
|
|
process.env.HOST ??= "127.0.0.1";
|
|
process.env.CORS_ORIGIN = process.env.CORS_ORIGIN || "";
|
|
process.env.AUTH_DISABLED = process.env.AUTH_DISABLED || "1";
|
|
process.env.SEED_DEFAULT_BUDGET = process.env.SEED_DEFAULT_BUDGET || "1";
|
|
|
|
export const prisma = new PrismaClient();
|
|
|
|
// hard reset for a single user
|
|
export async function resetUser(userId: string) {
|
|
await prisma.allocation.deleteMany({ where: { userId } });
|
|
await prisma.transaction.deleteMany({ where: { userId } });
|
|
await prisma.incomeEvent.deleteMany({ where: { userId } });
|
|
await prisma.fixedPlan.deleteMany({ where: { userId } });
|
|
await prisma.variableCategory.deleteMany({ where: { userId } });
|
|
await prisma.user.deleteMany({ where: { id: userId } });
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
// make sure the schema is applied before running tests
|
|
execSync("npx prisma migrate deploy", { stdio: "inherit" });
|
|
|
|
// Ensure a clean slate: wipe all tables to avoid cross-file leakage
|
|
await prisma.$transaction([
|
|
prisma.allocation.deleteMany({}),
|
|
prisma.transaction.deleteMany({}),
|
|
prisma.incomeEvent.deleteMany({}),
|
|
prisma.fixedPlan.deleteMany({}),
|
|
prisma.variableCategory.deleteMany({}),
|
|
prisma.user.deleteMany({}),
|
|
]);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await prisma.$disconnect();
|
|
});
|