diff --git a/server/src/__tests__/company-portability-routes.test.ts b/server/src/__tests__/company-portability-routes.test.ts index a04c2e87ff2..68474c51575 100644 --- a/server/src/__tests__/company-portability-routes.test.ts +++ b/server/src/__tests__/company-portability-routes.test.ts @@ -1,6 +1,6 @@ import express from "express"; import request from "supertest"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const mockCompanyService = vi.hoisted(() => ({ list: vi.fn(), @@ -104,15 +104,19 @@ function registerCompanyRouteMocks() { let appImportCounter = 0; -async function createApp(actor: Record) { +async function loadCompanyRouteModules() { registerCompanyRouteMocks(); appImportCounter += 1; const routeModulePath = `../routes/companies.js?company-portability-routes-${appImportCounter}`; const middlewareModulePath = `../middleware/index.js?company-portability-routes-${appImportCounter}`; - const [{ companyRoutes }, { errorHandler }] = await Promise.all([ + return Promise.all([ import(routeModulePath) as Promise, import(middlewareModulePath) as Promise, ]); +} + +async function createApp(actor: Record) { + const [{ companyRoutes }, { errorHandler }] = await loadCompanyRouteModules(); const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -224,14 +228,7 @@ function boardActor(userId: string) { // actor is chosen per request from the `x-test-user-id` header. This lets one // app exercise cross-user isolation and the per-user duplicate guard. async function createBoardApp() { - registerCompanyRouteMocks(); - appImportCounter += 1; - const routeModulePath = `../routes/companies.js?company-portability-routes-${appImportCounter}`; - const middlewareModulePath = `../middleware/index.js?company-portability-routes-${appImportCounter}`; - const [{ companyRoutes }, { errorHandler }] = await Promise.all([ - import(routeModulePath) as Promise, - import(middlewareModulePath) as Promise, - ]); + const [{ companyRoutes }, { errorHandler }] = await loadCompanyRouteModules(); const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -335,6 +332,10 @@ const importMeta = { }; describe("company portability routes", () => { + beforeAll(async () => { + await loadCompanyRouteModules(); + }); + beforeEach(() => { vi.clearAllMocks(); mockAgentService.getById.mockImplementation(async (id: string) => ({ diff --git a/server/src/__tests__/routines-routes.test.ts b/server/src/__tests__/routines-routes.test.ts index 2e0de518189..1a3edb51b7c 100644 --- a/server/src/__tests__/routines-routes.test.ts +++ b/server/src/__tests__/routines-routes.test.ts @@ -174,11 +174,16 @@ function registerModuleMocks() { })); } -async function createApp(actor: Record) { +async function loadRouteModules() { // Load the route after its mocks are installed. Concurrent importActual // calls through the middleware barrel can cache unmocked dependencies. const { errorHandler } = await import("../middleware/error-handler.js"); const { routineRoutes } = await import("../routes/routines.js"); + return { errorHandler, routineRoutes }; +} + +async function createApp(actor: Record) { + const { errorHandler, routineRoutes } = await loadRouteModules(); const app = express(); app.use(express.json({ verify: (req, _res, buf) => { (req as any).rawBody = buf; } })); app.use((req, _res, next) => { @@ -203,7 +208,7 @@ describe("routine routes", () => { })); }); - beforeEach(() => { + beforeEach(async () => { vi.resetModules(); vi.doUnmock("@paperclipai/shared/telemetry"); vi.doUnmock("../telemetry.js"); @@ -327,6 +332,7 @@ describe("routine routes", () => { status: "resolved", }); mockAnnotationService.remapOpenThreadsForRoutineDocument.mockResolvedValue([]); + await loadRouteModules(); }); it("passes project filters to the routine list service", async () => {