agentleFS
bkjain655bkjain655/agent-skills/skills/vitest-unit-testing/SKILL.md

vitest-unit-testing

Skill0 starsChanged 3 months agoMITView on GitHub

Unit testing with Vitest for TypeScript libs: pure functions, Zod schemas, env helpers, and API utilities. Use when adding unit tests, testing business logic, or validating schemas without a browser.

  • Installs packages

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Vitest Unit Testing

Install

npm install -D vitest

Scripts

"test": "vitest run",
"test:watch": "vitest"

Optional vitest.config.ts if aliases needed:

import { defineConfig } from "vitest/config";
import path from "path";

export default defineConfig({
  resolve: { alias: { "@": path.resolve(__dirname, "./src") } },
});

What to unit test

LayerExamples
Pure utilsformatters, parsers, date math
SchemasZod .safeParse valid/invalid cases
Calculationstotals, percentages, projections
Env helpersfeature flags with vi.stubEnv
Normalizersmigration/persist merge logic

Skip trivial UI unless logic is extracted.

Test file convention

Colocate: format.test.ts next to format.ts, or __tests__/ folder.

Patterns

import { describe, it, expect, afterEach, vi } from "vitest";

describe("parseAmount", () => {
  it("strips currency symbols", () => {
    expect(parseAmount("₹1,50,000")).toBe(150000);
  });
});

Env stubbing

afterEach(() => vi.unstubAllEnvs());

it("defaults when unset", () => {
  vi.stubEnv("NEXT_PUBLIC_SITE_URL", undefined);
  expect(getSiteUrl()).toBe("http://localhost:3000");
});

Schema tests

it("rejects missing email", () => {
  const r = feedbackSchema.safeParse({ name: "A", message: "hi" });
  expect(r.success).toBe(false);
});

Table-driven cases

it.each([
  [0, "0"],
  [150000, "1,50,000"],
])("formats %i as %s", (input, expected) => {
  expect(formatAmount(input)).toBe(expected);
});

Mocking fetch (route handlers)

Extract handler logic to pure function; test function directly. For integration, use fetch against dev server or MSW sparingly.

CI

- run: npm run test

Fast suite should complete in seconds — keep hundreds of tests, not thousands of DOM tests here (use Playwright for those).

Anti-patterns

  • Testing implementation details (private function names)
  • Snapshot testing large objects without meaningful assertions
  • Duplicating E2E flows in Vitest with jsdom
  • Forgetting to reset env/mocks in afterEach

Pair with: playwright-e2e-testing, nextjs-api-routes.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.