eslint-plugin-jest 24.4.2 → 26.1.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 (77) hide show
  1. package/README.md +75 -50
  2. package/docs/rules/expect-expect.md +42 -1
  3. package/docs/rules/max-nested-describe.md +4 -5
  4. package/docs/rules/no-conditional-expect.md +57 -3
  5. package/docs/rules/no-conditional-in-test.md +79 -0
  6. package/docs/rules/no-deprecated-functions.md +5 -0
  7. package/docs/rules/no-done-callback.md +3 -3
  8. package/docs/rules/no-if.md +5 -0
  9. package/docs/rules/no-standalone-expect.md +3 -3
  10. package/docs/rules/no-test-return-statement.md +1 -2
  11. package/docs/rules/prefer-comparison-matcher.md +55 -0
  12. package/docs/rules/prefer-equality-matcher.md +29 -0
  13. package/docs/rules/prefer-expect-assertions.md +126 -0
  14. package/docs/rules/prefer-expect-resolves.md +53 -0
  15. package/docs/rules/prefer-hooks-on-top.md +72 -48
  16. package/docs/rules/{lowercase-name.md → prefer-lowercase-title.md} +7 -7
  17. package/docs/rules/prefer-snapshot-hint.md +188 -0
  18. package/docs/rules/prefer-to-be.md +53 -0
  19. package/docs/rules/require-hook.md +187 -0
  20. package/docs/rules/require-top-level-describe.md +28 -0
  21. package/docs/rules/{valid-describe.md → valid-describe-callback.md} +1 -1
  22. package/docs/rules/valid-expect-in-promise.md +55 -14
  23. package/docs/rules/valid-expect.md +13 -0
  24. package/docs/rules/valid-title.md +30 -2
  25. package/lib/index.js +2 -3
  26. package/lib/processors/snapshot-processor.js +1 -1
  27. package/lib/rules/consistent-test-it.js +20 -20
  28. package/lib/rules/detectJestVersion.js +29 -0
  29. package/lib/rules/expect-expect.js +25 -11
  30. package/lib/rules/max-nested-describe.js +5 -5
  31. package/lib/rules/no-conditional-expect.js +9 -9
  32. package/lib/rules/no-conditional-in-test.js +60 -0
  33. package/lib/rules/no-deprecated-functions.js +14 -32
  34. package/lib/rules/no-done-callback.js +10 -10
  35. package/lib/rules/no-export.js +6 -6
  36. package/lib/rules/no-focused-tests.js +11 -11
  37. package/lib/rules/no-identical-title.js +3 -3
  38. package/lib/rules/no-if.js +13 -11
  39. package/lib/rules/no-interpolation-in-snapshots.js +6 -6
  40. package/lib/rules/no-jasmine-globals.js +10 -10
  41. package/lib/rules/no-large-snapshots.js +11 -11
  42. package/lib/rules/no-standalone-expect.js +14 -14
  43. package/lib/rules/no-test-prefixes.js +6 -6
  44. package/lib/rules/no-test-return-statement.js +8 -8
  45. package/lib/rules/prefer-comparison-matcher.js +139 -0
  46. package/lib/rules/prefer-equality-matcher.js +98 -0
  47. package/lib/rules/prefer-expect-assertions.js +93 -11
  48. package/lib/rules/prefer-expect-resolves.js +48 -0
  49. package/lib/rules/prefer-hooks-on-top.js +1 -1
  50. package/lib/rules/{lowercase-name.js → prefer-lowercase-title.js} +20 -1
  51. package/lib/rules/prefer-snapshot-hint.js +112 -0
  52. package/lib/rules/prefer-spy-on.js +9 -9
  53. package/lib/rules/prefer-to-be.js +136 -0
  54. package/lib/rules/prefer-to-contain.js +19 -67
  55. package/lib/rules/prefer-to-have-length.js +9 -14
  56. package/lib/rules/prefer-todo.js +9 -9
  57. package/lib/rules/require-hook.js +121 -0
  58. package/lib/rules/require-top-level-describe.js +40 -6
  59. package/lib/rules/utils.js +34 -30
  60. package/lib/rules/{valid-describe.js → valid-describe-callback.js} +9 -9
  61. package/lib/rules/valid-expect-in-promise.js +336 -67
  62. package/lib/rules/valid-expect.js +36 -19
  63. package/lib/rules/valid-title.js +61 -61
  64. package/package.json +40 -27
  65. package/CHANGELOG.md +0 -513
  66. package/docs/rules/no-expect-resolves.md +0 -47
  67. package/docs/rules/no-truthy-falsy.md +0 -53
  68. package/docs/rules/no-try-expect.md +0 -63
  69. package/docs/rules/prefer-inline-snapshots.md +0 -51
  70. package/docs/rules/prefer-to-be-null.md +0 -33
  71. package/docs/rules/prefer-to-be-undefined.md +0 -33
  72. package/lib/rules/no-expect-resolves.js +0 -40
  73. package/lib/rules/no-truthy-falsy.js +0 -58
  74. package/lib/rules/no-try-expect.js +0 -89
  75. package/lib/rules/prefer-inline-snapshots.js +0 -69
  76. package/lib/rules/prefer-to-be-null.js +0 -67
  77. package/lib/rules/prefer-to-be-undefined.js +0 -67
