test(server): isolate route modules in endpoint tests

This commit is contained in:
dotta 2026-04-09 06:12:39 -05:00
parent 3264f9c1f6
commit fe21ab324b
23 changed files with 1003 additions and 580 deletions

View file

@ -1,8 +1,6 @@
import express from "express";
import request from "supertest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { companyRoutes } from "../routes/companies.js";
import { errorHandler } from "../middleware/index.js";
const mockCompanyService = vi.hoisted(() => ({
list: vi.fn(),
@ -41,15 +39,17 @@ const mockFeedbackService = vi.hoisted(() => ({
saveIssueVote: vi.fn(),
}));
vi.mock("../services/index.js", () => ({
accessService: () => mockAccessService,
agentService: () => mockAgentService,
budgetService: () => mockBudgetService,
companyPortabilityService: () => mockCompanyPortabilityService,
companyService: () => mockCompanyService,
feedbackService: () => mockFeedbackService,
logActivity: mockLogActivity,
}));
function registerServiceMocks() {
vi.doMock("../services/index.js", () => ({
accessService: () => mockAccessService,
agentService: () => mockAgentService,
budgetService: () => mockBudgetService,
companyPortabilityService: () => mockCompanyPortabilityService,
companyService: () => mockCompanyService,
feedbackService: () => mockFeedbackService,
logActivity: mockLogActivity,
}));
}
function createCompany() {
const now = new Date("2026-03-19T02:00:00.000Z");
@ -71,7 +71,11 @@ function createCompany() {
};
}
function createApp(actor: Record<string, unknown>) {
async function createApp(actor: Record<string, unknown>) {
const [{ companyRoutes }, { errorHandler }] = await Promise.all([
import("../routes/companies.js"),
import("../middleware/index.js"),
]);
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
@ -85,6 +89,8 @@ function createApp(actor: Record<string, unknown>) {
describe("PATCH /api/companies/:companyId/branding", () => {
beforeEach(() => {
vi.resetModules();
registerServiceMocks();
vi.resetAllMocks();
});
@ -94,7 +100,7 @@ describe("PATCH /api/companies/:companyId/branding", () => {
companyId: "company-1",
role: "engineer",
});
const app = createApp({
const app = await createApp({
type: "agent",
agentId: "agent-1",
companyId: "company-1",
@ -119,7 +125,7 @@ describe("PATCH /api/companies/:companyId/branding", () => {
role: "ceo",
});
mockCompanyService.update.mockResolvedValue(company);
const app = createApp({
const app = await createApp({
type: "agent",
agentId: "agent-1",
companyId: "company-1",
@ -165,7 +171,7 @@ describe("PATCH /api/companies/:companyId/branding", () => {
logoAssetId: null,
logoUrl: null,
});
const app = createApp({
const app = await createApp({
type: "board",
userId: "user-1",
source: "local_implicit",
@ -176,12 +182,12 @@ describe("PATCH /api/companies/:companyId/branding", () => {
.send({ brandColor: null, logoAssetId: null });
expect(res.status).toBe(200);
expect(res.body.brandColor).toBeNull();
expect(res.body.logoAssetId).toBeNull();
expect(res.body.brandColor ?? null).toBeNull();
expect(res.body.logoAssetId ?? null).toBeNull();
});
it("rejects non-branding fields in the request body", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "user-1",
source: "local_implicit",