source: frontend/node_modules/eslint-plugin-jest/docs/rules/no-hooks.md

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[9af201e]1# Disallow setup and teardown hooks (`no-hooks`)
2
3Jest provides global functions for setup and teardown tasks, which are called
4before/after each test case and each test suite. The use of these hooks promotes
5shared state between tests.
6
7## Rule Details
8
9This rule reports for the following function calls:
10
11- `beforeAll`
12- `beforeEach`
13- `afterAll`
14- `afterEach`
15
16Examples of **incorrect** code for this rule:
17
18```js
19/* eslint jest/no-hooks: "error" */
20
21function setupFoo(options) {
22 /* ... */
23}
24
25function setupBar(options) {
26 /* ... */
27}
28
29describe('foo', () => {
30 let foo;
31
32 beforeEach(() => {
33 foo = setupFoo();
34 });
35
36 afterEach(() => {
37 foo = null;
38 });
39
40 it('does something', () => {
41 expect(foo.doesSomething()).toBe(true);
42 });
43
44 describe('with bar', () => {
45 let bar;
46
47 beforeEach(() => {
48 bar = setupBar();
49 });
50
51 afterEach(() => {
52 bar = null;
53 });
54
55 it('does something with bar', () => {
56 expect(foo.doesSomething(bar)).toBe(true);
57 });
58 });
59});
60```
61
62Examples of **correct** code for this rule:
63
64```js
65/* eslint jest/no-hooks: "error" */
66
67function setupFoo(options) {
68 /* ... */
69}
70
71function setupBar(options) {
72 /* ... */
73}
74
75describe('foo', () => {
76 it('does something', () => {
77 const foo = setupFoo();
78 expect(foo.doesSomething()).toBe(true);
79 });
80
81 it('does something with bar', () => {
82 const foo = setupFoo();
83 const bar = setupBar();
84 expect(foo.doesSomething(bar)).toBe(true);
85 });
86});
87```
88
89## Options
90
91```json
92{
93 "jest/no-hooks": [
94 "error",
95 {
96 "allow": ["afterEach", "afterAll"]
97 }
98 ]
99}
100```
101
102### `allow`
103
104This array option controls which Jest hooks are checked by this rule. There are
105four possible values:
106
107- `"beforeAll"`
108- `"beforeEach"`
109- `"afterAll"`
110- `"afterEach"`
111
112By default, none of these options are enabled (the equivalent of
113`{ "allow": [] }`).
114
115Examples of **incorrect** code for the `{ "allow": ["afterEach"] }` option:
116
117```js
118/* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
119
120function setupFoo(options) {
121 /* ... */
122}
123
124let foo;
125
126beforeEach(() => {
127 foo = setupFoo();
128});
129
130afterEach(() => {
131 jest.resetModules();
132});
133
134test('foo does this', () => {
135 // ...
136});
137
138test('foo does that', () => {
139 // ...
140});
141```
142
143Examples of **correct** code for the `{ "allow": ["afterEach"] }` option:
144
145```js
146/* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
147
148function setupFoo(options) {
149 /* ... */
150}
151
152afterEach(() => {
153 jest.resetModules();
154});
155
156test('foo does this', () => {
157 const foo = setupFoo();
158 // ...
159});
160
161test('foo does that', () => {
162 const foo = setupFoo();
163 // ...
164});
165```
166
167## When Not To Use It
168
169If you prefer using the setup and teardown hooks provided by Jest, you can
170safely disable this rule.
171
172## Further Reading
173
174- [Jest docs - Setup and Teardown](https://facebook.github.io/jest/docs/en/setup-teardown.html)
Note: See TracBrowser for help on using the repository browser.