package/README.md CHANGED
@@ -59,23 +59,43 @@ doing:
59
59
  This is included in all configs shared by this plugin, so can be omitted if
60
60
  extending them.
61
61
 
62
- The behaviour of some rules (specifically `no-deprecated-functions`) change
63
- depending on the version of `jest` being used.
62
+ ### Jest `version` setting
64
63
 
65
- This setting is detected automatically based off the version of the `jest`
66
- package installed in `node_modules`, but it can also be provided explicitly if
67
- desired:
64
+ The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
65
+ depending on the version of Jest being used.
66
+
67
+ By default, this plugin will attempt to determine to locate Jest using
68
+ `require.resolve`, meaning it will start looking in the closest `node_modules`
69
+ folder to the file being linted and work its way up.
70
+
71
+ Since we cache the automatically determined version, if you're linting
72
+ sub-folders that have different versions of Jest, you may find that the wrong
73
+ version of Jest is considered when linting. You can work around this by
74
+ providing the Jest version explicitly in nested ESLint configs:
68
75
 
69
76
  ```json
70
77
  {
71
78
  "settings": {
72
79
  "jest": {
73
- "version": 26
80
+ "version": 27
74
81
  }
75
82
  }
76
83
  }
77
84
  ```
78
85
 
86
+ To avoid hard-coding a number, you can also fetch it from the installed version
87
+ of Jest if you use a JavaScript config file such as `.eslintrc.js`:
88
+
89
+ ```js
90
+ module.exports = {
91
+ settings: {
92
+ jest: {
93
+ version: require('jest/package.json').version,
94
+ },
95
+ },
96
+ };
97
+ ```
98
+
79
99
  ## Shareable configurations
80
100
 
81
101
  ### Recommended
