@depup/eslint-plugin-jest 29.15.1-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/changes.json +10 -0
  4. package/docs/rules/consistent-test-it.md +96 -0
  5. package/docs/rules/expect-expect.md +152 -0
  6. package/docs/rules/max-expects.md +78 -0
  7. package/docs/rules/max-nested-describe.md +134 -0
  8. package/docs/rules/no-alias-methods.md +57 -0
  9. package/docs/rules/no-commented-out-tests.md +68 -0
  10. package/docs/rules/no-conditional-expect.md +142 -0
  11. package/docs/rules/no-conditional-in-test.md +123 -0
  12. package/docs/rules/no-confusing-set-timeout.md +64 -0
  13. package/docs/rules/no-deprecated-functions.md +59 -0
  14. package/docs/rules/no-disabled-tests.md +72 -0
  15. package/docs/rules/no-done-callback.md +100 -0
  16. package/docs/rules/no-duplicate-hooks.md +79 -0
  17. package/docs/rules/no-error-equal.md +37 -0
  18. package/docs/rules/no-export.md +52 -0
  19. package/docs/rules/no-focused-tests.md +67 -0
  20. package/docs/rules/no-hooks.md +178 -0
  21. package/docs/rules/no-identical-title.md +59 -0
  22. package/docs/rules/no-interpolation-in-snapshots.md +67 -0
  23. package/docs/rules/no-jasmine-globals.md +69 -0
  24. package/docs/rules/no-large-snapshots.md +180 -0
  25. package/docs/rules/no-mocks-import.md +34 -0
  26. package/docs/rules/no-restricted-jest-methods.md +53 -0
  27. package/docs/rules/no-restricted-matchers.md +64 -0
  28. package/docs/rules/no-standalone-expect.md +106 -0
  29. package/docs/rules/no-test-prefixes.md +42 -0
  30. package/docs/rules/no-test-return-statement.md +50 -0
  31. package/docs/rules/no-unnecessary-assertion.md +41 -0
  32. package/docs/rules/no-unneeded-async-expect-function.md +41 -0
  33. package/docs/rules/no-untyped-mock-factory.md +74 -0
  34. package/docs/rules/padding-around-after-all-blocks.md +34 -0
  35. package/docs/rules/padding-around-after-each-blocks.md +38 -0
  36. package/docs/rules/padding-around-all.md +20 -0
  37. package/docs/rules/padding-around-before-all-blocks.md +37 -0
  38. package/docs/rules/padding-around-before-each-blocks.md +38 -0
  39. package/docs/rules/padding-around-describe-blocks.md +42 -0
  40. package/docs/rules/padding-around-expect-groups.md +44 -0
  41. package/docs/rules/padding-around-test-blocks.md +48 -0
  42. package/docs/rules/prefer-called-with.md +39 -0
  43. package/docs/rules/prefer-comparison-matcher.md +62 -0
  44. package/docs/rules/prefer-each.md +58 -0
  45. package/docs/rules/prefer-ending-with-an-expect.md +170 -0
  46. package/docs/rules/prefer-equality-matcher.md +36 -0
  47. package/docs/rules/prefer-expect-assertions.md +230 -0
  48. package/docs/rules/prefer-expect-resolves.md +62 -0
  49. package/docs/rules/prefer-hooks-in-order.md +137 -0
  50. package/docs/rules/prefer-hooks-on-top.md +124 -0
  51. package/docs/rules/prefer-importing-jest-globals.md +85 -0
  52. package/docs/rules/prefer-jest-mocked.md +40 -0
  53. package/docs/rules/prefer-lowercase-title.md +128 -0
  54. package/docs/rules/prefer-mock-promise-shorthand.md +41 -0
  55. package/docs/rules/prefer-mock-return-shorthand.md +50 -0
  56. package/docs/rules/prefer-snapshot-hint.md +192 -0
  57. package/docs/rules/prefer-spy-on.md +46 -0
  58. package/docs/rules/prefer-strict-equal.md +29 -0
  59. package/docs/rules/prefer-to-be.md +63 -0
  60. package/docs/rules/prefer-to-contain.md +53 -0
  61. package/docs/rules/prefer-to-have-been-called-times.md +43 -0
  62. package/docs/rules/prefer-to-have-been-called.md +35 -0
  63. package/docs/rules/prefer-to-have-length.md +41 -0
  64. package/docs/rules/prefer-todo.md +33 -0
  65. package/docs/rules/require-hook.md +187 -0
  66. package/docs/rules/require-to-throw-message.md +43 -0
  67. package/docs/rules/require-top-level-describe.md +86 -0
  68. package/docs/rules/unbound-method.md +61 -0
  69. package/docs/rules/valid-describe-callback.md +69 -0
  70. package/docs/rules/valid-expect-in-promise.md +79 -0
  71. package/docs/rules/valid-expect-with-promise.md +43 -0
  72. package/docs/rules/valid-expect.md +155 -0
  73. package/docs/rules/valid-mock-module-path.md +72 -0
  74. package/docs/rules/valid-title.md +319 -0
  75. package/index.d.ts +28 -0
  76. package/lib/globals.json +15 -0
  77. package/lib/index.js +85 -0
  78. package/lib/rules/consistent-test-it.js +101 -0
  79. package/lib/rules/expect-expect.js +102 -0
  80. package/lib/rules/max-expects.js +70 -0
  81. package/lib/rules/max-nested-describe.js +59 -0
  82. package/lib/rules/no-alias-methods.js +62 -0
  83. package/lib/rules/no-commented-out-tests.js +44 -0
  84. package/lib/rules/no-conditional-expect.js +80 -0
  85. package/lib/rules/no-conditional-in-test.js +63 -0
  86. package/lib/rules/no-confusing-set-timeout.js +61 -0
  87. package/lib/rules/no-deprecated-functions.js +81 -0
  88. package/lib/rules/no-disabled-tests.js +57 -0
  89. package/lib/rules/no-done-callback.js +120 -0
  90. package/lib/rules/no-duplicate-hooks.js +52 -0
  91. package/lib/rules/no-error-equal.js +41 -0
  92. package/lib/rules/no-export.js +61 -0
  93. package/lib/rules/no-focused-tests.js +63 -0
  94. package/lib/rules/no-hooks.js +51 -0
  95. package/lib/rules/no-identical-title.js +73 -0
  96. package/lib/rules/no-interpolation-in-snapshots.js +43 -0
  97. package/lib/rules/no-jasmine-globals.js +140 -0
  98. package/lib/rules/no-large-snapshots.js +100 -0
  99. package/lib/rules/no-mocks-import.js +46 -0
  100. package/lib/rules/no-restricted-jest-methods.js +53 -0
  101. package/lib/rules/no-restricted-matchers.js +60 -0
  102. package/lib/rules/no-standalone-expect.js +114 -0
  103. package/lib/rules/no-test-prefixes.js +48 -0
  104. package/lib/rules/no-test-return-statement.js +62 -0
  105. package/lib/rules/no-unnecessary-assertion.js +87 -0
  106. package/lib/rules/no-unneeded-async-expect-function.js +57 -0
  107. package/lib/rules/no-untyped-mock-factory.js +61 -0
  108. package/lib/rules/padding-around-after-all-blocks.js +17 -0
  109. package/lib/rules/padding-around-after-each-blocks.js +17 -0
  110. package/lib/rules/padding-around-all.js +15 -0
  111. package/lib/rules/padding-around-before-all-blocks.js +17 -0
  112. package/lib/rules/padding-around-before-each-blocks.js +17 -0
  113. package/lib/rules/padding-around-describe-blocks.js +17 -0
  114. package/lib/rules/padding-around-expect-groups.js +21 -0
  115. package/lib/rules/padding-around-test-blocks.js +17 -0
  116. package/lib/rules/prefer-called-with.js +47 -0
  117. package/lib/rules/prefer-comparison-matcher.js +111 -0
  118. package/lib/rules/prefer-each.js +77 -0
  119. package/lib/rules/prefer-ending-with-an-expect.js +102 -0
  120. package/lib/rules/prefer-equality-matcher.js +87 -0
  121. package/lib/rules/prefer-expect-assertions.js +237 -0
  122. package/lib/rules/prefer-expect-resolves.js +47 -0
  123. package/lib/rules/prefer-hooks-in-order.js +67 -0
  124. package/lib/rules/prefer-hooks-on-top.js +41 -0
  125. package/lib/rules/prefer-importing-jest-globals.js +126 -0
  126. package/lib/rules/prefer-jest-mocked.js +66 -0
  127. package/lib/rules/prefer-lowercase-title.js +128 -0
  128. package/lib/rules/prefer-mock-promise-shorthand.js +88 -0
  129. package/lib/rules/prefer-mock-return-shorthand.js +122 -0
  130. package/lib/rules/prefer-snapshot-hint.js +111 -0
  131. package/lib/rules/prefer-spy-on.js +80 -0
  132. package/lib/rules/prefer-strict-equal.js +46 -0
  133. package/lib/rules/prefer-to-be.js +98 -0
  134. package/lib/rules/prefer-to-contain.js +82 -0
  135. package/lib/rules/prefer-to-have-been-called-times.js +70 -0
  136. package/lib/rules/prefer-to-have-been-called.js +59 -0
  137. package/lib/rules/prefer-to-have-length.js +57 -0
  138. package/lib/rules/prefer-todo.js +72 -0
  139. package/lib/rules/require-hook.js +93 -0
  140. package/lib/rules/require-to-throw-message.js +45 -0
  141. package/lib/rules/require-top-level-describe.js +86 -0
  142. package/lib/rules/unbound-method.js +86 -0
  143. package/lib/rules/utils/accessors.js +127 -0
  144. package/lib/rules/utils/ast-utils.js +59 -0
  145. package/lib/rules/utils/detectJestVersion.js +24 -0
  146. package/lib/rules/utils/followTypeAssertionChain.js +10 -0
  147. package/lib/rules/utils/index.js +71 -0
  148. package/lib/rules/utils/misc.js +168 -0
  149. package/lib/rules/utils/padding.js +308 -0
  150. package/lib/rules/utils/parseJestFnCall.js +325 -0
  151. package/lib/rules/utils/ts.js +73 -0
  152. package/lib/rules/valid-describe-callback.js +94 -0
  153. package/lib/rules/valid-expect-in-promise.js +311 -0
  154. package/lib/rules/valid-expect-with-promise.js +54 -0
  155. package/lib/rules/valid-expect.js +302 -0
  156. package/lib/rules/valid-mock-module-path.js +90 -0
  157. package/lib/rules/valid-title.js +252 -0
  158. package/package.json +164 -0
