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
- ControllableModel/SWE-Skills-Benchtheir own version, first seen Nov 8, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- ControllableModel/SWE-Skills-Bench★ 8same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository23 other files it gives its agents.
- client-document-exportSkill
- cloudflare-turnstile-nextjsSkill
- ga4-analytics-nextjsSkill
- git-repo-privacy-auditSkill
- hardened-contact-form-nextjsSkill
- html-semantic-markupSkill
- multi-step-wizard-flowSkill
- nextjs-api-routesSkill
- nextjs-app-routerSkill
- nextjs-seo-metadataSkill
- nextjs-ui-foundationSkill
- next-themes-dark-modeSkill
- playwright-e2e-testingSkill
- react-components-patternsSkill
- react-hooks-patternsSkill
- recharts-dashboardSkill
- resend-email-nextjsSkill
- server-pdf-playwrightSkill
- shadcn-ui-setupSkill
- specialized-form-inputsSkill
- tailwind-css-v4Skill
- web-accessibility-wcagSkill
- zustand-persist-stateSkill
The file
Files
Vitest Unit Testing
Install
npm install -D vitestScripts
"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
| Layer | Examples |
| Pure utils | formatters, parsers, date math |
| Schemas | Zod .safeParse valid/invalid cases |
| Calculations | totals, percentages, projections |
| Env helpers | feature flags with vi.stubEnv |
| Normalizers | migration/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 testFast 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.
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
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.

