Sub topic 2
Organizing Tests with describe, test, and it
This chapter assumes you have Vitest installed and can run npx vitest from the previous chapter. It also assumes you know ES module syntax and arrow functions. Here you will learn how Vitest finds test files and how to group tests so the output reads clearly.
How Vitest finds test files
By default, Vitest collects any file whose name matches *.test.js or *.spec.js. The same rule covers .ts, .jsx, .tsx, .mjs, and .cjs. Vitest skips node_modules. The usual convention is to put each test file next to the module it tests, such as cart.js and cart.test.js.
test and it
test(name, fn) registers one test case. The name describes the expected behavior, and the function holds the code and assertions. it is an alias for test with identical behavior. Teams usually choose it when the name reads as a sentence, as in "it adds an item". Pick one per project and use it consistently.
A test passes when its function finishes without throwing. A test with no assertions therefore passes too, which can hide mistakes.
Grouping with describe
describe(name, fn) creates a suite, which is a named group of tests. You can nest suites. Vitest joins the names of a test's enclosing suites with the test name when it reports results.
// cart.js
export function createCart() {
const items = []
return {
add(item) {
items.push(item)
},
count() {
return items.length
},
total() {
return items.reduce((sum, item) => sum + item.price, 0)
},
}
}
// cart.test.js
import { describe, it, expect } from 'vitest'
import { createCart } from './cart.js'
describe('cart', () => {
describe('count', () => {
it('starts at zero', () => {
expect(createCart().count()).toBe(0)
})
it('increases when an item is added', () => {
const cart = createCart()
cart.add({ name: 'pen', price: 2 })
expect(cart.count()).toBe(1)
})
})
describe('total', () => {
it('sums item prices', () => {
const cart = createCart()
cart.add({ name: 'pen', price: 2 })
cart.add({ name: 'book', price: 10 })
expect(cart.total()).toBe(12)
})
})
})To see the whole tree of names, run npx vitest run --reporter=verbose.
Collection runs before execution
Vitest runs each test file in two phases. In the collection phase, it calls every describe callback right away to register the tests inside. In the execution phase, it runs the registered test functions. Code written directly in a describe body therefore runs once, before any test starts.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| File named `cart.tests.js` | "No test files found" | Use `.test.js` or `.spec.js` |
| `expect` placed in a describe body | Error or confusing output during collection | Move the assertion into a `test`/`it` |
| Test with no assertion | Passes when it should not | Add an `expect`, or call `expect.assertions(n)` to require a set number |
| Vague names like "works" | Failure output does not say what broke | Name the behavior: "sums item prices" |
Exercise
A file contains describe('parser') with a nested describe('dates') holding it('parses ISO strings'). The describe('parser') body also contains console.log('setup'). How is the test named in the report, and when does 'setup' print relative to the test running?
Show answer
The report names it "parser > dates > parses ISO strings". The 'setup' message prints once, during the collection phase, before any test in the file runs.
Exercise
Why might moving const cart = createCart() from inside each test into the outer describe body make the "starts at zero" test fail?
Show answer
The describe body runs once, so every test shares the same cart. If a test that adds items runs before "starts at zero", count() returns a number other than 0.