@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,187 @@
|
|
|
1
|
+
# jest/require-hook
|
|
2
|
+
|
|
3
|
+
📝 Require setup and teardown code to be within a hook.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
It's common when writing tests to need to perform setup work that has to happen
|
|
8
|
+
before tests run, and finishing work after tests run.
|
|
9
|
+
|
|
10
|
+
Because Jest executes all `describe` handlers in a test file _before_ it
|
|
11
|
+
executes any of the actual tests, it's important to ensure setup and teardown
|
|
12
|
+
work is done inside `before*` and `after*` handlers respectively, rather than
|
|
13
|
+
inside the `describe` blocks.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
This rule flags any expression that is either at the toplevel of a test file or
|
|
18
|
+
directly within the body of a `describe`, _except_ for the following:
|
|
19
|
+
|
|
20
|
+
- `import` statements
|
|
21
|
+
- `const` variables
|
|
22
|
+
- `let` _declarations_, and initializations to `null` or `undefined`
|
|
23
|
+
- Classes
|
|
24
|
+
- Types
|
|
25
|
+
- Calls to the standard Jest globals
|
|
26
|
+
|
|
27
|
+
This rule flags any function calls within test files that are directly within
|
|
28
|
+
the body of a `describe`, and suggests wrapping them in one of the four
|
|
29
|
+
lifecycle hooks.
|
|
30
|
+
|
|
31
|
+
Here is a slightly contrived test file showcasing some common cases that would
|
|
32
|
+
be flagged:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { database, isCity } from '../database';
|
|
36
|
+
import { Logger } from '../../../src/Logger';
|
|
37
|
+
import { loadCities } from '../api';
|
|
38
|
+
|
|
39
|
+
jest.mock('../api');
|
|
40
|
+
|
|
41
|
+
const initializeCityDatabase = () => {
|
|
42
|
+
database.addCity('Vienna');
|
|
43
|
+
database.addCity('San Juan');
|
|
44
|
+
database.addCity('Wellington');
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const clearCityDatabase = () => {
|
|
48
|
+
database.clear();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
initializeCityDatabase();
|
|
52
|
+
|
|
53
|
+
test('that persists cities', () => {
|
|
54
|
+
expect(database.cities.length).toHaveLength(3);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('city database has Vienna', () => {
|
|
58
|
+
expect(isCity('Vienna')).toBeTruthy();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('city database has San Juan', () => {
|
|
62
|
+
expect(isCity('San Juan')).toBeTruthy();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('when loading cities from the api', () => {
|
|
66
|
+
let consoleWarnSpy = jest.spyOn(console, 'warn');
|
|
67
|
+
|
|
68
|
+
loadCities.mockResolvedValue(['Wellington', 'London']);
|
|
69
|
+
|
|
70
|
+
it('does not duplicate cities', async () => {
|
|
71
|
+
await database.loadCities();
|
|
72
|
+
|
|
73
|
+
expect(database.cities).toHaveLength(4);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('logs any duplicates', async () => {
|
|
77
|
+
await database.loadCities();
|
|
78
|
+
|
|
79
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
80
|
+
'Ignored duplicate cities: Wellington',
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
clearCityDatabase();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Here is the same slightly contrived test file showcasing the same common cases
|
|
89
|
+
but in ways that would be **not** flagged:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
import { database, isCity } from '../database';
|
|
93
|
+
import { Logger } from '../../../src/Logger';
|
|
94
|
+
import { loadCities } from '../api';
|
|
95
|
+
|
|
96
|
+
jest.mock('../api');
|
|
97
|
+
|
|
98
|
+
const initializeCityDatabase = () => {
|
|
99
|
+
database.addCity('Vienna');
|
|
100
|
+
database.addCity('San Juan');
|
|
101
|
+
database.addCity('Wellington');
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const clearCityDatabase = () => {
|
|
105
|
+
database.clear();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
initializeCityDatabase();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('that persists cities', () => {
|
|
113
|
+
expect(database.cities.length).toHaveLength(3);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('city database has Vienna', () => {
|
|
117
|
+
expect(isCity('Vienna')).toBeTruthy();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('city database has San Juan', () => {
|
|
121
|
+
expect(isCity('San Juan')).toBeTruthy();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('when loading cities from the api', () => {
|
|
125
|
+
let consoleWarnSpy;
|
|
126
|
+
|
|
127
|
+
beforeEach(() => {
|
|
128
|
+
consoleWarnSpy = jest.spyOn(console, 'warn');
|
|
129
|
+
loadCities.mockResolvedValue(['Wellington', 'London']);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('does not duplicate cities', async () => {
|
|
133
|
+
await database.loadCities();
|
|
134
|
+
|
|
135
|
+
expect(database.cities).toHaveLength(4);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('logs any duplicates', async () => {
|
|
139
|
+
await database.loadCities();
|
|
140
|
+
|
|
141
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
142
|
+
'Ignored duplicate cities: Wellington',
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
afterEach(() => {
|
|
148
|
+
clearCityDatabase();
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Options
|
|
153
|
+
|
|
154
|
+
If there are methods that you want to call outside of hooks and tests, you can
|
|
155
|
+
mark them as allowed using the `allowedFunctionCalls` option.
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"jest/require-hook": [
|
|
160
|
+
"error",
|
|
161
|
+
{
|
|
162
|
+
"allowedFunctionCalls": ["enableAutoDestroy"]
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Examples of **correct** code when using
|
|
169
|
+
`{ "allowedFunctionCalls": ["enableAutoDestroy"] }` option:
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
/* eslint jest/require-hook: ["error", { "allowedFunctionCalls": ["enableAutoDestroy"] }] */
|
|
173
|
+
|
|
174
|
+
import { enableAutoDestroy, mount } from '@vue/test-utils';
|
|
175
|
+
import { initDatabase, tearDownDatabase } from './databaseUtils';
|
|
176
|
+
|
|
177
|
+
enableAutoDestroy(afterEach);
|
|
178
|
+
|
|
179
|
+
beforeEach(initDatabase);
|
|
180
|
+
afterEach(tearDownDatabase);
|
|
181
|
+
|
|
182
|
+
describe('Foo', () => {
|
|
183
|
+
test('always returns 42', () => {
|
|
184
|
+
expect(global.getAnswer()).toBe(42);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# jest/require-to-throw-message
|
|
2
|
+
|
|
3
|
+
📝 Require a message for `toThrow()`.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
`toThrow()` (and its alias `toThrowError()`) is used to check if an error is
|
|
8
|
+
thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if
|
|
9
|
+
no message is defined, then the test will pass for any thrown error. Requiring a
|
|
10
|
+
message ensures that the intended error is thrown.
|
|
11
|
+
|
|
12
|
+
## Rule details
|
|
13
|
+
|
|
14
|
+
This rule triggers a warning if `toThrow()` or `toThrowError()` is used without
|
|
15
|
+
an error message.
|
|
16
|
+
|
|
17
|
+
The following patterns are considered warnings:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
test('all the things', async () => {
|
|
21
|
+
expect(() => a()).toThrow();
|
|
22
|
+
|
|
23
|
+
expect(() => a()).toThrowError();
|
|
24
|
+
|
|
25
|
+
await expect(a()).rejects.toThrow();
|
|
26
|
+
|
|
27
|
+
await expect(a()).rejects.toThrowError();
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The following patterns are not considered warnings:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
test('all the things', async () => {
|
|
35
|
+
expect(() => a()).toThrow('a');
|
|
36
|
+
|
|
37
|
+
expect(() => a()).toThrowError('a');
|
|
38
|
+
|
|
39
|
+
await expect(a()).rejects.toThrow('a');
|
|
40
|
+
|
|
41
|
+
await expect(a()).rejects.toThrowError('a');
|
|
42
|
+
});
|
|
43
|
+
```
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# jest/require-top-level-describe
|
|
2
|
+
|
|
3
|
+
📝 Require test cases and hooks to be inside a `describe` block.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
Jest allows you to organise your test files the way you want it. However, the
|
|
8
|
+
more your codebase grows, the more it becomes hard to navigate in your test
|
|
9
|
+
files. This rule makes sure you provide at least a top-level `describe` block in
|
|
10
|
+
your test file.
|
|
11
|
+
|
|
12
|
+
## Rule details
|
|
13
|
+
|
|
14
|
+
This rule triggers a warning if a test case (`test` and `it`) or a hook
|
|
15
|
+
(`beforeAll`, `beforeEach`, `afterEach`, `afterAll`) is not located in a
|
|
16
|
+
top-level `describe` block.
|
|
17
|
+
|
|
18
|
+
The following patterns are considered warnings:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
// Above a describe block
|
|
22
|
+
test('my test', () => {});
|
|
23
|
+
describe('test suite', () => {
|
|
24
|
+
it('test', () => {});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Below a describe block
|
|
28
|
+
describe('test suite', () => {});
|
|
29
|
+
test('my test', () => {});
|
|
30
|
+
|
|
31
|
+
// Same for hooks
|
|
32
|
+
beforeAll('my beforeAll', () => {});
|
|
33
|
+
describe('test suite', () => {});
|
|
34
|
+
afterEach('my afterEach', () => {});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The following patterns are **not** considered warnings:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// In a describe block
|
|
41
|
+
describe('test suite', () => {
|
|
42
|
+
test('my test', () => {});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// In a nested describe block
|
|
46
|
+
describe('test suite', () => {
|
|
47
|
+
test('my test', () => {});
|
|
48
|
+
describe('another test suite', () => {
|
|
49
|
+
test('my other test', () => {});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
You can also enforce a limit on the number of describes allowed at the top-level
|
|
57
|
+
using the `maxNumberOfTopLevelDescribes` option:
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"jest/require-top-level-describe": [
|
|
62
|
+
"error",
|
|
63
|
+
{
|
|
64
|
+
"maxNumberOfTopLevelDescribes": 2
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Examples of **incorrect** code with the above config:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
describe('test suite', () => {
|
|
74
|
+
it('test', () => {});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('test suite', () => {});
|
|
78
|
+
|
|
79
|
+
describe('test suite', () => {});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This option defaults to `Infinity`, allowing any number of top-level describes.
|
|
83
|
+
|
|
84
|
+
## When Not To Use It
|
|
85
|
+
|
|
86
|
+
Don't use this rule on non-jest test files.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# jest/unbound-method
|
|
2
|
+
|
|
3
|
+
📝 Enforce unbound methods are called with their expected scope.
|
|
4
|
+
|
|
5
|
+
💭 This rule requires
|
|
6
|
+
[type information](https://typescript-eslint.io/linting/typed-linting).
|
|
7
|
+
|
|
8
|
+
<!-- end auto-generated rule header -->
|
|
9
|
+
|
|
10
|
+
## Rule details
|
|
11
|
+
|
|
12
|
+
This rule extends the base [`@typescript-eslint/unbound-method`][original-rule]
|
|
13
|
+
rule, meaning you must depend on `@typescript-eslint/eslint-plugin` for it to
|
|
14
|
+
work. It adds support for understanding when it's ok to pass an unbound method
|
|
15
|
+
to `expect` calls.
|
|
16
|
+
|
|
17
|
+
See the [`@typescript-eslint` documentation][original-rule] for more details on
|
|
18
|
+
the `unbound-method` rule.
|
|
19
|
+
|
|
20
|
+
Note that while this rule requires type information to work, it will fail
|
|
21
|
+
silently when not available allowing you to safely enable it on projects that
|
|
22
|
+
are not using TypeScript.
|
|
23
|
+
|
|
24
|
+
## How to use
|
|
25
|
+
|
|
26
|
+
```json5
|
|
27
|
+
{
|
|
28
|
+
parser: '@typescript-eslint/parser',
|
|
29
|
+
parserOptions: {
|
|
30
|
+
project: 'tsconfig.json',
|
|
31
|
+
ecmaVersion: 2020,
|
|
32
|
+
sourceType: 'module',
|
|
33
|
+
},
|
|
34
|
+
overrides: [
|
|
35
|
+
{
|
|
36
|
+
files: ['test/**'],
|
|
37
|
+
plugins: ['jest'],
|
|
38
|
+
rules: {
|
|
39
|
+
// you should turn the original rule off *only* for test files
|
|
40
|
+
'@typescript-eslint/unbound-method': 'off',
|
|
41
|
+
'jest/unbound-method': 'error',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
rules: {
|
|
46
|
+
'@typescript-eslint/unbound-method': 'error',
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This rule should be applied to your test files in place of the original rule,
|
|
52
|
+
which should be applied to the rest of your codebase.
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
See [`@typescript-eslint/unbound-method`][original-rule] options (e.g.
|
|
57
|
+
`ignoreStatic`).
|
|
58
|
+
|
|
59
|
+
<sup>Taken with ❤️ [from `@typescript-eslint` core][original-rule]</sup>
|
|
60
|
+
|
|
61
|
+
[original-rule]: https://typescript-eslint.io/rules/unbound-method
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# jest/valid-describe-callback
|
|
2
|
+
|
|
3
|
+
📝 Enforce valid `describe()` callback.
|
|
4
|
+
|
|
5
|
+
💼 This rule is enabled in the ✅ `recommended`
|
|
6
|
+
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
7
|
+
|
|
8
|
+
<!-- end auto-generated rule header -->
|
|
9
|
+
|
|
10
|
+
Using an improper `describe()` callback function can lead to unexpected test
|
|
11
|
+
errors.
|
|
12
|
+
|
|
13
|
+
## Rule details
|
|
14
|
+
|
|
15
|
+
This rule validates that the second parameter of a `describe()` function is a
|
|
16
|
+
callback function. This callback function:
|
|
17
|
+
|
|
18
|
+
- should not be
|
|
19
|
+
[async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
|
|
20
|
+
- should not contain any parameters
|
|
21
|
+
- should not contain any `return` statements
|
|
22
|
+
|
|
23
|
+
The following `describe` function aliases are also validated:
|
|
24
|
+
|
|
25
|
+
- `describe`
|
|
26
|
+
- `describe.only`
|
|
27
|
+
- `describe.skip`
|
|
28
|
+
- `fdescribe`
|
|
29
|
+
- `xdescribe`
|
|
30
|
+
|
|
31
|
+
The following patterns are considered warnings:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
// Async callback functions are not allowed
|
|
35
|
+
describe('myFunction()', async () => {
|
|
36
|
+
// ...
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Callback function parameters are not allowed
|
|
40
|
+
describe('myFunction()', done => {
|
|
41
|
+
// ...
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
//
|
|
45
|
+
describe('myFunction', () => {
|
|
46
|
+
// No return statements are allowed in block of a callback function
|
|
47
|
+
return Promise.resolve().then(() => {
|
|
48
|
+
it('breaks', () => {
|
|
49
|
+
throw new Error('Fail');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Returning a value from a describe block is not allowed
|
|
55
|
+
describe('myFunction', () =>
|
|
56
|
+
it('returns a truthy value', () => {
|
|
57
|
+
expect(myFunction()).toBeTruthy();
|
|
58
|
+
}));
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The following patterns are not considered warnings:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
describe('myFunction()', () => {
|
|
65
|
+
it('returns a truthy value', () => {
|
|
66
|
+
expect(myFunction()).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# jest/valid-expect-in-promise
|
|
2
|
+
|
|
3
|
+
📝 Require promises that have expectations in their chain to be valid.
|
|
4
|
+
|
|
5
|
+
💼 This rule is enabled in the ✅ `recommended`
|
|
6
|
+
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
7
|
+
|
|
8
|
+
<!-- end auto-generated rule header -->
|
|
9
|
+
|
|
10
|
+
Ensure promises that include expectations are returned or awaited.
|
|
11
|
+
|
|
12
|
+
## Rule details
|
|
13
|
+
|
|
14
|
+
This rule flags any promises within the body of a test that include expectations
|
|
15
|
+
that have either not been returned or awaited.
|
|
16
|
+
|
|
17
|
+
The following patterns is considered warning:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
it('promises a person', () => {
|
|
21
|
+
api.getPersonByName('bob').then(person => {
|
|
22
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('promises a counted person', () => {
|
|
27
|
+
const promise = api.getPersonByName('bob').then(person => {
|
|
28
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
promise.then(() => {
|
|
32
|
+
expect(analytics.gottenPeopleCount).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('promises multiple people', () => {
|
|
37
|
+
const firstPromise = api.getPersonByName('bob').then(person => {
|
|
38
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
39
|
+
});
|
|
40
|
+
const secondPromise = api.getPersonByName('alice').then(person => {
|
|
41
|
+
expect(person).toHaveProperty('name', 'Alice');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return Promise.any([firstPromise, secondPromise]);
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The following pattern is not warning:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
it('promises a person', async () => {
|
|
52
|
+
await api.getPersonByName('bob').then(person => {
|
|
53
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('promises a counted person', () => {
|
|
58
|
+
let promise = api.getPersonByName('bob').then(person => {
|
|
59
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
promise = promise.then(() => {
|
|
63
|
+
expect(analytics.gottenPeopleCount).toBe(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return promise;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('promises multiple people', () => {
|
|
70
|
+
const firstPromise = api.getPersonByName('bob').then(person => {
|
|
71
|
+
expect(person).toHaveProperty('name', 'Bob');
|
|
72
|
+
});
|
|
73
|
+
const secondPromise = api.getPersonByName('alice').then(person => {
|
|
74
|
+
expect(person).toHaveProperty('name', 'Alice');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return Promise.allSettled([firstPromise, secondPromise]);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# jest/valid-expect-with-promise
|
|
2
|
+
|
|
3
|
+
📝 Require that `resolve` and `reject` modifiers are present (and only) for
|
|
4
|
+
promise-like types.
|
|
5
|
+
|
|
6
|
+
💭 This rule requires
|
|
7
|
+
[type information](https://typescript-eslint.io/linting/typed-linting).
|
|
8
|
+
|
|
9
|
+
<!-- end auto-generated rule header -->
|
|
10
|
+
|
|
11
|
+
When working with promises, you must remember to use `resolves` and `rejects` to
|
|
12
|
+
assert on the value returned (or thrown) by the promise, rather than the promise
|
|
13
|
+
itself.
|
|
14
|
+
|
|
15
|
+
Inversely, while Jest does not prevent you from using `resolves` and `rejects`
|
|
16
|
+
on non-promise values, it is not necessary.
|
|
17
|
+
|
|
18
|
+
When TypeScript is in use, it is possible to determine when `resolves` and
|
|
19
|
+
`rejects` should and should not be needed.
|
|
20
|
+
|
|
21
|
+
## Rule details
|
|
22
|
+
|
|
23
|
+
This rule warns when:
|
|
24
|
+
|
|
25
|
+
- an `expect` is given a promise-like value but without `resolves` or `rejects`
|
|
26
|
+
- an `expect` is not given a promise-like value, but is used with `resolves` or
|
|
27
|
+
`rejects`
|
|
28
|
+
|
|
29
|
+
The following patterns are considered warnings:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
expect('hello world').resolves.toBe('hello sunshine');
|
|
33
|
+
|
|
34
|
+
expect(new Promise(r => r(0))).toThrow('oh noes!');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The following patterns are not considered warnings:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
expect('hello world').toBe('hello sunshine');
|
|
41
|
+
|
|
42
|
+
expect(new Promise(r => r(0))).rejects.toThrow('oh noes!');
|
|
43
|
+
```
|