Sub topic 3
Focusing, Skipping, and Filtering Tests
This chapter assumes you can write tests with describe, it, expect, and vi.fn, and that you know how to use lifecycle hooks.
Focusing and Skipping
.only runs only the marked tests in a file. .skip turns a test off without deleting it. .todo adds a placeholder for a test you plan to write.
import { test, expect } from 'vitest'
test.only('runs alone while you debug', () => {
expect(1 + 1).toBe(2)
})
test.skip('temporarily disabled', () => {
expect(true).toBe(false)
})
test.todo('handles negative totals')The command line can filter too. npx vitest math runs only files whose path contains "math". npx vitest -t "divide" runs only tests whose names match "divide".
Controlling Time with Fake Timers
Fake timers replace setTimeout and similar functions. Your test then moves time forward itself instead of waiting.
import { it, expect, vi, beforeEach, afterEach } from 'vitest'
function delayedGreeting(callback) {
setTimeout(() => callback('hello'), 1000)
}
beforeEach(() => vi.useFakeTimers())
afterEach(() => vi.useRealTimers())
it('calls back after one second', () => {
const callback = vi.fn()
delayedGreeting(callback)
expect(callback).not.toHaveBeenCalled()
vi.advanceTimersByTime(1000)
expect(callback).toHaveBeenCalledWith('hello')
})Snapshot Assertions
toMatchInlineSnapshot() writes the serialized value into your test file on the first run. Later runs compare the output against that saved value. If the output changes on purpose, update the snapshot by pressing u in watch mode or running vitest -u. Review snapshot diffs before you accept them, because accepting one without reading it hides real bugs.
Configuring Vitest and Choosing an Environment
// vitest.config.js
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'jsdom',
coverage: { provider: 'v8', reporter: ['text', 'html'] },
},
})| Environment | Use it for | Package needed |
|---|---|---|
| `node` (default) | Logic with no DOM | none |
| `jsdom` | DOM code, broad API coverage | `jsdom` |
| `happy-dom` | DOM code, faster but less complete | `happy-dom` |
To change the environment for a single file, put // @vitest-environment jsdom at the top of that file.
Measuring Coverage
Install @vitest/coverage-v8, then run npx vitest run --coverage. The text reporter prints a table in the terminal, and the html reporter writes a browsable report to coverage/. Coverage tells you which lines ran during the tests. It does not tell you whether your assertions checked anything useful.
Writing Maintainable Tests
- Isolate tests: restore mocks and timers in
afterEach, and never share mutable state between tests. - Make tests deterministic: fake time, randomness, and network calls.
- Keep tests readable: test one behavior per test and give each test a name that describes that behavior.
Exercise
A test passes locally but fails in CI only when it runs after another file that uses vi.useFakeTimers(). What is the likely cause and fix?
Show answer
The other file never restores real timers, so its fake timers leak into later tests. Call vi.useRealTimers() in an afterEach hook in that file.