Write clear, maintainable Vitest and Playwright tests with precise assertions, consistent structure, and strong behavioral coverage.
You write tests that are clear, maintainable, and thorough. You optimize for readability and reliability. Tests should be easy to understand and cover both typical use cases and edge cases.
toBe, toEqual, and toStrictEqual over loose checks.toBeDefined, toBeTruthy, toHaveLength, or toMatchObject when you can assert the exact value instead.expect.arrayContaining or expect.objectContaining. Assert the full expected value.toStrictEqual when checking objects or arrays to catch extra or missing properties.toBe for primitives (strings, numbers, booleans).toBeUndefined only when the expected value is genuinely undefined.// ❌ BAD - vague, does not catch wrong values
expect(result).toBeDefined()
expect(items).toHaveLength(2)
expect(user).toMatchObject({ name: 'Alice' })
// ✅ GOOD - exact, catches regressions
expect(result).toBe('expected-value')
expect(items).toStrictEqual([{ id: 1 }, { id: 2 }])
expect(user).toStrictEqual({ name: 'Alice', role: 'admin' })
/src
/lib
custom-lib.ts
custom-lib.test.ts
import { describe, it, expect } from 'vitest'
import { generateSlug, doSomething } from './custom-lib'
describe('generateSlug', () => {
it('generates a slug from the title', () => {
const result = generateSlug('Hello World')
expect(result).toBe('hello-world')
})
it('handles empty input gracefully', () => {
const result = generateSlug('')
expect(result).toBe('')
})
})
describe('doSomething', () => {
it('does something really well', () => {
const result = doSomething('Hello World')
expect(result).toBe('hello-world')
})
})