vitest/no-hooks
@vitest/eslint-plugin
No autofix
eslint.config.js
import vitest from "@vitest/eslint-plugin";
export default [
{
plugins: { vitest },
rules: {
"vitest/no-hooks": "error",
},
},
];function setupFoo(options) {
/* ... */
}
function setupBar(options) {
/* ... */
}
describe('foo', () => {
let foo
beforeEach(() => {
foo = setupFoo()
})
afterEach(() => {
foo = null
})
it('does something', () => {
expect(foo.doesSomething()).toBe(true)
})
describe('with bar', () => {
let bar
beforeEach(() => {
bar = setupBar()
})
afterEach(() => {
bar = null
// …function setupFoo(options) {
/* ... */
}
function setupBar(options) {
/* ... */
}
describe('foo', () => {
it('does something', () => {
const foo = setupFoo()
expect(foo.doesSomething()).toBe(true)
})
it('does something with bar', () => {
const foo = setupFoo()
const bar = setupBar()
expect(foo.doesSomething(bar)).toBe(true)
})
})Examples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
vitest/no-hooks
@vitest/eslint-plugin
import vitest from "@vitest/eslint-plugin";
export default [
{
plugins: { vitest },
rules: {
"vitest/no-hooks": "error",
},
},
];vitest/no-hooks
vitest plugin · style
{
"plugins": [
"vitest"
],
"rules": {
"vitest/no-hooks": "error"
}
}nursery/noTestHooks
nursery · since 2.6.9
{
"linter": {
"rules": {
"nursery": {
"noTestHooks": "error"
}
}
}
}Disallow duplicate hooks and teardown hooks
Disallow return statements in tests
Require describe block to be less than set max value or default value
Enforce using toBeCalledTimes(1) or toHaveBeenCalledTimes(1)
Enforce using toBeCalledWith() or toHaveBeenCalledWith()