Sub topic 10
Focusing, Skipping, and Filtering Tests
This chapter assumes you can write tests with describe, test, and it, and run them with npx vitest (watch mode) or npx vitest run (single run). Once a suite grows, you often want to run only part of it, usually while debugging one failure. Vitest gives you two ways to do that. Modifiers in the code change which tests run. Filters on the command line choose files and test names without changing any code.
Modifiers in the Test File
test, it, and describe all accept these modifiers:
| Modifier | Effect |
|---|---|
| `.only` | Runs only the marked tests or groups in this file |
| `.skip` | Does not run the test, and reports it as skipped |
| `.todo` | Records a planned test that has no body yet |
| `.skipIf(cond)` | Skips when `cond` is truthy |
| `.runIf(cond)` | Runs only when `cond` is truthy |
// cart.test.js
import { describe, it, expect } from 'vitest'
function total(items) {
return items.reduce((sum, item) => sum + item.price, 0)
}
describe('total', () => {
it.only('sums item prices', () => {
expect(total([{ price: 2 }, { price: 3 }])).toBe(5)
})
it('returns 0 for an empty cart', () => {
expect(total([])).toBe(0)
})
it.skip('applies a discount code', () => {
// waiting on the discount feature
})
it.todo('rounds to two decimal places')
it.skipIf(process.platform === 'win32')('uses POSIX paths', () => {
expect('/tmp/cart'.startsWith('/')).toBe(true)
})
})Here only sums item prices runs. Every other test in the file shows up as skipped or todo. .only applies within a single file. Other test files still run unless you filter them out from the command line.
Filters on the Command Line
Filters leave your code untouched, so you have nothing to clean up afterwards.
# Run only files whose path contains "cart"
npx vitest run cart
# Run only tests whose full name matches a pattern
npx vitest run -t "empty cart"
# Combine both: files matching "cart", tests matching "sums"
npx vitest run cart -t "sums"Any positional argument is matched as a substring against file paths. -t (long form --testNamePattern) is matched against the full test name, which includes the names of the describe blocks around it. For example, -t "total returns" matches the second test above.
In watch mode you can also filter from the keyboard. Press p to filter by file name, t to filter by test name, and a to run all tests again.
Common Mistakes
- Committing
.onlyor.skip. A skipped test can't catch a regression. Track unfinished work with.todo, which shows up in every report. - Expecting
.onlyto work across files. Add a file filter, such asnpx vitest run cart, to narrow the run to one file. - Forgetting
describenames in-tpatterns. The pattern is matched against the full name, so a pattern that spans two levels needs both names.
Exercise
You want to run only the test named "rejects for an invalid id" inside api.test.js, without editing any file. What command do you use?
Show answer
npx vitest run api -t "rejects for an invalid id". The positional argument "api" limits the run to matching files, and -t limits it to tests whose full name matches the pattern.
Exercise
A file contains one it.only test and one it.skip test. A teammate runs the suite on a CI server where CI=true. What happens, and why?
Show answer
The run fails. When the CI environment variable is set, allowOnly defaults to false, so Vitest reports the .only as an error. This stops a focused test from silently hiding the rest of the suite.