all
config.
It's common when writing tests to need to perform a particular setup work before and after a test suite run. Because Vitest executes all describe
handlers in a test file before it executes any of the actual tests, it's important to ensure setup and teardown work is done inside before*
and after*
handlers respectively, rather than inside the describe
blocks.
This rule flags any expression that is either at the toplevel of a test file or directly within the body of a describe
except the following:
import
statementsconst
variableslet
declarations and initializations tonull
orundefined
- Classes
- Types
This rule flags any function within in a describe
block and suggest wrapping them in one of the four lifecycle hooks.
The following patterns are considered warnings:
import { database } from './api'
describe('foo', () => {
database.connect()
test('bar', () => {
// ...
})
database.disconnect()
})
The following patterns are not warnings:
describe('foo', () => {
before(() => {
database.connect()
})
test('bar', () => {
// ...
})
})
If there are methods that you want to call outside of hooks and tests, you can mark them as allowed using the allowedFunctionCalls
option.
{
"vitest/require-hook": ["error", {
"allowedFunctionCalls": ["database.connect"]
}]
}
The following patterns are not warnings because database.connect
is allowed:
import { database } from './api'
describe('foo', () => {
database.connect()
test('bar', () => {
// ...
})
database.disconnect()
})