vitest-testing
Vitest Testing
Test File Placement
| Source location | Test location |
|---|---|
app/composables/<name>.ts |
tests/app/composables/<name>.test.ts |
app/components/<Name>.vue |
app/components/__tests__/<Name>.test.ts (DOM tests) OR tests/app/components/<Name>.test.ts (logic-only) |
app/utils/<name>.ts |
tests/app/utils/<name>.test.ts |
services/<name>.ts |
tests/services/<name>.test.ts |
utils/<name>.ts |
tests/utils/<name>.test.ts |
terraform/lambda/src/<Name>/index.ts |
terraform/lambda/src/<Name>/__tests__/index.test.ts |
Use __tests__/ co-location only for component DOM tests and Lambda handlers. Everything else goes in the top-level tests/ mirror tree.
Environment Selection
- Default:
node(composables, services, utils, Lambda handlers) - DOM tests only: Add
/** @vitest-environment happy-dom */as the first line of the test file - Never change
vitest.config.tsenvironment globally
Test Writing Rules
- Imports — always explicit:
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' - Nuxt auto-imports — call
mockNuxtImports()fromtests/helpersinbeforeEachfor any code usinguseRuntimeConfig,useState,computed,ref,watch,onMounted,onUnmounted,navigateTo - Naming — always
*.test.ts, never*.spec.ts - Timers — use
vi.useFakeTimers()inbeforeEachandvi.useRealTimers()inafterEachfor async/timer tests - Console noise — suppress with
vi.spyOn(console, 'log').mockImplementation(() => {})inbeforeEach - Logic extraction — prefer testing extracted logic over DOM rendering; test behavior, not templates
- Component DOM tests — use
@vue/test-utilsmount()with stubbed Nuxt UI components - AWS service mocks — use
mockSend+vi.mock('@aws-sdk/...')pattern (seereferences/testing-patterns.md)
What to Test (per code type)
- Composables: return values, state mutations, reactive updates, edge cases
- Components: props, emits, slot rendering, conditional rendering (via logic extraction where possible)
- Services: success path, error handling (custom error classes), edge cases (empty input, missing fields)
- Utils: pure function I/O, boundary conditions
- Lambda handlers: each resolver action, valid/invalid input, DynamoDB mock responses, error paths
Running Tests
bun run test # Single run
bun run test:watch # Watch mode
bun run test:coverage # With v8 coverage report
Coverage
Coverage uses @vitest/coverage-v8. Config is in vitest.config.ts. Thresholds: lines 80%, functions 80%, branches 70%, statements 80%.
Reference
Read references/testing-patterns.md for detailed mock patterns, full templates, and real codebase examples.
More from ralphcrisostomo/nuxt-development-skills
ralph
Convert PRDs to prd.json format for the Ralph autonomous agent system. Use when you have an existing PRD and need to convert it to Ralph's JSON format. Triggers on: convert this prd, turn this into ralph format, create prd.json from this, ralph json.
52prd
Generate a Product Requirements Document (PRD) for a new feature. Use when planning a feature, starting a new project, or when asked to create a PRD. Triggers on: create a prd, write prd for, plan this feature, requirements for, spec out.
39optimise-claude
Use when auditing, trimming, or restructuring AI instruction files (CLAUDE.md, SKILL.md, AGENTS.md) to reduce context-window consumption. Trigger whenever CLAUDE.md is bloated or Claude ignores instructions, a SKILL.md exceeds 120 lines, skills share duplicated content, AGENTS.md has large inline blocks, or the user asks to optimize, slim down, or reduce token usage.
37nuxt-init
Use when scaffolding a new Nuxt 4 project with standard config files (prettier, eslint, gitignore, husky, vitest, tsconfig, sops) and bun scripts.
33nuxt-terraform
Scaffold Nuxt + AWS Terraform infrastructure. Use when adding GraphQL resolvers, Lambda functions, initializing a new project with AppSync, DynamoDB, Cognito, writing Terraform tests, or generating/reviewing Terraform code style. Triggers on: add graphql resolver, create lambda, scaffold terraform, init terraform, add appsync resolver, add mutation, add query, add terraform test, write tftest, terraform style.
32todo
Use when scanning a codebase for incomplete work and maintaining a living TODO.md grouped by feature. Triggers on: scan for todos, find incomplete work, update todo, what needs doing, create todo list.
30