source: frontend/node_modules/eslint-plugin-jest/docs/rules/prefer-expect-assertions.md

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

Fix frontend appearance

  • Property mode set to 100644
File size: 4.8 KB
Line 
1# Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)
2
3Ensure every test to have either `expect.assertions(<number of assertions>)` OR
4`expect.hasAssertions()` as its first expression.
5
6## Rule details
7
8This rule triggers a warning if,
9
10- `expect.assertions(<number of assertions>)` OR `expect.hasAssertions()` is not
11 present as first statement in a test, e.g.:
12
13```js
14test('my test', () => {
15 expect(someThing()).toEqual('foo');
16});
17```
18
19- `expect.assertions(<number of assertions>)` is the first statement in a test
20 where argument passed to `expect.assertions(<number of assertions>)` is not a
21 valid number, e.g.:
22
23```js
24test('my test', () => {
25 expect.assertions('1');
26 expect(someThing()).toEqual('foo');
27});
28```
29
30### Default configuration
31
32The following patterns are considered warnings:
33
34```js
35test('my test', () => {
36 expect.assertions('1');
37 expect(someThing()).toEqual('foo');
38});
39
40test('my test', () => {
41 expect(someThing()).toEqual('foo');
42});
43```
44
45The following patterns would not be considered warnings:
46
47```js
48test('my test', () => {
49 expect.assertions(1);
50 expect(someThing()).toEqual('foo');
51});
52
53test('my test', () => {
54 expect.hasAssertions();
55 expect(someThing()).toEqual('foo');
56});
57```
58
59## Options
60
61This rule can be configured to only check tests that match certain patterns that
62typically look like `expect` calls might be missed, such as in promises or
63loops.
64
65By default, none of these options are enabled meaning the rule checks _every_
66test for a call to either `expect.hasAssertions` or `expect.assertions`. If any
67of the options are enabled the rule checks any test that matches _at least one_
68of the patterns represented by the enabled options (think "OR" rather than
69"AND").
70
71#### `onlyFunctionsWithAsyncKeyword`
72
73When `true`, this rule will only warn for tests that use the `async` keyword.
74
75```json
76{
77 "rules": {
78 "jest/prefer-expect-assertions": [
79 "warn",
80 { "onlyFunctionsWithAsyncKeyword": true }
81 ]
82 }
83}
84```
85
86When `onlyFunctionsWithAsyncKeyword` option is set to `true`, the following
87pattern would be a warning:
88
89```js
90test('my test', async () => {
91 const result = await someAsyncFunc();
92 expect(result).toBe('foo');
93});
94```
95
96While the following patterns would not be considered warnings:
97
98```js
99test('my test', () => {
100 const result = someFunction();
101 expect(result).toBe('foo');
102});
103
104test('my test', async () => {
105 expect.assertions(1);
106 const result = await someAsyncFunc();
107 expect(result).toBe('foo');
108});
109```
110
111#### `onlyFunctionsWithExpectInLoop`
112
113When `true`, this rule will only warn for tests that have `expect` calls within
114a native loop.
115
116```json
117{
118 "rules": {
119 "jest/prefer-expect-assertions": [
120 "warn",
121 { "onlyFunctionsWithAsyncKeyword": true }
122 ]
123 }
124}
125```
126
127Examples of **incorrect** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
128
129```js
130describe('getNumbers', () => {
131 it('only returns numbers that are greater than zero', () => {
132 const numbers = getNumbers();
133
134 for (const number in numbers) {
135 expect(number).toBeGreaterThan(0);
136 }
137 });
138});
139```
140
141Examples of **correct** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
142
143```js
144describe('getNumbers', () => {
145 it('only returns numbers that are greater than zero', () => {
146 expect.hasAssertions();
147
148 const numbers = getNumbers();
149
150 for (const number in numbers) {
151 expect(number).toBeGreaterThan(0);
152 }
153 });
154
155 it('returns more than one number', () => {
156 expect(getNumbers().length).toBeGreaterThan(1);
157 });
158});
159```
160
161#### `onlyFunctionsWithExpectInCallback`
162
163When `true`, this rule will only warn for tests that have `expect` calls within
164a callback.
165
166```json
167{
168 "rules": {
169 "jest/prefer-expect-assertions": [
170 "warn",
171 { "onlyFunctionsWithExpectInCallback": true }
172 ]
173 }
174}
175```
176
177Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is
178`true`:
179
180```js
181describe('getNumbers', () => {
182 it('only returns numbers that are greater than zero', () => {
183 const numbers = getNumbers();
184
185 getNumbers().forEach(number => {
186 expect(number).toBeGreaterThan(0);
187 });
188 });
189});
190
191describe('/users', () => {
192 it.each([1, 2, 3])('returns ok', id => {
193 client.get(`/users/${id}`, response => {
194 expect(response.status).toBe(200);
195 });
196 });
197});
198```
199
200Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is
201`true`:
202
203```js
204describe('getNumbers', () => {
205 it('only returns numbers that are greater than zero', () => {
206 expect.hasAssertions();
207
208 const numbers = getNumbers();
209
210 getNumbers().forEach(number => {
211 expect(number).toBeGreaterThan(0);
212 });
213 });
214});
215
216describe('/users', () => {
217 it.each([1, 2, 3])('returns ok', id => {
218 expect.assertions(3);
219
220 client.get(`/users/${id}`, response => {
221 expect(response.status).toBe(200);
222 });
223 });
224});
225```
Note: See TracBrowser for help on using the repository browser.