@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,62 @@
|
|
|
1
|
+
# jest/prefer-expect-resolves
|
|
2
|
+
|
|
3
|
+
📝 Prefer `await expect(...).resolves` over `expect(await ...)` syntax.
|
|
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 working with promises, there are two primary ways you can test the resolved
|
|
11
|
+
value:
|
|
12
|
+
|
|
13
|
+
1. use the `resolve` modifier on `expect`
|
|
14
|
+
(`await expect(...).resolves.<matcher>` style)
|
|
15
|
+
2. `await` the promise and assert against its result
|
|
16
|
+
(`expect(await ...).<matcher>` style)
|
|
17
|
+
|
|
18
|
+
While the second style is arguably less dependent on `jest`, if the promise
|
|
19
|
+
rejects it will be treated as a general error, resulting in less predictable
|
|
20
|
+
behaviour and output from `jest`.
|
|
21
|
+
|
|
22
|
+
Additionally, favoring the first style ensures consistency with its `rejects`
|
|
23
|
+
counterpart, as there is no way of "awaiting" a rejection.
|
|
24
|
+
|
|
25
|
+
## Rule details
|
|
26
|
+
|
|
27
|
+
This rule triggers a warning if an `await` is done within an `expect`, and
|
|
28
|
+
recommends using `resolves` instead.
|
|
29
|
+
|
|
30
|
+
Examples of **incorrect** code for this rule
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
it('passes', async () => {
|
|
34
|
+
expect(await someValue()).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('is true', async () => {
|
|
38
|
+
const myPromise = Promise.resolve(true);
|
|
39
|
+
|
|
40
|
+
expect(await myPromise).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Examples of **correct** code for this rule
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
it('passes', async () => {
|
|
48
|
+
await expect(someValue()).resolves.toBe(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('is true', async () => {
|
|
52
|
+
const myPromise = Promise.resolve(true);
|
|
53
|
+
|
|
54
|
+
await expect(myPromise).resolves.toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('errors', async () => {
|
|
58
|
+
await expect(Promise.reject(new Error('oh noes!'))).rejects.toThrowError(
|
|
59
|
+
'oh noes!',
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
```
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# jest/prefer-hooks-in-order
|
|
2
|
+
|
|
3
|
+
📝 Prefer having hooks in a consistent order.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
While hooks can be setup in any order, they're always called by `jest` in this
|
|
8
|
+
specific order:
|
|
9
|
+
|
|
10
|
+
1. `beforeAll`
|
|
11
|
+
1. `beforeEach`
|
|
12
|
+
1. `afterEach`
|
|
13
|
+
1. `afterAll`
|
|
14
|
+
|
|
15
|
+
This rule aims to make that more obvious by enforcing grouped hooks be setup in
|
|
16
|
+
that order within tests.
|
|
17
|
+
|
|
18
|
+
## Rule details
|
|
19
|
+
|
|
20
|
+
Examples of **incorrect** code for this rule
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
/* eslint jest/prefer-hooks-in-order: "error" */
|
|
24
|
+
|
|
25
|
+
describe('foo', () => {
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
seedMyDatabase();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
beforeAll(() => {
|
|
31
|
+
createMyDatabase();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('accepts this input', () => {
|
|
35
|
+
// ...
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns that value', () => {
|
|
39
|
+
// ...
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('when the database has specific values', () => {
|
|
43
|
+
const specificValue = '...';
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
seedMyDatabase(specificValue);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('accepts that input', () => {
|
|
50
|
+
// ...
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('throws an error', () => {
|
|
54
|
+
// ...
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
clearLogger();
|
|
59
|
+
});
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
mockLogger();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('logs a message', () => {
|
|
65
|
+
// ...
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
afterAll(() => {
|
|
70
|
+
removeMyDatabase();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Examples of **correct** code for this rule
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
/* eslint jest/prefer-hooks-in-order: "error" */
|
|
79
|
+
|
|
80
|
+
describe('foo', () => {
|
|
81
|
+
beforeAll(() => {
|
|
82
|
+
createMyDatabase();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
beforeEach(() => {
|
|
86
|
+
seedMyDatabase();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('accepts this input', () => {
|
|
90
|
+
// ...
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('returns that value', () => {
|
|
94
|
+
// ...
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('when the database has specific values', () => {
|
|
98
|
+
const specificValue = '...';
|
|
99
|
+
|
|
100
|
+
beforeEach(() => {
|
|
101
|
+
seedMyDatabase(specificValue);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('accepts that input', () => {
|
|
105
|
+
// ...
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('throws an error', () => {
|
|
109
|
+
// ...
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
beforeEach(() => {
|
|
113
|
+
mockLogger();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
afterEach(() => {
|
|
117
|
+
clearLogger();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('logs a message', () => {
|
|
121
|
+
// ...
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
afterAll(() => {
|
|
126
|
+
removeMyDatabase();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Also See
|
|
132
|
+
|
|
133
|
+
- [`prefer-hooks-on-top`](prefer-hooks-on-top.md)
|
|
134
|
+
|
|
135
|
+
## Further Reading
|
|
136
|
+
|
|
137
|
+
- [Order of Execution](https://jestjs.io/docs/setup-teardown#order-of-execution)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# jest/prefer-hooks-on-top
|
|
2
|
+
|
|
3
|
+
📝 Suggest having hooks before any test cases.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
While hooks can be setup anywhere in a test file, they are always called in a
|
|
8
|
+
specific order, which means it can be confusing if they're intermixed with test
|
|
9
|
+
cases.
|
|
10
|
+
|
|
11
|
+
This rule helps to ensure that hooks are always defined before test cases.
|
|
12
|
+
|
|
13
|
+
## Rule details
|
|
14
|
+
|
|
15
|
+
Examples of **incorrect** code for this rule
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
/* eslint jest/prefer-hooks-on-top: "error" */
|
|
19
|
+
|
|
20
|
+
describe('foo', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
seedMyDatabase();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('accepts this input', () => {
|
|
26
|
+
// ...
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
beforeAll(() => {
|
|
30
|
+
createMyDatabase();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('returns that value', () => {
|
|
34
|
+
// ...
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('when the database has specific values', () => {
|
|
38
|
+
const specificValue = '...';
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
seedMyDatabase(specificValue);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('accepts that input', () => {
|
|
45
|
+
// ...
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('throws an error', () => {
|
|
49
|
+
// ...
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
clearLogger();
|
|
54
|
+
});
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
mockLogger();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('logs a message', () => {
|
|
60
|
+
// ...
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterAll(() => {
|
|
65
|
+
removeMyDatabase();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Examples of **correct** code for this rule
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
/* eslint jest/prefer-hooks-on-top: "error" */
|
|
74
|
+
|
|
75
|
+
describe('foo', () => {
|
|
76
|
+
beforeAll(() => {
|
|
77
|
+
createMyDatabase();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
seedMyDatabase();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
afterAll(() => {
|
|
85
|
+
clearMyDatabase();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('accepts this input', () => {
|
|
89
|
+
// ...
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('returns that value', () => {
|
|
93
|
+
// ...
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('when the database has specific values', () => {
|
|
97
|
+
const specificValue = '...';
|
|
98
|
+
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
seedMyDatabase(specificValue);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
beforeEach(() => {
|
|
104
|
+
mockLogger();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
afterEach(() => {
|
|
108
|
+
clearLogger();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('accepts that input', () => {
|
|
112
|
+
// ...
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('throws an error', () => {
|
|
116
|
+
// ...
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('logs a message', () => {
|
|
120
|
+
// ...
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# jest/prefer-importing-jest-globals
|
|
2
|
+
|
|
3
|
+
📝 Prefer importing Jest globals.
|
|
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
|
+
This rule aims to enforce explicit imports from `@jest/globals`.
|
|
11
|
+
|
|
12
|
+
1. This is useful for ensuring that the Jest APIs are imported the same way in
|
|
13
|
+
the codebase.
|
|
14
|
+
2. When you can't modify Jest's
|
|
15
|
+
[`injectGlobals`](https://jestjs.io/docs/configuration#injectglobals-boolean)
|
|
16
|
+
configuration property, this rule can help to ensure that the Jest globals
|
|
17
|
+
are imported explicitly and facilitate a migration to `@jest/globals`.
|
|
18
|
+
|
|
19
|
+
## Rule details
|
|
20
|
+
|
|
21
|
+
Examples of **incorrect** code for this rule
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
/* eslint jest/prefer-importing-jest-globals: "error" */
|
|
25
|
+
|
|
26
|
+
describe('foo', () => {
|
|
27
|
+
it('accepts this input', () => {
|
|
28
|
+
// ...
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Examples of **correct** code for this rule
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
/* eslint jest/prefer-importing-jest-globals: "error" */
|
|
37
|
+
|
|
38
|
+
import { describe, it } from '@jest/globals';
|
|
39
|
+
|
|
40
|
+
describe('foo', () => {
|
|
41
|
+
it('accepts this input', () => {
|
|
42
|
+
// ...
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Options
|
|
48
|
+
|
|
49
|
+
This rule can be configured as follows
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"type": "object",
|
|
54
|
+
"properties": {
|
|
55
|
+
"types": {
|
|
56
|
+
"type": "array",
|
|
57
|
+
"items": {
|
|
58
|
+
"type": "string",
|
|
59
|
+
"enum": ["hook", "describe", "test", "expect", "jest", "unknown"]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"additionalProperties": false
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### types
|
|
68
|
+
|
|
69
|
+
A list of Jest global types to enforce explicit imports for. By default, all
|
|
70
|
+
Jest globals are enforced.
|
|
71
|
+
|
|
72
|
+
This option is useful when you only want to enforce explicit imports for a
|
|
73
|
+
subset of Jest globals. For instance, when migrating to ESM, you might want to
|
|
74
|
+
enforce explicit imports only for the `jest` global, as of
|
|
75
|
+
[Jest's ESM documentation](https://jestjs.io/docs/ecmascript-modules#differences-between-esm-and-commonjs).
|
|
76
|
+
|
|
77
|
+
```json5
|
|
78
|
+
{
|
|
79
|
+
'jest/prefer-importing-jest-globals': ['error', { types: ['jest'] }],
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Further Reading
|
|
84
|
+
|
|
85
|
+
- [Documentation](https://jestjs.io/docs/api)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# jest/prefer-jest-mocked
|
|
2
|
+
|
|
3
|
+
📝 Prefer `jest.mocked()` over `fn as jest.Mock`.
|
|
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 working with mocks of functions using Jest, it's recommended to use the
|
|
11
|
+
`jest.mocked()` helper function to properly type the mocked functions. This rule
|
|
12
|
+
enforces the use of `jest.mocked()` for better type safety and readability.
|
|
13
|
+
|
|
14
|
+
Restricted types:
|
|
15
|
+
|
|
16
|
+
- `jest.Mock`
|
|
17
|
+
- `jest.MockedFunction`
|
|
18
|
+
- `jest.MockedClass`
|
|
19
|
+
- `jest.MockedObject`
|
|
20
|
+
|
|
21
|
+
## Rule details
|
|
22
|
+
|
|
23
|
+
The following patterns are warnings:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
(foo as jest.Mock).mockReturnValue(1);
|
|
27
|
+
const mock = (foo as jest.Mock).mockReturnValue(1);
|
|
28
|
+
(foo as unknown as jest.Mock).mockReturnValue(1);
|
|
29
|
+
(Obj.foo as jest.Mock).mockReturnValue(1);
|
|
30
|
+
([].foo as jest.Mock).mockReturnValue(1);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The following patterns are not warnings:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
jest.mocked(foo).mockReturnValue(1);
|
|
37
|
+
const mock = jest.mocked(foo).mockReturnValue(1);
|
|
38
|
+
jest.mocked(Obj.foo).mockReturnValue(1);
|
|
39
|
+
jest.mocked([].foo).mockReturnValue(1);
|
|
40
|
+
```
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# jest/prefer-lowercase-title
|
|
2
|
+
|
|
3
|
+
📝 Enforce lowercase test names.
|
|
4
|
+
|
|
5
|
+
🔧 This rule is automatically fixable by the
|
|
6
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
7
|
+
|
|
8
|
+
<!-- end auto-generated rule header -->
|
|
9
|
+
|
|
10
|
+
## Rule details
|
|
11
|
+
|
|
12
|
+
Enforce `it`, `test` and `describe` to have descriptions that begin with a
|
|
13
|
+
lowercase letter. This provides more readable test failures. This rule is not
|
|
14
|
+
enabled by default.
|
|
15
|
+
|
|
16
|
+
The following pattern is considered a warning:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
it('Adds 1 + 2 to equal 3', () => {
|
|
20
|
+
expect(sum(1, 2)).toBe(3);
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The following pattern is not considered a warning:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
it('adds 1 + 2 to equal 3', () => {
|
|
28
|
+
expect(sum(1, 2)).toBe(3);
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Options
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{
|
|
36
|
+
"jest/prefer-lowercase-title": [
|
|
37
|
+
"error",
|
|
38
|
+
{
|
|
39
|
+
"ignore": ["describe", "test"]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### `ignore`
|
|
46
|
+
|
|
47
|
+
This array option controls which Jest functions are checked by this rule. There
|
|
48
|
+
are three possible values:
|
|
49
|
+
|
|
50
|
+
- `"describe"`
|
|
51
|
+
- `"test"`
|
|
52
|
+
- `"it"`
|
|
53
|
+
|
|
54
|
+
By default, none of these options are enabled (the equivalent of
|
|
55
|
+
`{ "ignore": [] }`).
|
|
56
|
+
|
|
57
|
+
Example of **correct** code for the `{ "ignore": ["describe"] }` option:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["describe"] }] */
|
|
61
|
+
|
|
62
|
+
describe('Uppercase description');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Example of **correct** code for the `{ "ignore": ["test"] }` option:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["test"] }] */
|
|
69
|
+
|
|
70
|
+
test('Uppercase description');
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Example of **correct** code for the `{ "ignore": ["it"] }` option:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["it"] }] */
|
|
77
|
+
|
|
78
|
+
it('Uppercase description');
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### `allowedPrefixes`
|
|
82
|
+
|
|
83
|
+
This array option allows specifying prefixes, which contain capitals that titles
|
|
84
|
+
can start with. This can be useful when writing tests for API endpoints, where
|
|
85
|
+
you'd like to prefix with the HTTP method.
|
|
86
|
+
|
|
87
|
+
By default, nothing is allowed (the equivalent of `{ "allowedPrefixes": [] }`).
|
|
88
|
+
|
|
89
|
+
Example of **correct** code for the `{ "allowedPrefixes": ["GET"] }` option:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "allowedPrefixes": ["GET"] }] */
|
|
93
|
+
|
|
94
|
+
describe('GET /live');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### `ignoreTopLevelDescribe`
|
|
98
|
+
|
|
99
|
+
This option can be set to allow only the top-level `describe` blocks to have a
|
|
100
|
+
title starting with an upper-case letter.
|
|
101
|
+
|
|
102
|
+
Example of **correct** code for the `{ "ignoreTopLevelDescribe": true }` option:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignoreTopLevelDescribe": true }] */
|
|
106
|
+
describe('MyClass', () => {
|
|
107
|
+
describe('#myMethod', () => {
|
|
108
|
+
it('does things', () => {
|
|
109
|
+
//
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### `ignoreTodos`
|
|
116
|
+
|
|
117
|
+
This option is used to control whether
|
|
118
|
+
[`todo`](https://jestjs.io/docs/api#testtodoname) Jest functions to be checked
|
|
119
|
+
by this rule. By the default, the option is set to false.
|
|
120
|
+
|
|
121
|
+
Example of **correct** code for the `{ "ignoreTodos": true }` option:
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignoreTodos": true }] */
|
|
125
|
+
test.todo('Uppercase description');
|
|
126
|
+
|
|
127
|
+
it.todo('Uppercase description');
|
|
128
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# jest/prefer-mock-promise-shorthand
|
|
2
|
+
|
|
3
|
+
📝 Prefer mock resolved/rejected shorthands for promises.
|
|
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 working with mocks of functions that return promises, Jest provides some
|
|
11
|
+
API sugar functions to reduce the amount of boilerplate you have to write.
|
|
12
|
+
|
|
13
|
+
These methods should be preferred when possible.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
The following patterns are warnings:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
jest.fn().mockImplementation(() => Promise.resolve(123));
|
|
21
|
+
jest
|
|
22
|
+
.spyOn(fs.promises, 'readFile')
|
|
23
|
+
.mockReturnValue(Promise.reject(new Error('oh noes!')));
|
|
24
|
+
|
|
25
|
+
myFunction
|
|
26
|
+
.mockReturnValueOnce(Promise.resolve(42))
|
|
27
|
+
.mockImplementationOnce(() => Promise.resolve(42))
|
|
28
|
+
.mockReturnValue(Promise.reject(new Error('too many calls!')));
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The following patterns are not warnings:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
jest.fn().mockResolvedValue(123);
|
|
35
|
+
jest.spyOn(fs.promises, 'readFile').mockRejectedValue(new Error('oh noes!'));
|
|
36
|
+
|
|
37
|
+
myFunction
|
|
38
|
+
.mockResolvedValueOnce(42)
|
|
39
|
+
.mockResolvedValueOnce(42)
|
|
40
|
+
.mockRejectedValue(new Error('too many calls!'));
|
|
41
|
+
```
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# jest/prefer-mock-return-shorthand
|
|
2
|
+
|
|
3
|
+
📝 Prefer mock return shorthands.
|
|
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 working with mocks of functions that return simple values, Jest provides
|
|
11
|
+
some API sugar functions to reduce the amount of boilerplate you have to write.
|
|
12
|
+
|
|
13
|
+
These methods should be preferred when possible.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
The following patterns are warnings:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
jest.fn().mockImplementation(() => 'hello world');
|
|
21
|
+
|
|
22
|
+
jest
|
|
23
|
+
.spyOn(fs.promises, 'readFile')
|
|
24
|
+
.mockImplementationOnce(() => Promise.reject(new Error('oh noes!')));
|
|
25
|
+
|
|
26
|
+
myFunction
|
|
27
|
+
.mockImplementationOnce(() => 42)
|
|
28
|
+
.mockImplementationOnce(() => Promise.resolve(42))
|
|
29
|
+
.mockReturnValue(0);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The following patterns are not warnings:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
jest.fn().mockResolvedValue(123);
|
|
36
|
+
|
|
37
|
+
jest
|
|
38
|
+
.spyOn(fs.promises, 'readFile')
|
|
39
|
+
.mockReturnValue(Promise.reject(new Error('oh noes!')));
|
|
40
|
+
jest.spyOn(fs.promises, 'readFile').mockRejectedValue(new Error('oh noes!'));
|
|
41
|
+
|
|
42
|
+
jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
|
|
43
|
+
throw new Error('oh noes!');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
myFunction
|
|
47
|
+
.mockResolvedValueOnce(42)
|
|
48
|
+
.mockResolvedValueOnce(42)
|
|
49
|
+
.mockReturnValue(0);
|
|
50
|
+
```
|