@@ -108,7 +128,7 @@ config file:
108
128
  ```
109
129
 
110
130
  See
111
- [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
131
+ [ESLint documentation](https://eslint.org/docs/user-guide/configuring/configuration-files#extending-configuration-files)
112
132
  for more information about extending configuration files.
113
133
 
114
134
  ### All
@@ -130,49 +150,53 @@ installations requiring long-term consistency.
130
150
 
131
151
  <!-- begin base rules list -->
132
152
 
133
- | Rule | Description | Configurations | Fixable |
134
- | ---------------------------------------------------------------------------- | --------------------------------------------------------------- | ---------------- | ------------ |
135
- | [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
136
- | [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
137
- | [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
138
- | [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | |
139
- | [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
140
- | [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
141
- | [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
142
- | [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
143
- | [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
144
- | [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
145
- | [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
146
- | [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
147
- | [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
148
- | [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
149
- | [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
150
- | [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
151
- | [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
152
- | [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
153
- | [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
154
- | [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
155
- | [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
156
- | [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
157
- | [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
158
- | [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
159
- | [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
160
- | [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
161
- | [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
162
- | [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
163
- | [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
164
- | [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
165
- | [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
166
- | [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | ![style][] | ![fixable][] |
167
- | [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
168
- | [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
169
- | [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
170
- | [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
171
- | [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
172
- | [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
173
- | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
174
- | [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | ![recommended][] | |
175
- | [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
153
+ | Rule | Description | Configurations | Fixable |
154
+ | ---------------------------------------------------------------------------- | ------------------------------------------------------------------- | ---------------- | ------------ |
155
+ | [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
156
+ | [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
157
+ | [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | |
158
+ | [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
159
+ | [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
160
+ | [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
161
+ | [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | |
162
+ | [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
163
+ | [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
164
+ | [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
165
+ | [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
166
+ | [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
167
+ | [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
168
+ | [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
169
+ | [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
170
+ | [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
171
+ | [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
172
+ | [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
173
+ | [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
174
+ | [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
175
+ | [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
176
+ | [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
177
+ | [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
178
+ | [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
179
+ | [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
180
+ | [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | ![fixable][] |
181
+ | [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | ![suggest][] |
182
+ | [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
183
+ | [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
184
+ | [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
185
+ | [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | ![fixable][] |
186
+ | [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | |
187
+ | [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
188
+ | [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
189
+ | [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | ![style][] | ![fixable][] |
190
+ | [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
191
+ | [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
192
+ | [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
193
+ | [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | |
194
+ | [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
195
+ | [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
196
+ | [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | ![recommended][] | |
197
+ | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
198
+ | [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Ensure promises that have expectations in their chain are valid | ![recommended][] | |
199
+ | [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
176
200
 
177
201
  <!-- end base rules list -->
178
202
 
@@ -226,3 +250,4 @@ https://github.com/istanbuljs/eslint-plugin-istanbul
226
250
  [suggest]: https://img.shields.io/badge/-suggest-yellow.svg
227
251
  [fixable]: https://img.shields.io/badge/-fixable-green.svg
228
252
  [style]: https://img.shields.io/badge/-style-blue.svg
253
+ [`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
@@ -34,7 +34,8 @@ it('should work with callbacks/async', () => {
34
34
  "jest/expect-expect": [
35
35
  "error",
36
36
  {
37
- "assertFunctionNames": ["expect"]
37
+ "assertFunctionNames": ["expect"],
38
+ "additionalTestBlockFunctions": []
38
39
  }
39
40
  ]
40
41
  }
@@ -102,3 +103,43 @@ describe('GET /user', function () {
102
103
  });
103
104
  });
104
105
  ```
106
+
107
+ ### `additionalTestBlockFunctions`
108
+
109
+ This array can be used to specify the names of functions that should also be
110
+ treated as test blocks:
111
+
112
+ ```json
113
+ {
114
+ "rules": {
115
+ "jest/expect-expect": [
116
+ "error",
117
+ { "additionalTestBlockFunctions": ["theoretically"] }
118
+ ]
119
+ }
120
+ }
121
+ ```
122
+
123
+ The following is _correct_ when using the above configuration:
124
+
125
+ ```js
126
+ import theoretically from 'jest-theories';
127
+
128
+ describe('NumberToLongString', () => {
129
+ const theories = [
130
+ { input: 100, expected: 'One hundred' },
131
+ { input: 1000, expected: 'One thousand' },
132
+ { input: 10000, expected: 'Ten thousand' },
133
+ { input: 100000, expected: 'One hundred thousand' },
134
+ ];
135
+
136
+ theoretically(
137
+ 'the number {input} is correctly translated to string',
138
+ theories,
139
+ theory => {
140
+ const output = NumberToLongString(theory.input);
141
+ expect(output).toBe(theory.expected);
142
+ },
143
+ );
144
+ });
145
+ ```
@@ -116,16 +116,15 @@ describe('foo', () => {
116
116
  });
117
117
  });
118
118
 
119
- describe('foo2', function()) {
120
- describe('bar2', function() {
121
- it('should get something', function() {
119
+ describe('foo2', function () {
120
+ describe('bar2', function () {
121
+ it('should get something', function () {
122
122
  expect(getSomething()).toBe('Something');
123
123
  });
124
124
 
125
- it('should get else', function() {
125
+ it('should get else', function () {
126
126
  expect(getSomething()).toBe('Something');
127
127
  });
128
128
  });
129
129
  });
130
-
131
130
  ```
