@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.
- package/LICENSE +21 -0
- package/README.md +31 -0
- package/changes.json +10 -0
- package/docs/rules/consistent-test-it.md +96 -0
- package/docs/rules/expect-expect.md +152 -0
- package/docs/rules/max-expects.md +78 -0
- package/docs/rules/max-nested-describe.md +134 -0
- package/docs/rules/no-alias-methods.md +57 -0
- package/docs/rules/no-commented-out-tests.md +68 -0
- package/docs/rules/no-conditional-expect.md +142 -0
- package/docs/rules/no-conditional-in-test.md +123 -0
- package/docs/rules/no-confusing-set-timeout.md +64 -0
- package/docs/rules/no-deprecated-functions.md +59 -0
- package/docs/rules/no-disabled-tests.md +72 -0
- package/docs/rules/no-done-callback.md +100 -0
- package/docs/rules/no-duplicate-hooks.md +79 -0
- package/docs/rules/no-error-equal.md +37 -0
- package/docs/rules/no-export.md +52 -0
- package/docs/rules/no-focused-tests.md +67 -0
- package/docs/rules/no-hooks.md +178 -0
- package/docs/rules/no-identical-title.md +59 -0
- package/docs/rules/no-interpolation-in-snapshots.md +67 -0
- package/docs/rules/no-jasmine-globals.md +69 -0
- package/docs/rules/no-large-snapshots.md +180 -0
- package/docs/rules/no-mocks-import.md +34 -0
- package/docs/rules/no-restricted-jest-methods.md +53 -0
- package/docs/rules/no-restricted-matchers.md +64 -0
- package/docs/rules/no-standalone-expect.md +106 -0
- package/docs/rules/no-test-prefixes.md +42 -0
- package/docs/rules/no-test-return-statement.md +50 -0
- package/docs/rules/no-unnecessary-assertion.md +41 -0
- package/docs/rules/no-unneeded-async-expect-function.md +41 -0
- package/docs/rules/no-untyped-mock-factory.md +74 -0
- package/docs/rules/padding-around-after-all-blocks.md +34 -0
- package/docs/rules/padding-around-after-each-blocks.md +38 -0
- package/docs/rules/padding-around-all.md +20 -0
- package/docs/rules/padding-around-before-all-blocks.md +37 -0
- package/docs/rules/padding-around-before-each-blocks.md +38 -0
- package/docs/rules/padding-around-describe-blocks.md +42 -0
- package/docs/rules/padding-around-expect-groups.md +44 -0
- package/docs/rules/padding-around-test-blocks.md +48 -0
- package/docs/rules/prefer-called-with.md +39 -0
- package/docs/rules/prefer-comparison-matcher.md +62 -0
- package/docs/rules/prefer-each.md +58 -0
- package/docs/rules/prefer-ending-with-an-expect.md +170 -0
- package/docs/rules/prefer-equality-matcher.md +36 -0
- package/docs/rules/prefer-expect-assertions.md +230 -0
- package/docs/rules/prefer-expect-resolves.md +62 -0
- package/docs/rules/prefer-hooks-in-order.md +137 -0
- package/docs/rules/prefer-hooks-on-top.md +124 -0
- package/docs/rules/prefer-importing-jest-globals.md +85 -0
- package/docs/rules/prefer-jest-mocked.md +40 -0
- package/docs/rules/prefer-lowercase-title.md +128 -0
- package/docs/rules/prefer-mock-promise-shorthand.md +41 -0
- package/docs/rules/prefer-mock-return-shorthand.md +50 -0
- package/docs/rules/prefer-snapshot-hint.md +192 -0
- package/docs/rules/prefer-spy-on.md +46 -0
- package/docs/rules/prefer-strict-equal.md +29 -0
- package/docs/rules/prefer-to-be.md +63 -0
- package/docs/rules/prefer-to-contain.md +53 -0
- package/docs/rules/prefer-to-have-been-called-times.md +43 -0
- package/docs/rules/prefer-to-have-been-called.md +35 -0
- package/docs/rules/prefer-to-have-length.md +41 -0
- package/docs/rules/prefer-todo.md +33 -0
- package/docs/rules/require-hook.md +187 -0
- package/docs/rules/require-to-throw-message.md +43 -0
- package/docs/rules/require-top-level-describe.md +86 -0
- package/docs/rules/unbound-method.md +61 -0
- package/docs/rules/valid-describe-callback.md +69 -0
- package/docs/rules/valid-expect-in-promise.md +79 -0
- package/docs/rules/valid-expect-with-promise.md +43 -0
- package/docs/rules/valid-expect.md +155 -0
- package/docs/rules/valid-mock-module-path.md +72 -0
- package/docs/rules/valid-title.md +319 -0
- package/index.d.ts +28 -0
- package/lib/globals.json +15 -0
- package/lib/index.js +85 -0
- package/lib/rules/consistent-test-it.js +101 -0
- package/lib/rules/expect-expect.js +102 -0
- package/lib/rules/max-expects.js +70 -0
- package/lib/rules/max-nested-describe.js +59 -0
- package/lib/rules/no-alias-methods.js +62 -0
- package/lib/rules/no-commented-out-tests.js +44 -0
- package/lib/rules/no-conditional-expect.js +80 -0
- package/lib/rules/no-conditional-in-test.js +63 -0
- package/lib/rules/no-confusing-set-timeout.js +61 -0
- package/lib/rules/no-deprecated-functions.js +81 -0
- package/lib/rules/no-disabled-tests.js +57 -0
- package/lib/rules/no-done-callback.js +120 -0
- package/lib/rules/no-duplicate-hooks.js +52 -0
- package/lib/rules/no-error-equal.js +41 -0
- package/lib/rules/no-export.js +61 -0
- package/lib/rules/no-focused-tests.js +63 -0
- package/lib/rules/no-hooks.js +51 -0
- package/lib/rules/no-identical-title.js +73 -0
- package/lib/rules/no-interpolation-in-snapshots.js +43 -0
- package/lib/rules/no-jasmine-globals.js +140 -0
- package/lib/rules/no-large-snapshots.js +100 -0
- package/lib/rules/no-mocks-import.js +46 -0
- package/lib/rules/no-restricted-jest-methods.js +53 -0
- package/lib/rules/no-restricted-matchers.js +60 -0
- package/lib/rules/no-standalone-expect.js +114 -0
- package/lib/rules/no-test-prefixes.js +48 -0
- package/lib/rules/no-test-return-statement.js +62 -0
- package/lib/rules/no-unnecessary-assertion.js +87 -0
- package/lib/rules/no-unneeded-async-expect-function.js +57 -0
- package/lib/rules/no-untyped-mock-factory.js +61 -0
- package/lib/rules/padding-around-after-all-blocks.js +17 -0
- package/lib/rules/padding-around-after-each-blocks.js +17 -0
- package/lib/rules/padding-around-all.js +15 -0
- package/lib/rules/padding-around-before-all-blocks.js +17 -0
- package/lib/rules/padding-around-before-each-blocks.js +17 -0
- package/lib/rules/padding-around-describe-blocks.js +17 -0
- package/lib/rules/padding-around-expect-groups.js +21 -0
- package/lib/rules/padding-around-test-blocks.js +17 -0
- package/lib/rules/prefer-called-with.js +47 -0
- package/lib/rules/prefer-comparison-matcher.js +111 -0
- package/lib/rules/prefer-each.js +77 -0
- package/lib/rules/prefer-ending-with-an-expect.js +102 -0
- package/lib/rules/prefer-equality-matcher.js +87 -0
- package/lib/rules/prefer-expect-assertions.js +237 -0
- package/lib/rules/prefer-expect-resolves.js +47 -0
- package/lib/rules/prefer-hooks-in-order.js +67 -0
- package/lib/rules/prefer-hooks-on-top.js +41 -0
- package/lib/rules/prefer-importing-jest-globals.js +126 -0
- package/lib/rules/prefer-jest-mocked.js +66 -0
- package/lib/rules/prefer-lowercase-title.js +128 -0
- package/lib/rules/prefer-mock-promise-shorthand.js +88 -0
- package/lib/rules/prefer-mock-return-shorthand.js +122 -0
- package/lib/rules/prefer-snapshot-hint.js +111 -0
- package/lib/rules/prefer-spy-on.js +80 -0
- package/lib/rules/prefer-strict-equal.js +46 -0
- package/lib/rules/prefer-to-be.js +98 -0
- package/lib/rules/prefer-to-contain.js +82 -0
- package/lib/rules/prefer-to-have-been-called-times.js +70 -0
- package/lib/rules/prefer-to-have-been-called.js +59 -0
- package/lib/rules/prefer-to-have-length.js +57 -0
- package/lib/rules/prefer-todo.js +72 -0
- package/lib/rules/require-hook.js +93 -0
- package/lib/rules/require-to-throw-message.js +45 -0
- package/lib/rules/require-top-level-describe.js +86 -0
- package/lib/rules/unbound-method.js +86 -0
- package/lib/rules/utils/accessors.js +127 -0
- package/lib/rules/utils/ast-utils.js +59 -0
- package/lib/rules/utils/detectJestVersion.js +24 -0
- package/lib/rules/utils/followTypeAssertionChain.js +10 -0
- package/lib/rules/utils/index.js +71 -0
- package/lib/rules/utils/misc.js +168 -0
- package/lib/rules/utils/padding.js +308 -0
- package/lib/rules/utils/parseJestFnCall.js +325 -0
- package/lib/rules/utils/ts.js +73 -0
- package/lib/rules/valid-describe-callback.js +94 -0
- package/lib/rules/valid-expect-in-promise.js +311 -0
- package/lib/rules/valid-expect-with-promise.js +54 -0
- package/lib/rules/valid-expect.js +302 -0
- package/lib/rules/valid-mock-module-path.js +90 -0
- package/lib/rules/valid-title.js +252 -0
- package/package.json +164 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# jest/prefer-snapshot-hint
|
|
2
|
+
|
|
3
|
+
📝 Prefer including a hint with external snapshots.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
When working with external snapshot matchers it's considered best practice to
|
|
8
|
+
provide a hint (as the last argument to the matcher) describing the expected
|
|
9
|
+
snapshot content that will be included in the snapshots name by Jest.
|
|
10
|
+
|
|
11
|
+
This makes it easier for reviewers to verify the snapshots during review, and
|
|
12
|
+
for anyone to know whether an outdated snapshot is the correct behavior before
|
|
13
|
+
updating.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
This rule looks for any use of an external snapshot matcher (e.g.
|
|
18
|
+
`toMatchSnapshot` and `toThrowErrorMatchingSnapshot`) and checks if they include
|
|
19
|
+
a snapshot hint.
|
|
20
|
+
|
|
21
|
+
## Options
|
|
22
|
+
|
|
23
|
+
### `'always'`
|
|
24
|
+
|
|
25
|
+
Require a hint to _always_ be provided when using external snapshot matchers.
|
|
26
|
+
|
|
27
|
+
Examples of **incorrect** code for the `'always'` option:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
31
|
+
expect(stdout).toMatchSnapshot();
|
|
32
|
+
expect(stderr).toMatchSnapshot();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('cli', () => {
|
|
36
|
+
describe('--version flag', () => {
|
|
37
|
+
it('prints the version', async () => {
|
|
38
|
+
snapshotOutput(await runCli(['--version']));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('--config flag', () => {
|
|
43
|
+
it('reads the config', async () => {
|
|
44
|
+
const { stdout, parsedConfig } = await runCli([
|
|
45
|
+
'--config',
|
|
46
|
+
'jest.config.js',
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
expect(stdout).toMatchSnapshot();
|
|
50
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('prints nothing to stderr', async () => {
|
|
54
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
55
|
+
|
|
56
|
+
expect(stderr).toMatchSnapshot();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('when the file does not exist', () => {
|
|
60
|
+
it('throws an error', async () => {
|
|
61
|
+
await expect(
|
|
62
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
63
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Examples of **correct** code for the `'always'` option:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
74
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
75
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
describe('cli', () => {
|
|
79
|
+
describe('--version flag', () => {
|
|
80
|
+
it('prints the version', async () => {
|
|
81
|
+
snapshotOutput(await runCli(['--version']), {
|
|
82
|
+
stdout: 'version string',
|
|
83
|
+
stderr: 'empty',
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('--config flag', () => {
|
|
89
|
+
it('reads the config', async () => {
|
|
90
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
91
|
+
|
|
92
|
+
expect(stdout).toMatchSnapshot({}, 'stdout: config settings');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('prints nothing to stderr', async () => {
|
|
96
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
97
|
+
|
|
98
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('when the file does not exist', () => {
|
|
102
|
+
it('throws an error', async () => {
|
|
103
|
+
await expect(
|
|
104
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
105
|
+
).rejects.toThrowErrorMatchingSnapshot('stderr: config error');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `'multi'` (default)
|
|
113
|
+
|
|
114
|
+
Require a hint to be provided when there are multiple external snapshot matchers
|
|
115
|
+
within the scope (meaning it includes nested calls).
|
|
116
|
+
|
|
117
|
+
Examples of **incorrect** code for the `'multi'` option:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
121
|
+
expect(stdout).toMatchSnapshot();
|
|
122
|
+
expect(stderr).toMatchSnapshot();
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
describe('cli', () => {
|
|
126
|
+
describe('--version flag', () => {
|
|
127
|
+
it('prints the version', async () => {
|
|
128
|
+
snapshotOutput(await runCli(['--version']));
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('--config flag', () => {
|
|
133
|
+
it('reads the config', async () => {
|
|
134
|
+
const { stdout, parsedConfig } = await runCli([
|
|
135
|
+
'--config',
|
|
136
|
+
'jest.config.js',
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
expect(stdout).toMatchSnapshot();
|
|
140
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('prints nothing to stderr', async () => {
|
|
144
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
145
|
+
|
|
146
|
+
expect(stderr).toMatchSnapshot();
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Examples of **correct** code for the `'multi'` option:
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
156
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
157
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
describe('cli', () => {
|
|
161
|
+
describe('--version flag', () => {
|
|
162
|
+
it('prints the version', async () => {
|
|
163
|
+
snapshotOutput(await runCli(['--version']), {
|
|
164
|
+
stdout: 'version string',
|
|
165
|
+
stderr: 'empty',
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('--config flag', () => {
|
|
171
|
+
it('reads the config', async () => {
|
|
172
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
173
|
+
|
|
174
|
+
expect(stdout).toMatchSnapshot();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('prints nothing to stderr', async () => {
|
|
178
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
179
|
+
|
|
180
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('when the file does not exist', () => {
|
|
184
|
+
it('throws an error', async () => {
|
|
185
|
+
await expect(
|
|
186
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
187
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# jest/prefer-spy-on
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `jest.spyOn()`.
|
|
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
|
+
When mocking a function by overwriting a property you have to manually restore
|
|
11
|
+
the original implementation when cleaning up. When using `jest.spyOn()` Jest
|
|
12
|
+
keeps track of changes, and they can be restored with `jest.restoreAllMocks()`,
|
|
13
|
+
`mockFn.mockRestore()` or by setting `restoreMocks` to `true` in the Jest
|
|
14
|
+
config.
|
|
15
|
+
|
|
16
|
+
Note: The mock created by `jest.spyOn()` still behaves the same as the original
|
|
17
|
+
function. The original function can be overwritten with
|
|
18
|
+
`mockFn.mockImplementation()` or by some of the
|
|
19
|
+
[other mock functions](https://jestjs.io/docs/en/mock-function-api).
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
Date.now = jest.fn(); // Original behaviour lost, returns undefined
|
|
23
|
+
|
|
24
|
+
jest.spyOn(Date, 'now'); // Turned into a mock function but behaviour hasn't changed
|
|
25
|
+
jest.spyOn(Date, 'now').mockImplementation(() => 10); // Will always return 10
|
|
26
|
+
jest.spyOn(Date, 'now').mockReturnValue(10); // Will always return 10
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Rule details
|
|
30
|
+
|
|
31
|
+
This rule triggers a warning if an object's property is overwritten with a jest
|
|
32
|
+
mock.
|
|
33
|
+
|
|
34
|
+
The following patterns are considered warnings:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
Date.now = jest.fn();
|
|
38
|
+
Date.now = jest.fn(() => 10);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
These patterns would not be considered warnings:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
jest.spyOn(Date, 'now');
|
|
45
|
+
jest.spyOn(Date, 'now').mockImplementation(() => 10);
|
|
46
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# jest/prefer-strict-equal
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toStrictEqual()`.
|
|
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
|
+
`toStrictEqual` not only checks that two objects contain the same data but also
|
|
11
|
+
that they have the same structure. It is common to expect objects to not only
|
|
12
|
+
have identical values but also to have identical keys. A stricter equality will
|
|
13
|
+
catch cases where two objects do not have identical keys.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
This rule triggers a warning if `toEqual()` is used to assert equality.
|
|
18
|
+
|
|
19
|
+
The following pattern is considered warning:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The following pattern is not warning:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false
|
|
29
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# jest/prefer-to-be
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toBe()` for primitive literals.
|
|
4
|
+
|
|
5
|
+
💼 This rule is enabled in the 🎨 `style`
|
|
6
|
+
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
7
|
+
|
|
8
|
+
🔧 This rule is automatically fixable by the
|
|
9
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
10
|
+
|
|
11
|
+
<!-- end auto-generated rule header -->
|
|
12
|
+
|
|
13
|
+
When asserting against primitive literals such as numbers and strings, the
|
|
14
|
+
equality matchers all operate the same, but read slightly differently in code.
|
|
15
|
+
|
|
16
|
+
This rule recommends using the `toBe` matcher in these situations, as it forms
|
|
17
|
+
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
|
|
18
|
+
rule recommends using their specific `toBe` matchers, as they give better error
|
|
19
|
+
messages as well.
|
|
20
|
+
|
|
21
|
+
## Rule details
|
|
22
|
+
|
|
23
|
+
This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to
|
|
24
|
+
assert a primitive literal value such as numbers, strings, and booleans.
|
|
25
|
+
|
|
26
|
+
The following patterns are considered warnings:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
expect(value).not.toEqual(5);
|
|
30
|
+
expect(getMessage()).toStrictEqual('hello world');
|
|
31
|
+
expect(loadMessage()).resolves.toEqual('hello world');
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The following pattern is not warning:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
expect(value).not.toBe(5);
|
|
38
|
+
expect(getMessage()).toBe('hello world');
|
|
39
|
+
expect(loadMessage()).resolves.toBe('hello world');
|
|
40
|
+
expect(didError).not.toBe(true);
|
|
41
|
+
|
|
42
|
+
expect(catchError()).toStrictEqual({ message: 'oh noes!' });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
|
|
46
|
+
used to assert against those literal values instead of their more specific
|
|
47
|
+
`toBe` counterparts:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
expect(value).not.toBe(undefined);
|
|
51
|
+
expect(getMessage()).toBe(null);
|
|
52
|
+
expect(countMessages()).resolves.not.toBe(NaN);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The following pattern is not warning:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
expect(value).toBeDefined();
|
|
59
|
+
expect(getMessage()).toBeNull();
|
|
60
|
+
expect(countMessages()).resolves.not.toBeNaN();
|
|
61
|
+
|
|
62
|
+
expect(catchError()).toStrictEqual({ message: undefined });
|
|
63
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# jest/prefer-to-contain
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toContain()`.
|
|
4
|
+
|
|
5
|
+
💼 This rule is enabled in the 🎨 `style`
|
|
6
|
+
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
7
|
+
|
|
8
|
+
🔧 This rule is automatically fixable by the
|
|
9
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
10
|
+
|
|
11
|
+
<!-- end auto-generated rule header -->
|
|
12
|
+
|
|
13
|
+
In order to have a better failure message, `toContain()` should be used upon
|
|
14
|
+
asserting expectations on an array containing an object.
|
|
15
|
+
|
|
16
|
+
## Rule details
|
|
17
|
+
|
|
18
|
+
This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
|
|
19
|
+
used to assert object inclusion in an array
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
expect(a.includes(b)).toBe(true);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
expect(a.includes(b)).not.toBe(true);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
expect(a.includes(b)).toBe(false);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The following patterns are considered warnings:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
expect(a.includes(b)).toBe(true);
|
|
37
|
+
|
|
38
|
+
expect(a.includes(b)).not.toBe(true);
|
|
39
|
+
|
|
40
|
+
expect(a.includes(b)).toBe(false);
|
|
41
|
+
|
|
42
|
+
expect(a.includes(b)).toEqual(true);
|
|
43
|
+
|
|
44
|
+
expect(a.includes(b)).toStrictEqual(true);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The following patterns are not considered warnings:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
expect(a).toContain(b);
|
|
51
|
+
|
|
52
|
+
expect(a).not.toContain(b);
|
|
53
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# jest/prefer-to-have-been-called-times
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toHaveBeenCalledTimes()`.
|
|
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
|
+
In order to have a better failure message, `toHaveBeenCalledTimes` should be
|
|
11
|
+
used instead of directly checking the length of `mock.calls`.
|
|
12
|
+
|
|
13
|
+
## Rule details
|
|
14
|
+
|
|
15
|
+
This rule triggers a warning if `toHaveLength` is used to assert the number of
|
|
16
|
+
times a mock is called.
|
|
17
|
+
|
|
18
|
+
> [!NOTE]
|
|
19
|
+
>
|
|
20
|
+
> This rule should ideally be paired with
|
|
21
|
+
> [`prefer-to-have-length`](./prefer-to-have-length.md)
|
|
22
|
+
|
|
23
|
+
The following patterns are considered warnings:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
expect(someFunction.mock.calls).toHaveLength(1);
|
|
27
|
+
expect(someFunction.mock.calls).toHaveLength(0);
|
|
28
|
+
|
|
29
|
+
expect(someFunction.mock.calls).not.toHaveLength(1);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The following patterns are not warnings:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
expect(someFunction).toHaveBeenCalledTimes(1);
|
|
36
|
+
expect(someFunction).toHaveBeenCalledTimes(0);
|
|
37
|
+
|
|
38
|
+
expect(someFunction).not.toHaveBeenCalledTimes(0);
|
|
39
|
+
|
|
40
|
+
expect(uncalledFunction).not.toBeCalled();
|
|
41
|
+
|
|
42
|
+
expect(method.mock.calls[0][0]).toStrictEqual(value);
|
|
43
|
+
```
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# jest/prefer-to-have-been-called
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toHaveBeenCalled`.
|
|
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
|
+
In order to have a better failure message, `toHaveBeenCalled` should be used
|
|
11
|
+
instead of checking the number of times a mock has been called.
|
|
12
|
+
|
|
13
|
+
## Rule details
|
|
14
|
+
|
|
15
|
+
This rule triggers a warning if `toHaveBeenCalledTimes` is used to assert that a
|
|
16
|
+
mock has or has not been called zero times
|
|
17
|
+
|
|
18
|
+
> [!NOTE]
|
|
19
|
+
>
|
|
20
|
+
> This rule should ideally be paired with
|
|
21
|
+
> [`prefer-to-have-been-called-times`](./prefer-to-have-been-called-times.md)
|
|
22
|
+
|
|
23
|
+
The following patterns are considered warnings:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
expect(someFunction).toHaveBeenCalledTimes(0);
|
|
27
|
+
expect(someFunction).not.toHaveBeenCalledTimes(0);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The following patterns are not warnings:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
expect(someFunction).not.toHaveBeenCalled();
|
|
34
|
+
expect(someFunction).toHaveBeenCalled();
|
|
35
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# jest/prefer-to-have-length
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `toHaveLength()`.
|
|
4
|
+
|
|
5
|
+
💼 This rule is enabled in the 🎨 `style`
|
|
6
|
+
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
7
|
+
|
|
8
|
+
🔧 This rule is automatically fixable by the
|
|
9
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
10
|
+
|
|
11
|
+
<!-- end auto-generated rule header -->
|
|
12
|
+
|
|
13
|
+
In order to have a better failure message, `toHaveLength()` should be used upon
|
|
14
|
+
asserting expectations on objects length property.
|
|
15
|
+
|
|
16
|
+
## Rule details
|
|
17
|
+
|
|
18
|
+
This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
|
|
19
|
+
used to assert objects length property.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
expect(files.length).toBe(1);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This rule is enabled by default.
|
|
26
|
+
|
|
27
|
+
The following patterns are considered warnings:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
expect(files.length).toBe(1);
|
|
31
|
+
|
|
32
|
+
expect(files.length).toEqual(1);
|
|
33
|
+
|
|
34
|
+
expect(files.length).toStrictEqual(1);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The following pattern is not warning:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
expect(files).toHaveLength(1);
|
|
41
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# jest/prefer-todo
|
|
2
|
+
|
|
3
|
+
📝 Suggest using `test.todo`.
|
|
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
|
+
When test cases are empty then it is better to mark them as `test.todo` as it
|
|
11
|
+
will be highlighted in the summary output.
|
|
12
|
+
|
|
13
|
+
## Rule details
|
|
14
|
+
|
|
15
|
+
This rule triggers a warning if empty test cases are used without 'test.todo'.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
test('i need to write this test');
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The following pattern is considered warning:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
test('i need to write this test'); // Unimplemented test case
|
|
25
|
+
test('i need to write this test', () => {}); // Empty test case body
|
|
26
|
+
test.skip('i need to write this test', () => {}); // Empty test case body
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The following pattern is not warning:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
test.todo('i need to write this test');
|
|
33
|
+
```
|