AgentSkillsCN

testing

使用Vitest对应用进行测试的指导原则

SKILL.md
--- frontmatter
name: testing
description: Guidelines for testing the application with Vitest

Testing Guidelines

Testing Framework

  • vitest is used for testing
  • Run tests using cd apps/web && pnpm test --run (not npx vitest). Don't use sandbox or the test won't run.
  • Tests are colocated next to the tested file
    • Example: dir/format.ts and dir/format.test.ts
  • AI tests are placed in the __tests__ directory and are not run by default (they use a real LLM)

Common Mocks

Server-Only Mock

ts
vi.mock("server-only", () => ({}));

Prisma Mock

ts
import { beforeEach } from "vitest";
import prisma from "@/utils/__mocks__/prisma";

vi.mock("@/utils/prisma");

describe("example", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("test", async () => {
    prisma.group.findMany.mockResolvedValue([]);
  });
});

Helpers

You can get mocks for emails, accounts, and rules here:

tsx
import { getEmail, getEmailAccount, getRule } from "@/__tests__/helpers";

Best Practices

  • Each test should be independent
  • Use descriptive test names
  • Mock external dependencies
  • Clean up mocks between tests
  • Avoid testing implementation details
  • Do not mock the Logger