jest/prefer-hooks-on-top
eslint-plugin-jest
No autofix
eslint.config.js
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-hooks-on-top": "error",
},
},
];describe('foo', () => {
beforeEach(() => {
seedMyDatabase();
});
it('accepts this input', () => {
// ...
});
beforeAll(() => {
createMyDatabase();
});
it('returns that value', () => {
// ...
});
describe('when the database has specific values', () => {
const specificValue = '...';
beforeEach(() => {
seedMyDatabase(specificValue);
});
it('accepts that input', () => {
// ...
});
it('throws an error', () => {
// ...
});
// …describe('foo', () => {
beforeAll(() => {
createMyDatabase();
});
beforeEach(() => {
seedMyDatabase();
});
afterAll(() => {
clearMyDatabase();
});
it('accepts this input', () => {
// ...
});
it('returns that value', () => {
// ...
});
describe('when the database has specific values', () => {
const specificValue = '...';
beforeEach(() => {
seedMyDatabase(specificValue);
});
beforeEach(() => {
mockLogger();
});
// …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.
jest/prefer-hooks-on-top
eslint-plugin-jest
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-hooks-on-top": "error",
},
},
];jest/prefer-hooks-on-top
jest plugin · style
{
"plugins": [
"jest"
],
"rules": {
"jest/prefer-hooks-on-top": "error"
}
}style/useTestHooksOnTop
style · since 2.4.14
{
"linter": {
"rules": {
"style": {
"useTestHooksOnTop": "error"
}
}
}
}Enforce having hooks before any test cases
Prefer having hooks in a consistent order
Suggest using the built-in equality matchers