diff --git a/server/src/__tests__/agent-test-environment-routes.test.ts b/server/src/__tests__/agent-test-environment-routes.test.ts index 4bba678c432..7f43c234693 100644 --- a/server/src/__tests__/agent-test-environment-routes.test.ts +++ b/server/src/__tests__/agent-test-environment-routes.test.ts @@ -1,6 +1,6 @@ import express from "express"; import request from "supertest"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import type { ServerAdapterModule } from "../adapters/index.js"; const mockAgentService = vi.hoisted(() => ({ @@ -164,11 +164,15 @@ function mockManagedRuntime(method: "api_key" | "subscription") { ); } -async function createApp() { - const [{ agentRoutes }, { errorHandler }] = await Promise.all([ +function loadAppModules() { + return Promise.all([ vi.importActual("../routes/agents.js"), vi.importActual("../middleware/index.js"), ]); +} + +async function createApp() { + const [{ agentRoutes }, { errorHandler }] = await loadAppModules(); const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -192,8 +196,12 @@ async function unregisterTestAdapter(type: string) { } describe("agent test-environment route", () => { - beforeEach(async () => { + beforeAll(async () => { vi.resetModules(); + await loadAppModules(); + }); + + beforeEach(async () => { vi.clearAllMocks(); mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: null }); mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableManagedSandboxOnly: false }); diff --git a/server/src/__tests__/issue-execution-policy-routes.test.ts b/server/src/__tests__/issue-execution-policy-routes.test.ts index 4ead3acee65..14ac0f90c7d 100644 --- a/server/src/__tests__/issue-execution-policy-routes.test.ts +++ b/server/src/__tests__/issue-execution-policy-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"; import { normalizeIssueExecutionPolicy } from "../services/issue-execution-policy.ts"; const mockIssueService = vi.hoisted(() => ({ @@ -200,12 +200,16 @@ async function createApp(actor?: TestActor) { } describe("issue execution policy routes", () => { - beforeEach(async () => { + beforeAll(async () => { vi.resetModules(); vi.doUnmock("../services/index.js"); vi.doUnmock("../routes/issues.js"); vi.doUnmock("../middleware/index.js"); registerModuleMocks(); + await loadAppModules(); + }); + + beforeEach(() => { vi.clearAllMocks(); mockIssueService.assertCheckoutOwner.mockResolvedValue({ adoptedFromRunId: null }); mockIssueService.getByIdForUpdate.mockImplementation(async () => mockIssueService.getById()); @@ -267,9 +271,6 @@ describe("issue execution policy routes", () => { }; }); mockAccessService.hasPermission.mockResolvedValue(false); - // Finish cold imports before a test configures its request-specific mocks. - // A timed-out import must not resume a request against the next test's mocks. - await loadAppModules(); }); it.each([ diff --git a/server/src/__tests__/issue-thread-interaction-routes.test.ts b/server/src/__tests__/issue-thread-interaction-routes.test.ts index a15bb19b894..b4ff57dc9aa 100644 --- a/server/src/__tests__/issue-thread-interaction-routes.test.ts +++ b/server/src/__tests__/issue-thread-interaction-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 ASSIGNEE_AGENT_ID = "11111111-1111-4111-8111-111111111111"; const UNRELATED_AGENT_ID = "33333333-3333-4333-8333-333333333333"; @@ -309,13 +309,17 @@ async function resolveMockInteraction( } describe("issue thread interaction routes", () => { - beforeEach(async () => { + beforeAll(async () => { vi.resetModules(); vi.doUnmock("../routes/issues.js"); vi.doUnmock("../routes/authz.js"); vi.doUnmock("../middleware/index.js"); vi.doUnmock("../services/index.js"); registerModuleMocks(); + await loadAppModules(); + }, 60_000); + + beforeEach(() => { // Every mock here is a vi.hoisted() singleton. All 70+ tests share it. // A queued mockResolvedValueOnce() value can outlive its own test and // leak into a later, unrelated test. vi.resetAllMocks() drains that @@ -581,9 +585,7 @@ describe("issue thread interaction routes", () => { mockCrossIssueInfluence.sourceIssueId = ISSUE_ID; mockCrossIssueInfluence.priorCount = 0; mockCrossIssueInfluence.inserted.length = 0; - // Keep cold route imports in setup rather than the HTTP assertion timeout. - await loadAppModules(); - }, 60_000); + }); it("creates board-authored interactions", async () => { const app = await createApp();