eslint-plugin-jest 21.23.0 → 21.24.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 +2 -0
- package/docs/rules/no-alias-methods.md +46 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/rules/__tests__/no-alias-methods.test.js +157 -0
- package/rules/__tests__/prefer-strict-equal.test.js +4 -1
- package/rules/__tests__/require-tothrow-message.test.js +52 -0
- package/rules/no-alias-methods.js +57 -0
- package/rules/prefer-strict-equal.js +10 -3
- package/rules/require-tothrow-message.js +10 -3
- package/rules/__tests__/require-tothrow-message.js +0 -32
package/README.md
CHANGED
|
@@ -83,6 +83,7 @@ for more information about extending configuration files.
|
|
|
83
83
|
| [consistent-test-it][] | Enforce consistent test or it keyword | | ![fixable-green][] |
|
|
84
84
|
| [expect-expect][] | Enforce assertion to be made in a test body | | |
|
|
85
85
|
| [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
|
|
86
|
+
| [no-alias-methods][] | Disallow alias methods | | |
|
|
86
87
|
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
|
|
87
88
|
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
|
|
88
89
|
| [no-hooks][] | Disallow setup and teardown hooks | | |
|
|
@@ -111,6 +112,7 @@ for more information about extending configuration files.
|
|
|
111
112
|
[consistent-test-it]: docs/rules/consistent-test-it.md
|
|
112
113
|
[expect-expect]: docs/rules/expect-expect.md
|
|
113
114
|
[lowercase-name]: docs/rules/lowercase-name.md
|
|
115
|
+
[no-alias-methods]: docs/rules/no-alias-methods.md
|
|
114
116
|
[no-disabled-tests]: docs/rules/no-disabled-tests.md
|
|
115
117
|
[no-focused-tests]: docs/rules/no-focused-tests.md
|
|
116
118
|
[no-hooks]: docs/rules/no-hooks.md
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Don't use alias methods (no-alias-methods)
|
|
2
|
+
|
|
3
|
+
Several Jest methods have alias names, such as `toThrow` having the alias of
|
|
4
|
+
`toThrowError`. This rule ensures that only the canonical name as used in the
|
|
5
|
+
Jest documentation is used in the code. This makes it easier to search for all
|
|
6
|
+
occurrences of the method within code, and it ensures consistency among the
|
|
7
|
+
method names used.
|
|
8
|
+
|
|
9
|
+
## Rule details
|
|
10
|
+
|
|
11
|
+
This rule triggers a warning if the alias name, rather than the canonical name,
|
|
12
|
+
of a method is used.
|
|
13
|
+
|
|
14
|
+
### Default configuration
|
|
15
|
+
|
|
16
|
+
The following patterns are considered warnings:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
expect(a).toBeCalled();
|
|
20
|
+
expect(a).toBeCalledTimes();
|
|
21
|
+
expect(a).toBeCalledWith();
|
|
22
|
+
expect(a).lastCalledWith();
|
|
23
|
+
expect(a).nthCalledWith();
|
|
24
|
+
expect(a).toReturn();
|
|
25
|
+
expect(a).toReturnTimes();
|
|
26
|
+
expect(a).toReturnWith();
|
|
27
|
+
expect(a).lastReturnedWith();
|
|
28
|
+
expect(a).nthReturnedWith();
|
|
29
|
+
expect(a).toThrowError();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The following patterns are not considered warnings:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
expect(a).toHaveBeenCalled();
|
|
36
|
+
expect(a).toHaveBeenCalledTimes();
|
|
37
|
+
expect(a).toHaveBeenCalledWith();
|
|
38
|
+
expect(a).toHaveBeenLastCalledWith();
|
|
39
|
+
expect(a).toHaveBeenNthCalledWith();
|
|
40
|
+
expect(a).toHaveReturned();
|
|
41
|
+
expect(a).toHaveReturnedTimes();
|
|
42
|
+
expect(a).toHaveReturnedWith();
|
|
43
|
+
expect(a).toHaveLastReturnedWith();
|
|
44
|
+
expect(a).toHaveNthReturnedWith();
|
|
45
|
+
expect(a).toThrow();
|
|
46
|
+
```
|
package/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const validExpectInPromise = require('./rules/valid-expect-in-promise');
|
|
|
22
22
|
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
|
|
23
23
|
const preferStrictEqual = require('./rules/prefer-strict-equal');
|
|
24
24
|
const requireTothrowMessage = require('./rules/require-tothrow-message');
|
|
25
|
+
const noAliasMethods = require('./rules/no-alias-methods');
|
|
25
26
|
|
|
26
27
|
const snapshotProcessor = require('./processors/snapshot-processor');
|
|
27
28
|
|
|
@@ -91,5 +92,6 @@ module.exports = {
|
|
|
91
92
|
'prefer-inline-snapshots': preferInlineSnapshots,
|
|
92
93
|
'prefer-strict-equal': preferStrictEqual,
|
|
93
94
|
'require-tothrow-message': requireTothrowMessage,
|
|
95
|
+
'no-alias-methods': noAliasMethods,
|
|
94
96
|
},
|
|
95
97
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const RuleTester = require('eslint').RuleTester;
|
|
4
|
+
const rule = require('../no-alias-methods');
|
|
5
|
+
|
|
6
|
+
const ruleTester = new RuleTester();
|
|
7
|
+
|
|
8
|
+
ruleTester.run('no-alias-methods', rule, {
|
|
9
|
+
valid: [
|
|
10
|
+
'expect(a).toHaveBeenCalled()',
|
|
11
|
+
'expect(a).toHaveBeenCalledTimes()',
|
|
12
|
+
'expect(a).toHaveBeenCalledWith()',
|
|
13
|
+
'expect(a).toHaveBeenLastCalledWith()',
|
|
14
|
+
'expect(a).toHaveBeenNthCalledWith()',
|
|
15
|
+
'expect(a).toHaveReturned()',
|
|
16
|
+
'expect(a).toHaveReturnedTimes()',
|
|
17
|
+
'expect(a).toHaveReturnedWith()',
|
|
18
|
+
'expect(a).toHaveLastReturnedWith()',
|
|
19
|
+
'expect(a).toHaveNthReturnedWith()',
|
|
20
|
+
'expect(a).toThrow()',
|
|
21
|
+
],
|
|
22
|
+
|
|
23
|
+
invalid: [
|
|
24
|
+
{
|
|
25
|
+
code: 'expect(a).toBeCalled()',
|
|
26
|
+
errors: [
|
|
27
|
+
{
|
|
28
|
+
message:
|
|
29
|
+
'Replace toBeCalled() with its canonical name of toHaveBeenCalled()',
|
|
30
|
+
column: 11,
|
|
31
|
+
line: 1,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
output: 'expect(a).toHaveBeenCalled()',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
code: 'expect(a).toBeCalledTimes()',
|
|
38
|
+
errors: [
|
|
39
|
+
{
|
|
40
|
+
message:
|
|
41
|
+
'Replace toBeCalledTimes() with its canonical name of toHaveBeenCalledTimes()',
|
|
42
|
+
column: 11,
|
|
43
|
+
line: 1,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
output: 'expect(a).toHaveBeenCalledTimes()',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
code: 'expect(a).toBeCalledWith()',
|
|
50
|
+
errors: [
|
|
51
|
+
{
|
|
52
|
+
message:
|
|
53
|
+
'Replace toBeCalledWith() with its canonical name of toHaveBeenCalledWith()',
|
|
54
|
+
column: 11,
|
|
55
|
+
line: 1,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
output: 'expect(a).toHaveBeenCalledWith()',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
code: 'expect(a).lastCalledWith()',
|
|
62
|
+
errors: [
|
|
63
|
+
{
|
|
64
|
+
message:
|
|
65
|
+
'Replace lastCalledWith() with its canonical name of toHaveBeenLastCalledWith()',
|
|
66
|
+
column: 11,
|
|
67
|
+
line: 1,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
output: 'expect(a).toHaveBeenLastCalledWith()',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
code: 'expect(a).nthCalledWith()',
|
|
74
|
+
errors: [
|
|
75
|
+
{
|
|
76
|
+
message:
|
|
77
|
+
'Replace nthCalledWith() with its canonical name of toHaveBeenNthCalledWith()',
|
|
78
|
+
column: 11,
|
|
79
|
+
line: 1,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
output: 'expect(a).toHaveBeenNthCalledWith()',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
code: 'expect(a).toReturn()',
|
|
86
|
+
errors: [
|
|
87
|
+
{
|
|
88
|
+
message:
|
|
89
|
+
'Replace toReturn() with its canonical name of toHaveReturned()',
|
|
90
|
+
column: 11,
|
|
91
|
+
line: 1,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
output: 'expect(a).toHaveReturned()',
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
code: 'expect(a).toReturnTimes()',
|
|
98
|
+
errors: [
|
|
99
|
+
{
|
|
100
|
+
message:
|
|
101
|
+
'Replace toReturnTimes() with its canonical name of toHaveReturnedTimes()',
|
|
102
|
+
column: 11,
|
|
103
|
+
line: 1,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
output: 'expect(a).toHaveReturnedTimes()',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
code: 'expect(a).toReturnWith()',
|
|
110
|
+
errors: [
|
|
111
|
+
{
|
|
112
|
+
message:
|
|
113
|
+
'Replace toReturnWith() with its canonical name of toHaveReturnedWith()',
|
|
114
|
+
column: 11,
|
|
115
|
+
line: 1,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
output: 'expect(a).toHaveReturnedWith()',
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
code: 'expect(a).lastReturnedWith()',
|
|
122
|
+
errors: [
|
|
123
|
+
{
|
|
124
|
+
message:
|
|
125
|
+
'Replace lastReturnedWith() with its canonical name of toHaveLastReturnedWith()',
|
|
126
|
+
column: 11,
|
|
127
|
+
line: 1,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
output: 'expect(a).toHaveLastReturnedWith()',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
code: 'expect(a).nthReturnedWith()',
|
|
134
|
+
errors: [
|
|
135
|
+
{
|
|
136
|
+
message:
|
|
137
|
+
'Replace nthReturnedWith() with its canonical name of toHaveNthReturnedWith()',
|
|
138
|
+
column: 11,
|
|
139
|
+
line: 1,
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
output: 'expect(a).toHaveNthReturnedWith()',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
code: 'expect(a).toThrowError()',
|
|
146
|
+
errors: [
|
|
147
|
+
{
|
|
148
|
+
message:
|
|
149
|
+
'Replace toThrowError() with its canonical name of toThrow()',
|
|
150
|
+
column: 11,
|
|
151
|
+
line: 1,
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
output: 'expect(a).toThrow()',
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
});
|
|
@@ -6,7 +6,10 @@ const rule = require('../prefer-strict-equal');
|
|
|
6
6
|
const ruleTester = new RuleTester();
|
|
7
7
|
|
|
8
8
|
ruleTester.run('prefer-strict-equal', rule, {
|
|
9
|
-
valid: [
|
|
9
|
+
valid: [
|
|
10
|
+
'expect(something).toStrictEqual(somethingElse);',
|
|
11
|
+
"a().toEqual('b')",
|
|
12
|
+
],
|
|
10
13
|
invalid: [
|
|
11
14
|
{
|
|
12
15
|
code: 'expect(something).toEqual(somethingElse);',
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const RuleTester = require('eslint').RuleTester;
|
|
4
|
+
const rule = require('../require-tothrow-message');
|
|
5
|
+
|
|
6
|
+
const ruleTester = new RuleTester({
|
|
7
|
+
parserOptions: {
|
|
8
|
+
ecmaVersion: 6,
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
ruleTester.run('require-tothrow-message', rule, {
|
|
13
|
+
valid: [
|
|
14
|
+
// String
|
|
15
|
+
"expect(() => { throw new Error('a'); }).toThrow('a');",
|
|
16
|
+
"expect(() => { throw new Error('a'); }).toThrowError('a');",
|
|
17
|
+
|
|
18
|
+
// Template literal
|
|
19
|
+
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);",
|
|
20
|
+
|
|
21
|
+
// Regex
|
|
22
|
+
"expect(() => { throw new Error('a'); }).toThrow(/^a$/);",
|
|
23
|
+
|
|
24
|
+
// Function
|
|
25
|
+
"expect(() => { throw new Error('a'); })" +
|
|
26
|
+
".toThrow((() => { return 'a'; })());",
|
|
27
|
+
|
|
28
|
+
// Allow no message for `not`.
|
|
29
|
+
"expect(() => { throw new Error('a'); }).not.toThrow();",
|
|
30
|
+
],
|
|
31
|
+
|
|
32
|
+
invalid: [
|
|
33
|
+
// Empty toThrow
|
|
34
|
+
{
|
|
35
|
+
code: "expect(() => { throw new Error('a'); }).toThrow();",
|
|
36
|
+
errors: [
|
|
37
|
+
{ message: 'Add an error message to toThrow()', column: 41, line: 1 },
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
// Empty toThrowError
|
|
41
|
+
{
|
|
42
|
+
code: "expect(() => { throw new Error('a'); }).toThrowError();",
|
|
43
|
+
errors: [
|
|
44
|
+
{
|
|
45
|
+
message: 'Add an error message to toThrowError()',
|
|
46
|
+
column: 41,
|
|
47
|
+
line: 1,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const expectCase = require('./util').expectCase;
|
|
4
|
+
const getDocsUrl = require('./util').getDocsUrl;
|
|
5
|
+
const method = require('./util').method;
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
meta: {
|
|
9
|
+
docs: {
|
|
10
|
+
url: getDocsUrl(__filename),
|
|
11
|
+
},
|
|
12
|
+
fixable: 'code',
|
|
13
|
+
},
|
|
14
|
+
create(context) {
|
|
15
|
+
// The Jest methods which have aliases. The canonical name is the first
|
|
16
|
+
// index of each item.
|
|
17
|
+
const methodNames = [
|
|
18
|
+
['toHaveBeenCalled', 'toBeCalled'],
|
|
19
|
+
['toHaveBeenCalledTimes', 'toBeCalledTimes'],
|
|
20
|
+
['toHaveBeenCalledWith', 'toBeCalledWith'],
|
|
21
|
+
['toHaveBeenLastCalledWith', 'lastCalledWith'],
|
|
22
|
+
['toHaveBeenNthCalledWith', 'nthCalledWith'],
|
|
23
|
+
['toHaveReturned', 'toReturn'],
|
|
24
|
+
['toHaveReturnedTimes', 'toReturnTimes'],
|
|
25
|
+
['toHaveReturnedWith', 'toReturnWith'],
|
|
26
|
+
['toHaveLastReturnedWith', 'lastReturnedWith'],
|
|
27
|
+
['toHaveNthReturnedWith', 'nthReturnedWith'],
|
|
28
|
+
['toThrow', 'toThrowError'],
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
CallExpression(node) {
|
|
33
|
+
if (!expectCase(node)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Check if the method used matches any of ours.
|
|
38
|
+
const propertyName = method(node) && method(node).name;
|
|
39
|
+
const methodItem = methodNames.find(item => item[1] === propertyName);
|
|
40
|
+
|
|
41
|
+
if (methodItem) {
|
|
42
|
+
context.report({
|
|
43
|
+
message: `Replace {{ replace }}() with its canonical name of {{ canonical }}()`,
|
|
44
|
+
data: {
|
|
45
|
+
replace: methodItem[1],
|
|
46
|
+
canonical: methodItem[0],
|
|
47
|
+
},
|
|
48
|
+
node: method(node),
|
|
49
|
+
fix(fixer) {
|
|
50
|
+
return [fixer.replaceText(method(node), methodItem[0])];
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const expectCase = require('./util').expectCase;
|
|
3
4
|
const getDocsUrl = require('./util').getDocsUrl;
|
|
5
|
+
const method = require('./util').method;
|
|
4
6
|
|
|
5
7
|
module.exports = {
|
|
6
8
|
meta: {
|
|
@@ -12,14 +14,19 @@ module.exports = {
|
|
|
12
14
|
create(context) {
|
|
13
15
|
return {
|
|
14
16
|
CallExpression(node) {
|
|
15
|
-
|
|
17
|
+
if (!expectCase(node)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const propertyName = method(node) && method(node).name;
|
|
22
|
+
|
|
16
23
|
if (propertyName === 'toEqual') {
|
|
17
24
|
context.report({
|
|
18
25
|
fix(fixer) {
|
|
19
|
-
return [fixer.replaceText(node
|
|
26
|
+
return [fixer.replaceText(method(node), 'toStrictEqual')];
|
|
20
27
|
},
|
|
21
28
|
message: 'Use toStrictEqual() instead',
|
|
22
|
-
node: node
|
|
29
|
+
node: method(node),
|
|
23
30
|
});
|
|
24
31
|
}
|
|
25
32
|
},
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const argument = require('./util').argument;
|
|
4
|
+
const expectCase = require('./util').expectCase;
|
|
3
5
|
const getDocsUrl = require('./util').getDocsUrl;
|
|
6
|
+
const method = require('./util').method;
|
|
4
7
|
|
|
5
8
|
module.exports = {
|
|
6
9
|
meta: {
|
|
@@ -11,19 +14,23 @@ module.exports = {
|
|
|
11
14
|
create(context) {
|
|
12
15
|
return {
|
|
13
16
|
CallExpression(node) {
|
|
14
|
-
|
|
17
|
+
if (!expectCase(node)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const propertyName = method(node) && method(node).name;
|
|
15
22
|
|
|
16
23
|
// Look for `toThrow` calls with no arguments.
|
|
17
24
|
if (
|
|
18
25
|
['toThrow', 'toThrowError'].indexOf(propertyName) > -1 &&
|
|
19
|
-
!(node
|
|
26
|
+
!argument(node)
|
|
20
27
|
) {
|
|
21
28
|
context.report({
|
|
22
29
|
message: `Add an error message to {{ propertyName }}()`,
|
|
23
30
|
data: {
|
|
24
31
|
propertyName,
|
|
25
32
|
},
|
|
26
|
-
node: node
|
|
33
|
+
node: method(node),
|
|
27
34
|
});
|
|
28
35
|
}
|
|
29
36
|
},
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const RuleTester = require('eslint').RuleTester;
|
|
4
|
-
const rule = require('../require-tothrow-message');
|
|
5
|
-
|
|
6
|
-
const ruleTester = new RuleTester();
|
|
7
|
-
|
|
8
|
-
ruleTester.run('require-tothrow-message', rule, {
|
|
9
|
-
valid: [
|
|
10
|
-
"expect(function() { a() }).toThrow('a');",
|
|
11
|
-
"expect(function() { a() }).toThrowError('a');",
|
|
12
|
-
],
|
|
13
|
-
|
|
14
|
-
invalid: [
|
|
15
|
-
{
|
|
16
|
-
code: 'expect(function() { a() }).toThrow();',
|
|
17
|
-
errors: [
|
|
18
|
-
{ message: 'Add an error message to toThrow()', column: 28, line: 1 },
|
|
19
|
-
],
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
code: 'expect(function() { a() }).toThrowError();',
|
|
23
|
-
errors: [
|
|
24
|
-
{
|
|
25
|
-
message: 'Add an error message to toThrowError()',
|
|
26
|
-
column: 28,
|
|
27
|
-
line: 1,
|
|
28
|
-
},
|
|
29
|
-
],
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
});
|