source: frontend/node_modules/eslint-plugin-jest/docs/rules/prefer-hooks-on-top.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.9 KB
Line 
1# Suggest having hooks before any test cases (`prefer-hooks-on-top`)
2
3While hooks can be setup anywhere in a test file, they are always called in a
4specific order which means it can be confusing if they're intermixed with test
5cases.
6
7This rule helps to ensure that hooks are always defined before test cases.
8
9## Rule Details
10
11Examples of **incorrect** code for this rule
12
13```js
14/* eslint jest/prefer-hooks-on-top: "error" */
15
16describe('foo', () => {
17 beforeEach(() => {
18 seedMyDatabase();
19 });
20
21 it('accepts this input', () => {
22 // ...
23 });
24
25 beforeAll(() => {
26 createMyDatabase();
27 });
28
29 it('returns that value', () => {
30 // ...
31 });
32
33 describe('when the database has specific values', () => {
34 const specificValue = '...';
35
36 beforeEach(() => {
37 seedMyDatabase(specificValue);
38 });
39
40 it('accepts that input', () => {
41 // ...
42 });
43
44 it('throws an error', () => {
45 // ...
46 });
47
48 afterEach(() => {
49 clearLogger();
50 });
51 beforeEach(() => {
52 mockLogger();
53 });
54
55 it('logs a message', () => {
56 // ...
57 });
58 });
59
60 afterAll(() => {
61 removeMyDatabase();
62 });
63});
64```
65
66Examples of **correct** code for this rule
67
68```js
69/* eslint jest/prefer-hooks-on-top: "error" */
70
71describe('foo', () => {
72 beforeAll(() => {
73 createMyDatabase();
74 });
75
76 beforeEach(() => {
77 seedMyDatabase();
78 });
79
80 afterAll(() => {
81 clearMyDatabase();
82 });
83
84 it('accepts this input', () => {
85 // ...
86 });
87
88 it('returns that value', () => {
89 // ...
90 });
91
92 describe('when the database has specific values', () => {
93 const specificValue = '...';
94
95 beforeEach(() => {
96 seedMyDatabase(specificValue);
97 });
98
99 beforeEach(() => {
100 mockLogger();
101 });
102
103 afterEach(() => {
104 clearLogger();
105 });
106
107 it('accepts that input', () => {
108 // ...
109 });
110
111 it('throws an error', () => {
112 // ...
113 });
114
115 it('logs a message', () => {
116 // ...
117 });
118 });
119});
120```
Note: See TracBrowser for help on using the repository browser.