elie222

testing

Guidelines for testing the application with Vitest

elie222 11,090 1,363 Updated 3mo ago
GitHub

Install

npx skillscat add elie222/inbox-zero/testing

Install via the SkillsCat registry.

SKILL.md

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

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

Prisma Mock

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:

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