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.
- package/README.md +75 -50
- package/docs/rules/expect-expect.md +42 -1
- package/docs/rules/max-nested-describe.md +4 -5
- package/docs/rules/no-conditional-expect.md +57 -3
- package/docs/rules/no-conditional-in-test.md +79 -0
- package/docs/rules/no-deprecated-functions.md +5 -0
- package/docs/rules/no-done-callback.md +3 -3
- package/docs/rules/no-if.md +5 -0
- package/docs/rules/no-standalone-expect.md +3 -3
- package/docs/rules/no-test-return-statement.md +1 -2
- package/docs/rules/prefer-comparison-matcher.md +55 -0
- package/docs/rules/prefer-equality-matcher.md +29 -0
- package/docs/rules/prefer-expect-assertions.md +126 -0
- package/docs/rules/prefer-expect-resolves.md +53 -0
- package/docs/rules/prefer-hooks-on-top.md +72 -48
- package/docs/rules/{lowercase-name.md → prefer-lowercase-title.md} +7 -7
- package/docs/rules/prefer-snapshot-hint.md +188 -0
- package/docs/rules/prefer-to-be.md +53 -0
- package/docs/rules/require-hook.md +187 -0
- package/docs/rules/require-top-level-describe.md +28 -0
- package/docs/rules/{valid-describe.md → valid-describe-callback.md} +1 -1
- package/docs/rules/valid-expect-in-promise.md +55 -14
- package/docs/rules/valid-expect.md +13 -0
- package/docs/rules/valid-title.md +30 -2
- package/lib/index.js +2 -3
- package/lib/processors/snapshot-processor.js +1 -1
- package/lib/rules/consistent-test-it.js +20 -20
- package/lib/rules/detectJestVersion.js +29 -0
- package/lib/rules/expect-expect.js +25 -11
- package/lib/rules/max-nested-describe.js +5 -5
- package/lib/rules/no-conditional-expect.js +9 -9
- package/lib/rules/no-conditional-in-test.js +60 -0
- package/lib/rules/no-deprecated-functions.js +14 -32
- package/lib/rules/no-done-callback.js +10 -10
- package/lib/rules/no-export.js +6 -6
- package/lib/rules/no-focused-tests.js +11 -11
- package/lib/rules/no-identical-title.js +3 -3
- package/lib/rules/no-if.js +13 -11
- package/lib/rules/no-interpolation-in-snapshots.js +6 -6
- package/lib/rules/no-jasmine-globals.js +10 -10
- package/lib/rules/no-large-snapshots.js +11 -11
- package/lib/rules/no-standalone-expect.js +14 -14
- package/lib/rules/no-test-prefixes.js +6 -6
- package/lib/rules/no-test-return-statement.js +8 -8
- package/lib/rules/prefer-comparison-matcher.js +139 -0
- package/lib/rules/prefer-equality-matcher.js +98 -0
- package/lib/rules/prefer-expect-assertions.js +93 -11
- package/lib/rules/prefer-expect-resolves.js +48 -0
- package/lib/rules/prefer-hooks-on-top.js +1 -1
- package/lib/rules/{lowercase-name.js → prefer-lowercase-title.js} +20 -1
- package/lib/rules/prefer-snapshot-hint.js +112 -0
- package/lib/rules/prefer-spy-on.js +9 -9
- package/lib/rules/prefer-to-be.js +136 -0
- package/lib/rules/prefer-to-contain.js +19 -67
- package/lib/rules/prefer-to-have-length.js +9 -14
- package/lib/rules/prefer-todo.js +9 -9
- package/lib/rules/require-hook.js +121 -0
- package/lib/rules/require-top-level-describe.js +40 -6
- package/lib/rules/utils.js +34 -30
- package/lib/rules/{valid-describe.js → valid-describe-callback.js} +9 -9
- package/lib/rules/valid-expect-in-promise.js +336 -67
- package/lib/rules/valid-expect.js +36 -19
- package/lib/rules/valid-title.js +61 -61
- package/package.json +40 -27
- package/CHANGELOG.md +0 -513
- package/docs/rules/no-expect-resolves.md +0 -47
- package/docs/rules/no-truthy-falsy.md +0 -53
- package/docs/rules/no-try-expect.md +0 -63
- package/docs/rules/prefer-inline-snapshots.md +0 -51
- package/docs/rules/prefer-to-be-null.md +0 -33
- package/docs/rules/prefer-to-be-undefined.md +0 -33
- package/lib/rules/no-expect-resolves.js +0 -40
- package/lib/rules/no-truthy-falsy.js +0 -58
- package/lib/rules/no-try-expect.js +0 -89
- package/lib/rules/prefer-inline-snapshots.js +0 -69
- package/lib/rules/prefer-to-be-null.js +0 -67
- package/lib/rules/prefer-to-be-undefined.js +0 -67
|
@@ -58,6 +58,16 @@ test('my test', () => {
|
|
|
58
58
|
|
|
59
59
|
## Options
|
|
60
60
|
|
|
61
|
+
This rule can be configured to only check tests that match certain patterns that
|
|
62
|
+
typically look like `expect` calls might be missed, such as in promises or
|
|
63
|
+
loops.
|
|
64
|
+
|
|
65
|
+
By default, none of these options are enabled meaning the rule checks _every_
|
|
66
|
+
test for a call to either `expect.hasAssertions` or `expect.assertions`. If any
|
|
67
|
+
of the options are enabled the rule checks any test that matches _at least one_
|
|
68
|
+
of the patterns represented by the enabled options (think "OR" rather than
|
|
69
|
+
"AND").
|
|
70
|
+
|
|
61
71
|
#### `onlyFunctionsWithAsyncKeyword`
|
|
62
72
|
|
|
63
73
|
When `true`, this rule will only warn for tests that use the `async` keyword.
|
|
@@ -97,3 +107,119 @@ test('my test', async () => {
|
|
|
97
107
|
expect(result).toBe('foo');
|
|
98
108
|
});
|
|
99
109
|
```
|
|
110
|
+
|
|
111
|
+
#### `onlyFunctionsWithExpectInLoop`
|
|
112
|
+
|
|
113
|
+
When `true`, this rule will only warn for tests that have `expect` calls within
|
|
114
|
+
a native loop.
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"rules": {
|
|
119
|
+
"jest/prefer-expect-assertions": [
|
|
120
|
+
"warn",
|
|
121
|
+
{ "onlyFunctionsWithAsyncKeyword": true }
|
|
122
|
+
]
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Examples of **incorrect** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
describe('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
|
+
|
|
141
|
+
Examples of **correct** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
describe('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
|
+
|
|
163
|
+
When `true`, this rule will only warn for tests that have `expect` calls within
|
|
164
|
+
a callback.
|
|
165
|
+
|
|
166
|
+
```json
|
|
167
|
+
{
|
|
168
|
+
"rules": {
|
|
169
|
+
"jest/prefer-expect-assertions": [
|
|
170
|
+
"warn",
|
|
171
|
+
{ "onlyFunctionsWithExpectInCallback": true }
|
|
172
|
+
]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is
|
|
178
|
+
`true`:
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
describe('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
|
+
|
|
191
|
+
describe('/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
|
+
|
|
200
|
+
Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is
|
|
201
|
+
`true`:
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
describe('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
|
+
|
|
216
|
+
describe('/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
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`)
|
|
2
|
+
|
|
3
|
+
When working with promises, there are two primary ways you can test the resolved
|
|
4
|
+
value:
|
|
5
|
+
|
|
6
|
+
1. use the `resolve` modifier on `expect`
|
|
7
|
+
(`await expect(...).resolves.<matcher>` style)
|
|
8
|
+
2. `await` the promise and assert against its result
|
|
9
|
+
(`expect(await ...).<matcher>` style)
|
|
10
|
+
|
|
11
|
+
While the second style is arguably less dependent on `jest`, if the promise
|
|
12
|
+
rejects it will be treated as a general error, resulting in less predictable
|
|
13
|
+
behaviour and output from `jest`.
|
|
14
|
+
|
|
15
|
+
Additionally, favoring the first style ensures consistency with its `rejects`
|
|
16
|
+
counterpart, as there is no way of "awaiting" a rejection.
|
|
17
|
+
|
|
18
|
+
## Rule details
|
|
19
|
+
|
|
20
|
+
This rule triggers a warning if an `await` is done within an `expect`, and
|
|
21
|
+
recommends using `resolves` instead.
|
|
22
|
+
|
|
23
|
+
Examples of **incorrect** code for this rule
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
it('passes', async () => {
|
|
27
|
+
expect(await someValue()).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('is true', async () => {
|
|
31
|
+
const myPromise = Promise.resolve(true);
|
|
32
|
+
|
|
33
|
+
expect(await myPromise).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Examples of **correct** code for this rule
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
it('passes', async () => {
|
|
41
|
+
await expect(someValue()).resolves.toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('is true', async () => {
|
|
45
|
+
const myPromise = Promise.resolve(true);
|
|
46
|
+
|
|
47
|
+
await expect(myPromise).resolves.toBe(true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('errors', async () => {
|
|
51
|
+
await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!');
|
|
52
|
+
});
|
|
53
|
+
```
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# Suggest having hooks before any test cases (`prefer-hooks-on-top`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
While hooks can be setup anywhere in a test file, they are always called in a
|
|
4
|
+
specific order which means it can be confusing if they're intermixed with test
|
|
5
|
+
cases.
|
|
6
|
+
|
|
7
|
+
This rule helps to ensure that hooks are always defined before test cases.
|
|
4
8
|
|
|
5
9
|
## Rule Details
|
|
6
10
|
|
|
@@ -11,47 +15,51 @@ Examples of **incorrect** code for this rule
|
|
|
11
15
|
|
|
12
16
|
describe('foo', () => {
|
|
13
17
|
beforeEach(() => {
|
|
14
|
-
|
|
15
|
-
});
|
|
16
|
-
test('bar', () => {
|
|
17
|
-
some_fn();
|
|
18
|
+
seedMyDatabase();
|
|
18
19
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
test('bar', () => {
|
|
23
|
-
some_fn();
|
|
20
|
+
|
|
21
|
+
it('accepts this input', () => {
|
|
22
|
+
// ...
|
|
24
23
|
});
|
|
25
|
-
});
|
|
26
24
|
|
|
27
|
-
// Nested describe scenario
|
|
28
|
-
describe('foo', () => {
|
|
29
25
|
beforeAll(() => {
|
|
30
|
-
|
|
26
|
+
createMyDatabase();
|
|
31
27
|
});
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
|
|
29
|
+
it('returns that value', () => {
|
|
30
|
+
// ...
|
|
34
31
|
});
|
|
35
|
-
|
|
32
|
+
|
|
33
|
+
describe('when the database has specific values', () => {
|
|
34
|
+
const specificValue = '...';
|
|
35
|
+
|
|
36
36
|
beforeEach(() => {
|
|
37
|
-
|
|
37
|
+
seedMyDatabase(specificValue);
|
|
38
38
|
});
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
|
|
40
|
+
it('accepts that input', () => {
|
|
41
|
+
// ...
|
|
41
42
|
});
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
|
|
44
|
+
it('throws an error', () => {
|
|
45
|
+
// ...
|
|
44
46
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
clearLogger();
|
|
47
50
|
});
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
mockLogger();
|
|
50
53
|
});
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
|
|
55
|
+
it('logs a message', () => {
|
|
56
|
+
// ...
|
|
53
57
|
});
|
|
54
58
|
});
|
|
59
|
+
|
|
60
|
+
afterAll(() => {
|
|
61
|
+
removeMyDatabase();
|
|
62
|
+
});
|
|
55
63
|
});
|
|
56
64
|
```
|
|
57
65
|
|
|
@@ -61,35 +69,51 @@ Examples of **correct** code for this rule
|
|
|
61
69
|
/* eslint jest/prefer-hooks-on-top: "error" */
|
|
62
70
|
|
|
63
71
|
describe('foo', () => {
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
beforeAll(() => {
|
|
73
|
+
createMyDatabase();
|
|
66
74
|
});
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
afterEach(() => {
|
|
72
|
-
//some hook code
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
seedMyDatabase();
|
|
73
78
|
});
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
|
|
80
|
+
afterAll(() => {
|
|
81
|
+
clearMyDatabase();
|
|
76
82
|
});
|
|
77
|
-
});
|
|
78
83
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
beforeEach(() => {
|
|
82
|
-
//some hook code
|
|
84
|
+
it('accepts this input', () => {
|
|
85
|
+
// ...
|
|
83
86
|
});
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
|
|
88
|
+
it('returns that value', () => {
|
|
89
|
+
// ...
|
|
86
90
|
});
|
|
87
|
-
|
|
91
|
+
|
|
92
|
+
describe('when the database has specific values', () => {
|
|
93
|
+
const specificValue = '...';
|
|
94
|
+
|
|
88
95
|
beforeEach(() => {
|
|
89
|
-
|
|
96
|
+
seedMyDatabase(specificValue);
|
|
90
97
|
});
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
mockLogger();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
afterEach(() => {
|
|
104
|
+
clearLogger();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('accepts that input', () => {
|
|
108
|
+
// ...
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('throws an error', () => {
|
|
112
|
+
// ...
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('logs a message', () => {
|
|
116
|
+
// ...
|
|
93
117
|
});
|
|
94
118
|
});
|
|
95
119
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Enforce lowercase test names (`lowercase-
|
|
1
|
+
# Enforce lowercase test names (`prefer-lowercase-title`)
|
|
2
2
|
|
|
3
3
|
## Rule details
|
|
4
4
|
|
|
@@ -26,7 +26,7 @@ it('adds 1 + 2 to equal 3', () => {
|
|
|
26
26
|
|
|
27
27
|
```json
|
|
28
28
|
{
|
|
29
|
-
"jest/lowercase-
|
|
29
|
+
"jest/prefer-lowercase-title": [
|
|
30
30
|
"error",
|
|
31
31
|
{
|
|
32
32
|
"ignore": ["describe", "test"]
|
|
@@ -50,7 +50,7 @@ By default, none of these options are enabled (the equivalent of
|
|
|
50
50
|
Example of **correct** code for the `{ "ignore": ["describe"] }` option:
|
|
51
51
|
|
|
52
52
|
```js
|
|
53
|
-
/* eslint jest/lowercase-
|
|
53
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["describe"] }] */
|
|
54
54
|
|
|
55
55
|
describe('Uppercase description');
|
|
56
56
|
```
|
|
@@ -58,7 +58,7 @@ describe('Uppercase description');
|
|
|
58
58
|
Example of **correct** code for the `{ "ignore": ["test"] }` option:
|
|
59
59
|
|
|
60
60
|
```js
|
|
61
|
-
/* eslint jest/lowercase-
|
|
61
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["test"] }] */
|
|
62
62
|
|
|
63
63
|
test('Uppercase description');
|
|
64
64
|
```
|
|
@@ -66,7 +66,7 @@ test('Uppercase description');
|
|
|
66
66
|
Example of **correct** code for the `{ "ignore": ["it"] }` option:
|
|
67
67
|
|
|
68
68
|
```js
|
|
69
|
-
/* eslint jest/lowercase-
|
|
69
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["it"] }] */
|
|
70
70
|
|
|
71
71
|
it('Uppercase description');
|
|
72
72
|
```
|
|
@@ -82,7 +82,7 @@ By default, nothing is allowed (the equivalent of `{ "allowedPrefixes": [] }`).
|
|
|
82
82
|
Example of **correct** code for the `{ "allowedPrefixes": ["GET"] }` option:
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
|
-
/* eslint jest/lowercase-
|
|
85
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "allowedPrefixes": ["GET"] }] */
|
|
86
86
|
|
|
87
87
|
describe('GET /live');
|
|
88
88
|
```
|
|
@@ -95,7 +95,7 @@ title starting with an upper-case letter.
|
|
|
95
95
|
Example of **correct** code for the `{ "ignoreTopLevelDescribe": true }` option:
|
|
96
96
|
|
|
97
97
|
```js
|
|
98
|
-
/* eslint jest/lowercase-
|
|
98
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignoreTopLevelDescribe": true }] */
|
|
99
99
|
describe('MyClass', () => {
|
|
100
100
|
describe('#myMethod', () => {
|
|
101
101
|
it('does things', () => {
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# Prefer including a hint with external snapshots (`prefer-snapshot-hint`)
|
|
2
|
+
|
|
3
|
+
When working with external snapshot matchers it's considered best practice to
|
|
4
|
+
provide a hint (as the last argument to the matcher) describing the expected
|
|
5
|
+
snapshot content that will be included in the snapshots name by Jest.
|
|
6
|
+
|
|
7
|
+
This makes it easier for reviewers to verify the snapshots during review, and
|
|
8
|
+
for anyone to know whether an outdated snapshot is the correct behavior before
|
|
9
|
+
updating.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule looks for any use of an external snapshot matcher (e.g.
|
|
14
|
+
`toMatchSnapshot` and `toThrowErrorMatchingSnapshot`) and checks if they include
|
|
15
|
+
a snapshot hint.
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
### `'always'`
|
|
20
|
+
|
|
21
|
+
Require a hint to _always_ be provided when using external snapshot matchers.
|
|
22
|
+
|
|
23
|
+
Examples of **incorrect** code for the `'always'` option:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
27
|
+
expect(stdout).toMatchSnapshot();
|
|
28
|
+
expect(stderr).toMatchSnapshot();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe('cli', () => {
|
|
32
|
+
describe('--version flag', () => {
|
|
33
|
+
it('prints the version', async () => {
|
|
34
|
+
snapshotOutput(await runCli(['--version']));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('--config flag', () => {
|
|
39
|
+
it('reads the config', async () => {
|
|
40
|
+
const { stdout, parsedConfig } = await runCli([
|
|
41
|
+
'--config',
|
|
42
|
+
'jest.config.js',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
expect(stdout).toMatchSnapshot();
|
|
46
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('prints nothing to stderr', async () => {
|
|
50
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
51
|
+
|
|
52
|
+
expect(stderr).toMatchSnapshot();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('when the file does not exist', () => {
|
|
56
|
+
it('throws an error', async () => {
|
|
57
|
+
await expect(
|
|
58
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
59
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Examples of **correct** code for the `'always'` option:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
70
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
71
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
describe('cli', () => {
|
|
75
|
+
describe('--version flag', () => {
|
|
76
|
+
it('prints the version', async () => {
|
|
77
|
+
snapshotOutput(await runCli(['--version']), {
|
|
78
|
+
stdout: 'version string',
|
|
79
|
+
stderr: 'empty',
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('--config flag', () => {
|
|
85
|
+
it('reads the config', async () => {
|
|
86
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
87
|
+
|
|
88
|
+
expect(stdout).toMatchSnapshot({}, 'stdout: config settings');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('prints nothing to stderr', async () => {
|
|
92
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
93
|
+
|
|
94
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('when the file does not exist', () => {
|
|
98
|
+
it('throws an error', async () => {
|
|
99
|
+
await expect(
|
|
100
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
101
|
+
).rejects.toThrowErrorMatchingSnapshot('stderr: config error');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `'multi'` (default)
|
|
109
|
+
|
|
110
|
+
Require a hint to be provided when there are multiple external snapshot matchers
|
|
111
|
+
within the scope (meaning it includes nested calls).
|
|
112
|
+
|
|
113
|
+
Examples of **incorrect** code for the `'multi'` option:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
117
|
+
expect(stdout).toMatchSnapshot();
|
|
118
|
+
expect(stderr).toMatchSnapshot();
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
describe('cli', () => {
|
|
122
|
+
describe('--version flag', () => {
|
|
123
|
+
it('prints the version', async () => {
|
|
124
|
+
snapshotOutput(await runCli(['--version']));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('--config flag', () => {
|
|
129
|
+
it('reads the config', async () => {
|
|
130
|
+
const { stdout, parsedConfig } = await runCli([
|
|
131
|
+
'--config',
|
|
132
|
+
'jest.config.js',
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
expect(stdout).toMatchSnapshot();
|
|
136
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('prints nothing to stderr', async () => {
|
|
140
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
141
|
+
|
|
142
|
+
expect(stderr).toMatchSnapshot();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Examples of **correct** code for the `'multi'` option:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
152
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
153
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
describe('cli', () => {
|
|
157
|
+
describe('--version flag', () => {
|
|
158
|
+
it('prints the version', async () => {
|
|
159
|
+
snapshotOutput(await runCli(['--version']), {
|
|
160
|
+
stdout: 'version string',
|
|
161
|
+
stderr: 'empty',
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('--config flag', () => {
|
|
167
|
+
it('reads the config', async () => {
|
|
168
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
169
|
+
|
|
170
|
+
expect(stdout).toMatchSnapshot();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('prints nothing to stderr', async () => {
|
|
174
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
175
|
+
|
|
176
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('when the file does not exist', () => {
|
|
180
|
+
it('throws an error', async () => {
|
|
181
|
+
await expect(
|
|
182
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
183
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Suggest using `toBe()` for primitive literals (`prefer-to-be`)
|
|
2
|
+
|
|
3
|
+
When asserting against primitive literals such as numbers and strings, the
|
|
4
|
+
equality matchers all operate the same, but read slightly differently in code.
|
|
5
|
+
|
|
6
|
+
This rule recommends using the `toBe` matcher in these situations, as it forms
|
|
7
|
+
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
|
|
8
|
+
rule recommends using their specific `toBe` matchers, as they give better error
|
|
9
|
+
messages as well.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to
|
|
14
|
+
assert a primitive literal value such as numbers, strings, and booleans.
|
|
15
|
+
|
|
16
|
+
The following patterns are considered warnings:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
expect(value).not.toEqual(5);
|
|
20
|
+
expect(getMessage()).toStrictEqual('hello world');
|
|
21
|
+
expect(loadMessage()).resolves.toEqual('hello world');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The following pattern is not warning:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
expect(value).not.toBe(5);
|
|
28
|
+
expect(getMessage()).toBe('hello world');
|
|
29
|
+
expect(loadMessage()).resolves.toBe('hello world');
|
|
30
|
+
expect(didError).not.toBe(true);
|
|
31
|
+
|
|
32
|
+
expect(catchError()).toStrictEqual({ message: 'oh noes!' });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
|
|
36
|
+
used to assert against those literal values instead of their more specific
|
|
37
|
+
`toBe` counterparts:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
expect(value).not.toBe(undefined);
|
|
41
|
+
expect(getMessage()).toBe(null);
|
|
42
|
+
expect(countMessages()).resolves.not.toBe(NaN);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The following pattern is not warning:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
expect(value).toBeDefined();
|
|
49
|
+
expect(getMessage()).toBeNull();
|
|
50
|
+
expect(countMessages()).resolves.not.toBeNaN();
|
|
51
|
+
|
|
52
|
+
expect(catchError()).toStrictEqual({ message: undefined });
|
|
53
|
+
```
|