github-pages-deploy-action/node_modules/eslint-plugin-jest/docs/rules/prefer-hooks-on-top.md

97 lines
1.5 KiB
Markdown
Raw Normal View History

2020-09-13 06:19:45 +08:00
# Suggest having hooks before any test cases (`prefer-hooks-on-top`)
2020-03-07 11:45:40 +08:00
All hooks should be defined before the start of the tests
## Rule Details
Examples of **incorrect** code for this rule
```js
/* eslint jest/prefer-hooks-on-top: "error" */
2020-09-13 06:19:45 +08:00
describe('foo', () => {
2020-03-07 11:45:40 +08:00
beforeEach(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
beforeAll(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
});
// Nested describe scenario
2020-09-13 06:19:45 +08:00
describe('foo', () => {
2020-03-07 11:45:40 +08:00
beforeAll(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
2020-09-13 06:19:45 +08:00
describe('inner_foo', () => {
2020-03-07 11:45:40 +08:00
beforeEach(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('inner bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
2020-09-13 06:19:45 +08:00
test('inner bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
beforeAll(() => {
//some hook code
});
afterAll(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('inner bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
});
});
```
Examples of **correct** code for this rule
```js
/* eslint jest/prefer-hooks-on-top: "error" */
2020-09-13 06:19:45 +08:00
describe('foo', () => {
2020-03-07 11:45:40 +08:00
beforeEach(() => {
//some hook code
});
// Not affected by rule
someSetup();
afterEach(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
});
// Nested describe scenario
2020-09-13 06:19:45 +08:00
describe('foo', () => {
2020-03-07 11:45:40 +08:00
beforeEach(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
2020-09-13 06:19:45 +08:00
describe('inner_foo', () => {
2020-03-07 11:45:40 +08:00
beforeEach(() => {
//some hook code
});
2020-09-13 06:19:45 +08:00
test('inner bar', () => {
2020-03-07 11:45:40 +08:00
some_fn();
});
});
});
```