@@ -8,9 +8,9 @@ assumed to be promises.
8
8
 
9
9
  ## Rule Details
10
10
 
11
- Jest considered a test to have failed if it throws an error, rather than on if
12
- any particular function is called, meaning conditional calls to `expect` could
13
- result in tests silently being skipped.
11
+ Jest only considers a test to have failed if it throws an error, meaning if
12
+ calls to assertion functions like `expect` occur in conditional code such as a
13
+ `catch` statement, tests can end up passing but not actually test anything.
14
14
 
15
15
  Additionally, conditionals tend to make tests more brittle and complex, as they
16
16
  increase the amount of mental thinking needed to understand what is actually
@@ -79,3 +79,57 @@ it('throws an error', async () => {
79
79
  await expect(foo).rejects.toThrow(Error);
80
80
  });
81
81
  ```
82
+
83
+ ### How to catch a thrown error for testing without violating this rule
84
+
85
+ A common situation that comes up with this rule is when wanting to test
86
+ properties on a thrown error, as Jest's `toThrow` matcher only checks the
87
+ `message` property.
88
+
89
+ Most people write something like this:
90
+
91
+ ```typescript
92
+ describe('when the http request fails', () => {
93
+ it('includes the status code in the error', async () => {
94
+ try {
95
+ await makeRequest(url);
96
+ } catch (error) {
97
+ expect(error).toHaveProperty('statusCode', 404);
98
+ }
99
+ });
100
+ });
101
+ ```
102
+
103
+ As stated above, the problem with this is that if `makeRequest()` doesn't throw
104
+ the test will still pass as if the `expect` had been called.
105
+
106
+ While you can use `expect.assertions` & `expect.hasAssertions` for these
107
+ situations, they only work with `expect`.
108
+
109
+ A better way to handle this situation is to introduce a wrapper to handle the
110
+ catching, and otherwise returns a specific "no error thrown" error if nothing is
111
+ thrown by the wrapped function:
112
+
113
+ ```typescript
114
+ class NoErrorThrownError extends Error {}
115
+
116
+ const getError = async <TError>(call: () => unknown): Promise<TError> => {
117
+ try {
118
+ await call();
119
+
120
+ throw new NoErrorThrownError();
121
+ } catch (error: unknown) {
122
+ return error as TError;
123
+ }
124
+ };
125
+
126
+ describe('when the http request fails', () => {
127
+ it('includes the status code in the error', async () => {
128
+ const error = await getError(async () => makeRequest(url));
129
+
130
+ // check that the returned error wasn't that no error was thrown
131
+ expect(error).not.toBeInstanceOf(NoErrorThrownError);
132
+ expect(error).toHaveProperty('statusCode', 404);
133
+ });
134
+ });
135
+ ```
@@ -0,0 +1,79 @@
1
+ # Disallow conditional logic in tests (`no-conditional-in-test`)
2
+
3
+ Conditional logic in tests is usually an indication that a test is attempting to
4
+ cover too much, and not testing the logic it intends to. Each branch of code
5
+ executing within a conditional statement will usually be better served by a test
6
+ devoted to it.
7
+
8
+ ## Rule Details
9
+
10
+ This rule reports on any use of a conditional statement such as `if`, `switch`,
11
+ and ternary expressions.
12
+
13
+ Examples of **incorrect** code for this rule:
14
+
15
+ ```js
16
+ it('foo', () => {
17
+ if (true) {
18
+ doTheThing();
19
+ }
20
+ });
21
+
22
+ it('bar', () => {
23
+ switch (mode) {
24
+ case 'none':
25
+ generateNone();
26
+ case 'single':
27
+ generateOne();
28
+ case 'multiple':
29
+ generateMany();
30
+ }
31
+
32
+ expect(fixtures.length).toBeGreaterThan(-1);
33
+ });
34
+
35
+ it('baz', async () => {
36
+ const promiseValue = () => {
37
+ return something instanceof Promise
38
+ ? something
39
+ : Promise.resolve(something);
40
+ };
41
+
42
+ await expect(promiseValue()).resolves.toBe(1);
43
+ });
44
+ ```
45
+
46
+ Examples of **correct** code for this rule:
47
+
48
+ ```js
49
+ describe('my tests', () => {
50
+ if (true) {
51
+ it('foo', () => {
52
+ doTheThing();
53
+ });
54
+ }
55
+ });
56
+
57
+ beforeEach(() => {
58
+ switch (mode) {
59
+ case 'none':
60
+ generateNone();
61
+ case 'single':
62
+ generateOne();
63
+ case 'multiple':
64
+ generateMany();
65
+ }
66
+ });
67
+
68
+ it('bar', () => {
69
+ expect(fixtures.length).toBeGreaterThan(-1);
70
+ });
71
+
72
+ const promiseValue = something => {
73
+ return something instanceof Promise ? something : Promise.resolve(something);
74
+ };
75
+
76
+ it('baz', async () => {
77
+ await expect(promiseValue()).resolves.toBe(1);
78
+ });
79
+ ```
@@ -6,6 +6,11 @@ either been renamed for clarity, or replaced with more powerful APIs.
6
6
  While typically these deprecated functions are kept in the codebase for a number
7
7
  of majors, eventually they are removed completely.
8
8
 
9
+ This rule requires knowing which version of Jest you're using - see
10
+ [this section of the readme](../../README.md#jest-version-setting) for details
11
+ on how that is obtained automatically and how you can explicitly provide a
12
+ version if needed.
13
+
9
14
  ## Rule details
10
15
 
11
16
  This rule warns about calls to deprecated functions, and provides details on
@@ -3,7 +3,7 @@
3
3
  When calling asynchronous code in hooks and tests, `jest` needs to know when the
4
4
  asynchronous work is complete to progress the current run.
5
5
 
6
- Originally the most common pattern to archive this was to use callbacks:
6
+ Originally the most common pattern to achieve this was to use callbacks:
7
7
 
8
8
  ```js