@@ -0,0 +1,44 @@
1
+ # jest/padding-around-expect-groups
2
+
3
+ 📝 Enforce padding around `expect` groups.
4
+
5
+ 🔧 This rule is automatically fixable by the
6
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ ## Rule Details
11
+
12
+ This rule enforces a line of padding before _and_ after 1 or more `expect`
13
+ statements
14
+
15
+ Note that it doesn't add/enforce a padding line if it's the last statement in
16
+ its scope and it doesn't add/enforce padding between two or more adjacent
17
+ `expect` statements.
18
+
19
+ Examples of **incorrect** code for this rule:
20
+
21
+ ```js
22
+ test('thing one', () => {
23
+ let abc = 123;
24
+ expect(abc).toEqual(123);
25
+ expect(123).toEqual(abc);
26
+ abc = 456;
27
+ expect(abc).toEqual(456);
28
+ });
29
+ ```
30
+
31
+ Examples of **correct** code for this rule:
32
+
33
+ ```js
34
+ test('thing one', () => {
35
+ let abc = 123;
36
+
37
+ expect(abc).toEqual(123);
38
+ expect(123).toEqual(abc);
39
+
40
+ abc = 456;
41
+
42
+ expect(abc).toEqual(456);
43
+ });
44
+ ```
@@ -0,0 +1,48 @@
1
+ # jest/padding-around-test-blocks
2
+
3
+ 📝 Enforce padding around `test` and `it` blocks.
4
+
5
+ 🔧 This rule is automatically fixable by the
6
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ ## Rule Details
11
+
12
+ This rule enforces a line of padding before _and_ after 1 or more `test`/`it`
13
+ statements
14
+
15
+ Note that it doesn't add/enforce a padding line if it's the last statement in
16
+ its scope
17
+
18
+ Examples of **incorrect** code for this rule:
19
+
20
+ ```js
21
+ const thing = 123;
22
+ test('foo', () => {});
23
+ test('bar', () => {});
24
+ ```
25
+
26
+ ```js
27
+ const thing = 123;
28
+ it('foo', () => {});
29
+ it('bar', () => {});
30
+ ```
31
+
32
+ Examples of **correct** code for this rule:
33
+
34
+ ```js
35
+ const thing = 123;
36
+
37
+ test('foo', () => {});
38
+
39
+ test('bar', () => {});
40
+ ```
41
+
42
+ ```js
43
+ const thing = 123;
44
+
45
+ it('foo', () => {});
46
+
47
+ it('bar', () => {});
48
+ ```
@@ -0,0 +1,39 @@
1
+ # jest/prefer-called-with
2
+
3
+ 📝 Suggest using `toHaveBeenCalledWith()`.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ The `toHaveBeenCalled()` matcher is used to assert that a mock function has been
8
+ called one or more times, without checking the arguments passed. The assertion
9
+ is stronger when arguments are also validated using the `toHaveBeenCalledWith()`
10
+ matcher. When some arguments are difficult to check, using generic match like
11
+ `expect.anything()` at least enforces number and position of arguments.
12
+
13
+ This rule warns if the form without argument checking is used, except for `.not`
14
+ enforcing a function has never been called.
15
+
16
+ ## Rule details
17
+
18
+ The following patterns are warnings:
19
+
20
+ ```js
21
+ expect(someFunction).toBeCalled();
22
+
23
+ expect(someFunction).toHaveBeenCalled();
24
+ ```
25
+
26
+ The following patterns are not warnings:
27
+
28
+ ```js
29
+ expect(noArgsFunction).toHaveBeenCalledWith();
30
+
31
+ expect(roughArgsFunction).toHaveBeenCalledWith(
32
+ expect.anything(),
33
+ expect.any(Date),
34
+ );
35
+
36
+ expect(anyArgsFunction).toHaveBeenCalledTimes(1);
37
+
38
+ expect(uncalledFunction).not.toHaveBeenCalled();
39
+ ```
@@ -0,0 +1,62 @@
1
+ # jest/prefer-comparison-matcher
2
+
3
+ 📝 Suggest using the built-in comparison matchers.
4
+
5
+ 🔧 This rule is automatically fixable by the
6
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ Jest has a number of built-in matchers for comparing numbers, which allow for
11
+ more readable tests and error messages if an expectation fails.
12
+
13
+ ## Rule details
14
+
15
+ This rule checks for comparisons in tests that could be replaced with one of the
16
+ following built-in comparison matchers:
17
+
18
+ - `toBeGreaterThan`
19
+ - `toBeGreaterThanOrEqual`
20
+ - `toBeLessThan`
21
+ - `toBeLessThanOrEqual`
22
+
23
+ Examples of **incorrect** code for this rule:
24
+
25
+ ```js
26
+ expect(x > 5).toBe(true);
27
+ expect(x < 7).not.toEqual(true);
28
+ expect(x <= y).toStrictEqual(true);
29
+ ```
30
+
31
+ Examples of **correct** code for this rule:
32
+
33
+ ```js
34
+ expect(x).toBeGreaterThan(5);
35
+ expect(x).not.toBeLessThanOrEqual(7);
36
+ expect(x).toBeLessThanOrEqual(y);
37
+
38
+ // special case - see below
39
+ expect(x < 'Carl').toBe(true);
40
+ ```
41
+
42
+ Note that these matchers only work with numbers and bigints, and that the rule
43
+ assumes that any variables on either side of the comparison operator are of one
44
+ of those types - this means if you're using the comparison operator with
45
+ strings, the fix applied by this rule will result in an error.
46
+
47
+ ```js
48
+ expect(myName).toBeGreaterThanOrEqual(theirName); // Matcher error: received value must be a number or bigint
49
+ ```
50
+
51
+ The reason for this is that comparing strings with these operators is expected
52
+ to be very rare and would mean not being able to have an automatic fixer for
53
+ this rule.
54
+
55
+ If for some reason you are using these operators to compare strings, you can
56
+ disable this rule using an inline
57
+ [configuration comment](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules):
58
+
59
+ ```js
60
+ // eslint-disable-next-line jest/prefer-comparison-matcher
61
+ expect(myName > theirName).toBe(true);
62
+ ```
@@ -0,0 +1,58 @@
1
+ # jest/prefer-each
2
+
3
+ 📝 Prefer using `.each` rather than manual loops.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ Reports where you might be able to use `.each` instead of native loops.
8
+
9
+ ## Rule details
10
+
11
+ This rule triggers a warning if you use test case functions like `describe`,
12
+ `test`, and `it`, in a native loop - generally you should be able to use `.each`
13
+ instead which gives better output and makes it easier to run specific cases.
14
+
15
+ Examples of **incorrect** code for this rule:
16
+
17
+ ```js
18
+ for (const number of getNumbers()) {
19
+ it('is greater than five', function () {
20
+ expect(number).toBeGreaterThan(5);
21
+ });
22
+ }
23
+
24
+ for (const [input, expected] of data) {
25
+ beforeEach(() => setupSomething(input));
26
+
27
+ test(`results in ${expected}`, () => {
28
+ expect(doSomething()).toBe(expected);
29
+ });
30
+ }
31
+ ```
32
+
33
+ Examples of **correct** code for this rule:
34
+
35
+ ```js
36
+ it.each(getNumbers())(
37
+ 'only returns numbers that are greater than seven',
38
+ number => {
39
+ expect(number).toBeGreaterThan(7);
40
+ },
41
+ );
42
+
43
+ describe.each(data)('when input is %s', ([input, expected]) => {
44
+ beforeEach(() => setupSomething(input));
45
+
46
+ test(`results in ${expected}`, () => {
47
+ expect(doSomething()).toBe(expected);
48
+ });
49
+ });
50
+
51
+ // we don't warn on loops _in_ test functions because those typically involve
52
+ // complex setup that is better done in the test function itself
53
+ it('returns numbers that are greater than five', () => {
54
+ for (const number of getNumbers()) {
55
+ expect(number).toBeGreaterThan(5);
56
+ }
57
+ });
58
+ ```
@@ -0,0 +1,170 @@
1
+ # jest/prefer-ending-with-an-expect
2
+
3
+ 📝 Prefer having the last statement in a test be an assertion.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ Prefer ending tests with an `expect` assertion.
8
+
9
+ ## Rule details
10
+
11
+ This rule triggers when a test body does not end with an `expect` call, which
12
+ can indicate an unfinished test.
13
+
14
+ Examples of **incorrect** code for this rule:
15
+
16
+ ```js
17
+ it('lets me change the selected option', () => {
18
+ const container = render(MySelect, {
19
+ props: { options: [1, 2, 3], selected: 1 },
20
+ });
21
+
22
+ expect(container).toBeDefined();
23
+ expect(container.toHTML()).toContain('<option value="1" selected>');
24
+
25
+ container.setProp('selected', 2);
26
+ });
27
+ ```
28
+
29
+ Examples of **correct** code for this rule:
30
+
31
+ ```js
32
+ it('lets me change the selected option', () => {
33
+ const container = render(MySelect, {
34
+ props: { options: [1, 2, 3], selected: 1 },
35
+ });
36
+
37
+ expect(container).toBeDefined();
38
+ expect(container.toHTML()).toContain('<option value="1" selected>');
39
+
40
+ container.setProp('selected', 2);
41
+
42
+ expect(container.toHTML()).not.toContain('<option value="1" selected>');
43
+ expect(container.toHTML()).toContain('<option value="2" selected>');
44
+ });
45
+ ```
46
+
47
+ ## Options
48
+
49
+ ```json
50
+ {
51
+ "jest/prefer-ending-with-an-expect": [
52
+ "error",
53
+ {
54
+ "assertFunctionNames": ["expect"],
55
+ "additionalTestBlockFunctions": []
56
+ }
57
+ ]
58
+ }
59
+ ```
60
+
61
+ ### `assertFunctionNames`
62
+
63
+ This array option specifies the names of functions that should be considered to
64
+ be asserting functions. Function names can use wildcards i.e `request.*.expect`,
65
+ `request.**.expect`, `request.*.expect*`
66
+
67
+ Examples of **incorrect** code for the `{ "assertFunctionNames": ["expect"] }`
68
+ option:
69
+
70
+ ```js
71
+ /* eslint jest/prefer-ending-with-an-expect: ["error", { "assertFunctionNames": ["expect"] }] */
72
+
73
+ import { expectSaga } from 'redux-saga-test-plan';
74
+ import { addSaga } from '../src/sagas';
75
+
76
+ test('returns sum', () => {
77
+ expectSaga(addSaga, 1, 1).returns(2).run();
78
+ });
79
+ ```
80
+
81
+ Examples of **correct** code for the
82
+ `{ "assertFunctionNames": ["expect", "expectSaga"] }` option:
83
+
84
+ ```js
85
+ /* eslint jest/prefer-ending-with-an-expect: ["error", { "assertFunctionNames": ["expect", "expectSaga"] }] */
86
+
87
+ import { expectSaga } from 'redux-saga-test-plan';
88
+ import { addSaga } from '../src/sagas';
89
+
90
+ test('returns sum', () => {
91
+ expectSaga(addSaga, 1, 1).returns(2).run();
92
+ });
93
+ ```
94
+
95
+ Since the string is compiled into a regular expression, you'll need to escape
96
+ special characters such as `$` with a double backslash:
97
+
98
+ ```js
99
+ /* eslint jest/prefer-ending-with-an-expect: ["error", { "assertFunctionNames": ["expect\\$"] }] */
100
+
101
+ it('is money-like', () => {
102
+ expect$(1.0);
103
+ });
104
+ ```
105
+
106
+ Examples of **correct** code for working with the HTTP assertions library
107
+ [SuperTest](https://www.npmjs.com/package/supertest) with the
108
+ `{ "assertFunctionNames": ["expect", "request.**.expect"] }` option:
109
+
110
+ ```js
111
+ /* eslint jest/prefer-ending-with-an-expect: ["error", { "assertFunctionNames": ["expect", "request.**.expect"] }] */
112
+ const request = require('supertest');
113
+ const express = require('express');
114
+
115
+ const app = express();
116
+
117
+ describe('GET /user', function () {
118
+ it('responds with json', function (done) {
119
+ doSomething();
120
+
121
+ request(app).get('/user').expect('Content-Type', /json/).expect(200, done);
122
+ });
123
+ });
124
+ ```
125
+
126
+ ### `additionalTestBlockFunctions`
127
+
128
+ This array can be used to specify the names of functions that should also be
129
+ treated as test blocks:
130
+
131
+ ```json
132
+ {
133
+ "rules": {
134
+ "jest/prefer-ending-with-an-expect": [
135
+ "error",
136
+ { "additionalTestBlockFunctions": ["each.test"] }
137
+ ]
138
+ }
139
+ }
140
+ ```
141
+
142
+ The following is _correct_ when using the above configuration:
143
+
144
+ ```js
145
+ each([
146
+ [2, 3],
147
+ [1, 3],
148
+ ]).test(
149
+ 'the selection can change from %d to %d',
150
+ (firstSelection, secondSelection) => {
151
+ const container = render(MySelect, {
152
+ props: { options: [1, 2, 3], selected: firstSelection },
153
+ });
154
+
155
+ expect(container).toBeDefined();
156
+ expect(container.toHTML()).toContain(
157
+ `<option value="${firstSelection}" selected>`,
158
+ );
159
+
160
+ container.setProp('selected', secondSelection);
161
+
162
+ expect(container.toHTML()).not.toContain(
163
+ `<option value="${firstSelection}" selected>`,
164
+ );
165
+ expect(container.toHTML()).toContain(
166
+ `<option value="${secondSelection}" selected>`,
167
+ );
168
+ },
169
+ );
170
+ ```
@@ -0,0 +1,36 @@
1
+ # jest/prefer-equality-matcher
2
+
3
+ 📝 Suggest using the built-in equality matchers.
4
+
5
+ 💡 This rule is manually fixable by
6
+ [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ Jest has built-in matchers for expecting equality, which allow for more readable
11
+ tests and error messages if an expectation fails.
12
+
13
+ ## Rule details
14
+
15
+ This rule checks for _strict_ equality checks (`===` & `!==`) in tests that
16
+ could be replaced with one of the following built-in equality matchers:
17
+
18
+ - `toBe`
19
+ - `toEqual`
20
+ - `toStrictEqual`
21
+
22
+ Examples of **incorrect** code for this rule:
23
+
24
+ ```js
25
+ expect(x === 5).toBe(true);
26
+ expect(name === 'Carl').not.toEqual(true);
27
+ expect(myObj !== thatObj).toStrictEqual(true);
28
+ ```
29
+
30
+ Examples of **correct** code for this rule:
31
+
32
+ ```js
33
+ expect(x).toBe(5);
34
+ expect(name).not.toEqual('Carl');
35
+ expect(myObj).not.toStrictEqual(thatObj);
36
+ ```
@@ -0,0 +1,230 @@
1
+ # jest/prefer-expect-assertions
2
+
3
+ 📝 Suggest using `expect.assertions()` OR `expect.hasAssertions()`.
4
+
5
+ 💡 This rule is manually fixable by
6
+ [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ Ensure every test to have either `expect.assertions(<number of assertions>)` OR
11
+ `expect.hasAssertions()` as its first expression.
12
+
13
+ ## Rule details
14
+
15
+ This rule triggers a warning if,
16
+
17
+ - `expect.assertions(<number of assertions>)` OR `expect.hasAssertions()` is not
18
+ present as first statement in a test, e.g.:
19
+
20
+ ```js
21
+ test('my test', () => {
22
+ expect(someThing()).toEqual('foo');
23
+ });
24
+ ```
25
+
26
+ - `expect.assertions(<number of assertions>)` is the first statement in a test
27
+ where argument passed to `expect.assertions(<number of assertions>)` is not a
28
+ valid number, e.g.:
29
+
30
+ ```js
31
+ test('my test', () => {
32
+ expect.assertions('1');
33
+ expect(someThing()).toEqual('foo');
34
+ });
35
+ ```
36
+
37
+ The following patterns are considered warnings:
38
+
39
+ ```js
40
+ test('my test', () => {
41
+ expect.assertions('1');
42
+ expect(someThing()).toEqual('foo');
43
+ });
44
+
45
+ test('my test', () => {
46
+ expect(someThing()).toEqual('foo');
47
+ });
48
+ ```
49
+
50
+ The following patterns would not be considered warnings:
51
+
52
+ ```js
53
+ test('my test', () => {
54
+ expect.assertions(1);
55
+ expect(someThing()).toEqual('foo');
56
+ });
57
+
58
+ test('my test', () => {
59
+ expect.hasAssertions();
60
+ expect(someThing()).toEqual('foo');
61
+ });
62
+ ```
63
+
64
+ ## Options
65
+
66
+ This rule can be configured to only check tests that match certain patterns that
67
+ typically look like `expect` calls might be missed, such as in promises or
68
+ loops.
69
+
70
+ By default, none of these options are enabled meaning the rule checks _every_
71
+ test for a call to either `expect.hasAssertions` or `expect.assertions`. If any
72
+ of the options are enabled the rule checks any test that matches _at least one_
73
+ of the patterns represented by the enabled options (think "OR" rather than
74
+ "AND").
75
+
76
+ #### `onlyFunctionsWithAsyncKeyword`
77
+
78
+ When `true`, this rule will only warn for tests that use the `async` keyword.
79
+
80
+ ```json
81
+ {
82
+ "rules": {
83
+ "jest/prefer-expect-assertions": [
84
+ "warn",
85
+ { "onlyFunctionsWithAsyncKeyword": true }
86
+ ]
87
+ }
88
+ }
89
+ ```
90
+
91
+ When `onlyFunctionsWithAsyncKeyword` option is set to `true`, the following
92
+ pattern would be a warning:
93
+
94
+ ```js
95
+ test('my test', async () => {
96
+ const result = await someAsyncFunc();
97
+ expect(result).toBe('foo');
98
+ });
99
+ ```
100
+
101
+ While the following patterns would not be considered warnings:
102
+
103
+ ```js
104
+ test('my test', () => {
105
+ const result = someFunction();
106
+ expect(result).toBe('foo');
107
+ });
108
+
109
+ test('my test', async () => {
110
+ expect.assertions(1);
111
+ const result = await someAsyncFunc();
112
+ expect(result).toBe('foo');
113
+ });
114
+ ```
115
+
116
+ #### `onlyFunctionsWithExpectInLoop`
117
+
118
+ When `true`, this rule will only warn for tests that have `expect` calls within
119
+ a native loop.
120
+
121
+ ```json
122
+ {
123
+ "rules": {
124
+ "jest/prefer-expect-assertions": [
125
+ "warn",
126
+ { "onlyFunctionsWithExpectInLoop": true }
127
+ ]
128
+ }
129
+ }
130
+ ```
131
+
132
+ Examples of **incorrect** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
133
+
134
+ ```js
135
+ describe('getNumbers', () => {
136
+ it('only returns numbers that are greater than zero', () => {
137
+ const numbers = getNumbers();
138
+
139
+ for (const number in numbers) {
140
+ expect(number).toBeGreaterThan(0);
141
+ }
142
+ });
143
+ });
144
+ ```
145
+
146
+ Examples of **correct** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
147
+
148
+ ```js
149
+ describe('getNumbers', () => {
150
+ it('only returns numbers that are greater than zero', () => {
151
+ expect.hasAssertions();
152
+
153
+ const numbers = getNumbers();
154
+
155
+ for (const number in numbers) {
156
+ expect(number).toBeGreaterThan(0);
157
+ }
158
+ });
159
+
160
+ it('returns more than one number', () => {
161
+ expect(getNumbers().length).toBeGreaterThan(1);
162
+ });
163
+ });
164
+ ```
165
+
166
+ #### `onlyFunctionsWithExpectInCallback`
167
+
168
+ When `true`, this rule will only warn for tests that have `expect` calls within
169
+ a callback.
170
+
171
+ ```json
172
+ {
173
+ "rules": {
174
+ "jest/prefer-expect-assertions": [
175
+ "warn",
176
+ { "onlyFunctionsWithExpectInCallback": true }
177
+ ]
178
+ }
179
+ }
180
+ ```
181
+
182
+ Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is
183
+ `true`:
184
+
185
+ ```js
186
+ describe('getNumbers', () => {
187
+ it('only returns numbers that are greater than zero', () => {
188
+ const numbers = getNumbers();
189
+
190
+ getNumbers().forEach(number => {
191
+ expect(number).toBeGreaterThan(0);
192
+ });
193
+ });
194
+ });
195
+
196
+ describe('/users', () => {
197
+ it.each([1, 2, 3])('returns ok', id => {
198
+ client.get(`/users/${id}`, response => {
199
+ expect(response.status).toBe(200);
200
+ });
201
+ });
202
+ });
203
+ ```
204
+
205
+ Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is
206
+ `true`:
207
+
208
+ ```js
209
+ describe('getNumbers', () => {
210
+ it('only returns numbers that are greater than zero', () => {
211
+ expect.hasAssertions();
212
+
213
+ const numbers = getNumbers();
214
+
215
+ getNumbers().forEach(number => {
216
+ expect(number).toBeGreaterThan(0);
217
+ });
218
+ });
219
+ });
220
+
221
+ describe('/users', () => {
222
+ it.each([1, 2, 3])('returns ok', id => {
223
+ expect.assertions(1);
224
+
225
+ client.get(`/users/${id}`, response => {
226
+ expect(response.status).toBe(200);
227
+ });
228
+ });
229
+ });
230
+ ```