test(server): reuse remaining serialized route modules

Load route modules once per suite while keeping every test case and assertion.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-10-15 01:25:57 -07:00
1 parent fd057f6ac4
commit cd77c1c4dc
3 files changed
+25 -14

No files matched your search

@@ -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<typeof import("../routes/agents.js")>("../routes/agents.js"),
vi.importActual<typeof import("../middleware/index.js")>("../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 });
@@ -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([
@@ -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();