TanStack Start
10 Setup Tests
Test TanStack Start server functions, route handlers, streams, and Studio wiring.
Keep tests close to the boundary you own: server functions, route helpers, and the agent wrapper.
1. Install Test Tools
pnpm add -D vitest2. Extract Route Logic
Put route behavior in a testable helper:
import { supportAgent } from "~/ai/support-agent";
export async function handleSupportPost(request: Request): Promise<Response> {
const { message } = (await request.json()) as { message?: string };
if (!message?.trim()) {
return Response.json({ error: "message is required" }, { status: 400 });
}
const response = await supportAgent.prompt(message).send();
return Response.json({ output: response.output });
}Use the helper from the route:
export const Route = createFileRoute("/api/support")({
server: {
handlers: {
POST: ({ request }) => handleSupportPost(request),
},
},
});3. Test The Helper
import { describe, expect, it } from "vitest";
import { handleSupportPost } from "~/routes/api/support";
describe("POST /api/support", () => {
it("rejects empty messages", async () => {
const response = await handleSupportPost(
new Request("http://test.local/api/support", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ message: "" }),
}),
);
expect(response.status).toBe(400);
});
});4. Test Server Functions
import { askSupport } from "~/ai/support.functions";
const result = await askSupport({
data: { message: "How long does a reset link last?" },
});
expect(result.output).toContain("30 minutes");Use a mocked agent for route/unit tests. Reserve real provider calls for narrow integration tests.
5. Test Studio Without a Port
import { Studio } from "@anvia/studio";
import { supportAgent } from "~/ai/support-agent";
const studio = new Studio([supportAgent]);
const response = await studio.fetch(
new Request("http://studio.test/agents/support/runs", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ message: "Hello" }),
}),
);
expect(response.status).toBe(200);Next
Related guides: Testing, Tools and Pipelines, and Studio and Providers.