9
9
  test('the data is peanut butter', done => {
@@ -20,11 +20,11 @@ test('the data is peanut butter', done => {
20
20
  });
21
21
  ```
22
22
 
23
- This can be very error prone however, as it requires careful understanding of
23
+ This can be very error-prone however, as it requires careful understanding of
24
24
  how assertions work in tests or otherwise tests won't behave as expected.
25
25
 
26
26
  For example, if the `try/catch` was left out of the above code, the test would
27
- timeout rather than fail. Even with the `try/catch`, forgetting to pass the
27
+ time out rather than fail. Even with the `try/catch`, forgetting to pass the
28
28
  caught error to `done` will result in `jest` believing the test has passed.
29
29
 
30
30
  A more straightforward way to handle asynchronous code is to use Promises:
@@ -1,5 +1,10 @@
1
1
  # Disallow conditional logic (`no-if`)
2
2
 
3
+ ## Deprecated
4
+
5
+ This rule has been deprecated in favor of
6
+ [`no-conditional-in-test`](no-conditional-in-test.md).
7
+
3
8
  Conditional logic in tests is usually an indication that a test is attempting to
4
9
  cover too much, and not testing the logic it intends to. Each branch of code
5
10
  executing within an if statement will usually be better served by a test devoted
@@ -8,9 +8,9 @@ trigger this rule.
8
8
 
9
9
  This rule aims to eliminate `expect` statements that will not be executed. An
10
10
  `expect` inside of a `describe` block but outside of a `test` or `it` block or
11
- outside of a `describe` will not execute and therefore will trigger this rule.
12
- It is viable, however, to have an `expect` in a helper function that is called
13
- from within a `test` or `it` block so `expect` statements in a function will not
11
+ outside a `describe` will not execute and therefore will trigger this rule. It
12
+ is viable, however, to have an `expect` in a helper function that is called from
13
+ within a `test` or `it` block so `expect` statements in a function will not
14
14
  trigger this rule.
15
15
 
16
16
  Statements like `expect.hasAssertions()` will NOT trigger this rule since these
@@ -7,8 +7,7 @@ If you are returning Promises then you should update the test to use
7
7
 
8
8
  ## Rule details
9
9
 
10
- This rule triggers a warning if you use a return statement inside of a test
11
- body.
10
+ This rule triggers a warning if you use a return statement inside a test body.
12
11
 
13
12
  ```js
14
13
  /*eslint jest/no-test-return-statement: "error"*/
@@ -0,0 +1,55 @@
1
+ # Suggest using the built-in comparison matchers (`prefer-comparison-matcher`)
2
+
3
+ Jest has a number of built-in matchers for comparing numbers which allow for
4
+ more readable tests and error messages if an expectation fails.
5
+
6
+ ## Rule details
7
+
8
+ This rule checks for comparisons in tests that could be replaced with one of the
9
+ following built-in comparison matchers:
10
+
11
+ - `toBeGreaterThan`
12
+ - `toBeGreaterThanOrEqual`
13
+ - `toBeLessThan`
14
+ - `toBeLessThanOrEqual`
15
+
16
+ Examples of **incorrect** code for this rule:
17
+
18
+ ```js
19
+ expect(x > 5).toBe(true);
20
+ expect(x < 7).not.toEqual(true);
21
+ expect(x <= y).toStrictEqual(true);
22
+ ```
23
+
24
+ Examples of **correct** code for this rule:
25
+
26
+ ```js
27
+ expect(x).toBeGreaterThan(5);
28
+ expect(x).not.toBeLessThanOrEqual(7);
29
+ expect(x).toBeLessThanOrEqual(y);
30
+
31
+ // special case - see below
32
+ expect(x < 'Carl').toBe(true);
33
+ ```
34
+
35
+ Note that these matchers only work with numbers and bigints, and that the rule
36
+ assumes that any variables on either side of the comparison operator are of one
37
+ of those types - this means if you're using the comparison operator with
38
+ strings, the fix applied by this rule will result in an error.
39
+
40
+ ```js
41
+ expect(myName).toBeGreaterThanOrEqual(theirName); // Matcher error: received value must be a number or bigint
42
+ ```
43
+
44
+ The reason for this is that comparing strings with these operators is expected
45
+ to be very rare and would mean not being able to have an automatic fixer for
46
+ this rule.
47
+
48
+ If for some reason you are using these operators to compare strings, you can
49
+ disable this rule using an inline
50
+ [configuration comment](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules):
51
+
52
+ ```js
53
+ // eslint-disable-next-line jest/prefer-comparison-matcher
54
+ expect(myName > theirName).toBe(true);
55
+ ```
@@ -0,0 +1,29 @@
1
+ # Suggest using the built-in equality matchers (`prefer-equality-matcher`)
2
+
3
+ Jest has built-in matchers for expecting equality which allow for more readable
4
+ tests and error messages if an expectation fails.
5
+
6
+ ## Rule details
7
+
8
+ This rule checks for _strict_ equality checks (`===` & `!==`) in tests that
9
+ could be replaced with one of the following built-in equality matchers:
10
+
11
+ - `toBe`
12
+ - `toEqual`
13
+ - `toStrictEqual`
14
+
15
+ Examples of **incorrect** code for this rule:
16
+
17
+ ```js
18
+ expect(x === 5).toBe(true);
19
+ expect(name === 'Carl').not.toEqual(true);
20
+ expect(myObj !== thatObj).toStrictEqual(true);
21
+ ```
22
+
23
+ Examples of **correct** code for this rule:
24
+
25
+ ```js
26
+ expect(x).toBe(5);
27
+ expect(name).not.toEqual('Carl');
28
+ expect(myObj).toStrictEqual(thatObj);
29
+ ```