TypeScript testing patterns with Vitest and MSW. Use when writing unit tests, mocking APIs, creating typed mocks for dependency injection, or using snapshot testing.
Ships in typescript. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Inspect the owning package and existing implementation first. Reuse established project types, helpers, errors, lifecycle behavior, and test utilities. The patterns below are options, not an implementation checklist. Introduce one only when the current task requires it.
Comprehensive testing patterns using Vitest for unit testing, MSW for API mocking, and snapshot testing for complex object validation.
# Core testing dependencies
bun add -d vitest @vitest/ui
# MSW for API mocking
bun add -d msw
# Optional: coverage reporting
bun add -d @vitest/coverage-v8
import { describe, it, expect, vi, beforeEach } from 'vitest'
describe('UserService', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('creates user with valid data', async () => {
const result = await userService.create({ name: 'Alice' })
expect(result).toMatchObject({ name: 'Alice' })
})
})
Create type-safe mocks for dependency injection using vi.mocked():
import { vi } from 'vitest'
import type { UserRepository } from './user-repository'
// Mock the entire module
vi.mock('./user-repository')
// Get typed mock instance
const mockUserRepo = vi.mocked<UserRepository>({
findById: vi.fn(),
save: vi.fn(),
delete: vi.fn(),
})
// Type-safe mock return values
mockUserRepo.findById.mockResolvedValue({
id: '123',
name: 'Alice',
email: '[email protected]',
})
// Assertions with full type safety
expect(mockUserRepo.findById).toHaveBeenCalledWith('123')
// Single return value
mockRepo.findById.mockResolvedValue(user)
// Multiple calls, different returns
mockRepo.findById
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(user)
// Conditional logic
mockRepo.findById.mockImplementation(async (id) => {
if (id === '123') return user
return null
})
// Throw errors
mockRepo.save.mockRejectedValue(new Error('Database error'))
import { vi } from 'vitest'
import { emailService } from './email-service'
// Spy on method without replacing it
vi.spyOn(emailService, 'send')
// Call real implementation
await emailService.send({ to: '[email protected]', subject: 'Test' })
// Assert it was called
expect(emailService.send).toHaveBeenCalledOnce()
// Temporarily override return value
emailService.send.mockResolvedValueOnce({ messageId: 'test-123' })
See references/mocking.md for comprehensive mocking patterns including module mocks, class mocks, stateful handlers, and dependency injection.
Mock HTTP requests at the network level for integration tests:
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { afterAll, afterEach, beforeAll } from 'vitest'
// Define handlers
const handlers = [
http.get('/api/users/:id', ({ params }) => {
return HttpResponse.json({
id: params.id,
name: 'Alice',
email: '[email protected]',
})
}),
http.post('/api/users', async ({ request }) => {
const body = await request.json()
return HttpResponse.json({ id: '123', ...body }, { status: 201 })
}),
]
// Setup server
const server = setupServer(...handlers)
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
See references/msw.md for advanced MSW patterns including error simulation, delays, and per-test overrides.
Validate complex objects and output without manual assertions:
import { it, expect } from 'vitest'
it('generates correct user profile', () => {
const profile = generateUserProfile(user)
// Snapshot entire object
expect(profile).toMatchSnapshot()
})
// Handle dynamic values (dates, IDs)
it('creates order with timestamp', () => {
const order = createOrder(items)
expect(order).toMatchSnapshot({
id: expect.any(String),
createdAt: expect.any(Date),
})
})
// Inline snapshots for small objects
it('formats error message', () => {
const error = formatError(new Error('Failed'))
expect(error).toMatchInlineSnapshot(`
{
"message": "Failed",
"code": "UNKNOWN_ERROR",
}
`)
})
See references/snapshot-testing.md for snapshot maintenance, custom serializers, and best practices.
// Promises
it('loads user data', async () => {
const user = await userService.findById('123')
expect(user).toBeDefined()
})
// Callbacks
it('calls callback on completion', () => {
return new Promise<void>((resolve) => {
processData(data, (result) => {
expect(result).toBe(expected)
resolve()
})
})
})
// Timers
it('retries after delay', async () => {
vi.useFakeTimers()
const promise = retryOperation()
vi.advanceTimersByTime(1000)
const result = await promise
expect(result).toBe('success')
vi.useRealTimers()
})
// Group related tests
describe('UserService', () => {
describe('create', () => {
it('succeeds with valid data', () => {})
it('throws on duplicate email', () => {})
})
describe('update', () => {
it('updates existing user', () => {})
it('throws on not found', () => {})
})
})
// Shared setup
describe('authenticated requests', () => {
beforeEach(() => {
mockAuth.isAuthenticated.mockReturnValue(true)
})
it('allows user creation', () => {})
it('allows user deletion', () => {})
})
vi.mocked<T>() provides type safety for mock setup and assertionsexpect.any()beforeEach/afterEach to ensure test isolationasync/await for cleaner async test codevi.useFakeTimers() to control time in testsexpect().rejects.toThrow()class UserService {
constructor(
private repo: UserRepository,
private email: EmailService,
) {}
async create(data: CreateUserInput) {
const user = await this.repo.save(data)
await this.email.sendWelcome(user.email)
return user
}
}
// Test with mocks
it('sends welcome email on create', async () => {
const mockRepo = vi.mocked<UserRepository>({
save: vi.fn().mockResolvedValue(savedUser),
})
const mockEmail = vi.mocked<EmailService>({
sendWelcome: vi.fn().mockResolvedValue(undefined),
})
const service = new UserService(mockRepo, mockEmail)
await service.create(userData)
expect(mockEmail.sendWelcome).toHaveBeenCalledWith('[email protected]')
})
it('handles repository errors gracefully', async () => {
mockRepo.save.mockRejectedValue(new Error('Database error'))
await expect(
service.create(userData)
).rejects.toThrow('Failed to create user')
// Verify cleanup or rollback occurred
expect(mockEmail.sendWelcome).not.toHaveBeenCalled()
})
it('handles concurrent requests correctly', async () => {
const promises = [
service.processOrder(order1),
service.processOrder(order2),
service.processOrder(order3),
]
const results = await Promise.all(promises)
expect(results).toHaveLength(3)
expect(new Set(results.map(r => r.id)).size).toBe(3)
})
# Run single test file
vitest run path/to/test.spec.ts
# Run tests matching pattern
vitest run -t "creates user"
# Watch mode for TDD
vitest watch
# UI mode for debugging
vitest --ui
# Coverage report
vitest run --coverage
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
No slash command · from typescript
npx claudepluginhub martinffx/typescript-skills --plugin typescript8 skills in this plugin
/typescript-api-designREST API design patterns. Use when designing endpoints, error responses, pagination, versioning, or API structure. Framework-agnostic principles for building consistent, maintainable APIs./typescript-build-toolsTypeScript project tooling with Bun or pnpm, TypeScript 7, Vitest, Biome or Oxc (Oxlint and Oxfmt), and Turborepo. Use when setting up package.json scripts, running builds, typechecking, configuring tests, linting, formatting, or orchestrating monorepo development./typescript-drizzle-ormType-safe SQL with Drizzle ORM in TypeScript. Use when defining database schemas, writing queries, setting up relations, running migrations, working with PostgreSQL/MySQL/SQLite/Cloudflare D1/Durable Objects data layers, or integrating drizzle-orm/effect-postgres with @effect/sql-pg and Effect Layers./typescript-dynamodb-toolboxDynamoDB single-table design using dynamodb-toolbox v2. Use when creating entities, defining key patterns, designing GSIs, writing queries, implementing pagination, or working with any DynamoDB data layer in TypeScript projects./typescript-effect-tsBuild, review, or migrate TypeScript applications using Effect v3 or v4. Use whenever code imports from effect or @effect/*, integrates drizzle-orm/effect-postgres with @effect/sql-pg, or works with Effect.gen, typed errors, Result or Either, Context services, Layer dependency injection, Schema validation, fibers, concurrency, Scope, or acquireRelease. Resolve the installed Effect major before suggesting APIs so v3 and v4 syntax are never mixed./typescript-fastifyBuilding REST APIs with Fastify in TypeScript. Use when creating routes, handling requests, implementing validation with TypeBox, structuring applications, or working with HTTP handlers and plugins.