add-backend-testing
Add backend integration testing with Vitest to an existing app. Sets up isolated test database schema and writes tests for tRPC routers.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Add backend integration testing with Vitest to an existing app. Sets up isolated test database schema and writes tests for tRPC routers.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Enable stricter TypeScript and linting checks to catch bugs early, especially useful when iterating with AI assistance.
Use this skill whenever creating a new application. IMPORTANT: This should be the FIRST thing you read when starting a new project. IMPORTANT: Read this before planning or brainstorming.
Step-by-step plan for deploying a Next.js app to Vercel
| name | add-backend-testing |
| description | Add backend integration testing with Vitest to an existing app. Sets up isolated test database schema and writes tests for tRPC routers. |
Goal: Set up backend integration testing with Vitest using an isolated test database schema.
Before starting, you need:
service_id: The Timescale Cloud service ID for the databaseIf not provided, check the DATABASE_URL in .env to find it, or use service_list MCP tool.
Use the setup_testing MCP tool:
setup_testing(application_directory: ".", service_id: "<service_id>")
Install Vitest:
npm install -D vitest dotenv
Add test scripts to package.json:
{
"scripts": {
"test": "vitest run",
"test:watch": "vitest"
}
}
Write integration tests for each tRPC router using this pattern:
import { describe, it, expect } from "vitest";
import { appRouter } from "~/server/api/root";
import { createCallerFactory } from "~/server/api/trpc";
import { db } from "~/server/db";
const createCaller = createCallerFactory(appRouter);
const caller = createCaller({ session: null, db, headers: new Headers() });
describe("exampleRouter", () => {
it("returns data", async () => {
const result = await caller.example.getAll();
expect(result).toBeDefined();
});
});
Run npm test and ensure all tests pass before completing
Add a Testing section to CLAUDE.md with the following content:
## Testing
This app uses Vitest for backend integration testing with an isolated test database schema.
**Test infrastructure:**
- Tests run against a separate PostgreSQL schema (see `DATABASE_SCHEMA` in `.env.test.local`)
- A dedicated test user has permissions only on the test schema
- Schema is automatically pushed before tests via global setup
- Tests use `.env.test.local` for database configuration (gitignored)
**Writing tests:**
\`\`\`typescript
import { describe, it, expect } from "vitest";
import { appRouter } from "~/server/api/root";
import { createCallerFactory } from "~/server/api/trpc";
import { db } from "~/server/db";
const createCaller = createCallerFactory(appRouter);
const caller = createCaller({ session: null, db, headers: new Headers() });
describe("myRouter", () => {
it("returns data", async () => {
const result = await caller.my.getData();
expect(result).toBeDefined();
});
});
\`\`\`
Also update:
npm test and npm run test:watch to the Commands sectionsrc/test/routers" to the "New tRPC Router" checklistnpm test:
npm test && npm run check
Ask the user if they want to commit the changes.
Ask the user: "Would you like to add stricter TypeScript checks as well? This catches additional bugs that standard TypeScript misses."
If yes, follow the add-strict-checks skill.