source: frontend/node_modules/eslint-plugin-jest/docs/rules/no-standalone-expect.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: 2.1 KB
Line 
1# Disallow using `expect` outside of `it` or `test` blocks (`no-standalone-expect`)
2
3Prevents `expect` statements outside of a `test` or `it` block. An `expect`
4within a helper function (but outside of a `test` or `it` block) will not
5trigger this rule.
6
7## Rule Details
8
9This rule aims to eliminate `expect` statements that will not be executed. An
10`expect` inside of a `describe` block but outside of a `test` or `it` block or
11outside a `describe` will not execute and therefore will trigger this rule. It
12is viable, however, to have an `expect` in a helper function that is called from
13within a `test` or `it` block so `expect` statements in a function will not
14trigger this rule.
15
16Statements like `expect.hasAssertions()` will NOT trigger this rule since these
17calls will execute if they are not in a test block.
18
19Examples of **incorrect** code for this rule:
20
21```js
22// in describe
23describe('a test', () => {
24 expect(1).toBe(1);
25});
26
27// below other tests
28describe('a test', () => {
29 it('an it', () => {
30 expect(1).toBe(1);
31 });
32
33 expect(1).toBe(1);
34});
35```
36
37Examples of **correct** code for this rule:
38
39```js
40// in it block
41describe('a test', () => {
42 it('an it', () => {
43 expect(1).toBe(1);
44 });
45});
46
47// in helper function
48describe('a test', () => {
49 const helper = () => {
50 expect(1).toBe(1);
51 };
52
53 it('an it', () => {
54 helper();
55 });
56});
57
58describe('a test', () => {
59 expect.hasAssertions(1);
60});
61```
62
63\*Note that this rule will not trigger if the helper function is never used even
64thought the `expect` will not execute. Rely on a rule like no-unused-vars for
65this case.
66
67### Options
68
69#### `additionalTestBlockFunctions`
70
71This array can be used to specify the names of functions that should also be
72treated as test blocks:
73
74```json
75{
76 "rules": {
77 "jest/no-standalone-expect": [
78 "error",
79 { "additionalTestBlockFunctions": ["each.test"] }
80 ]
81 }
82}
83```
84
85The following is _correct_ when using the above configuration:
86
87```js
88each([
89 [1, 1, 2],
90 [1, 2, 3],
91 [2, 1, 3],
92]).test('returns the result of adding %d to %d', (a, b, expected) => {
93 expect(a + b).toBe(expected);
94});
95```
96
97## When Not To Use It
98
99Don't use this rule on non-jest test files.
Note: See TracBrowser for help on using the repository browser.