Skip to content

Commit df0cbd2

Browse files
committed
fixup: add more terminators, allow passing custom, add function name
1 parent 5877cba commit df0cbd2

File tree

2 files changed

+71
-27
lines changed

2 files changed

+71
-27
lines changed

configs/eslint-plugin-devtools/rules/no-expect-method-without-call.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const PROPERTY_TERMINATORS = [
4+
// Default chai terminators.
45
'ok',
56
'true',
67
'false',
@@ -9,6 +10,28 @@ const PROPERTY_TERMINATORS = [
910
'exist',
1011
'empty',
1112
'arguments',
13+
'NaN',
14+
'extensible',
15+
'sealed',
16+
'frozen',
17+
'finite',
18+
19+
// Terminators from chai-as-promised.
20+
'fulfilled',
21+
'rejected',
22+
23+
// Terminators from sinon-chai.
24+
'called',
25+
'calledOnce',
26+
'calledTwice',
27+
'calledThrice',
28+
'calledWithNew',
29+
30+
// Terminators from chai-dom.
31+
'displayed',
32+
'visible',
33+
'focus',
34+
'checked',
1235
];
1336

1437
function followNodeChainToExpectCall(node) {
@@ -41,18 +64,21 @@ module.exports = {
4164
description:
4265
'Require invocation of expect method assertions (e.g., to.throw())',
4366
},
44-
messages: {
45-
methodNotInvoked:
46-
'expect().to.[METHOD] must be invoked with parentheses, e.g., expect(() => someFn()).to.throw()',
47-
},
4867
},
4968

5069
create(context) {
70+
const options = context.options[0] ?? {};
71+
const additionalTerminators = options.properties ?? [];
72+
const validTerminators = [
73+
...PROPERTY_TERMINATORS,
74+
...additionalTerminators,
75+
];
76+
5177
return {
5278
MemberExpression(node) {
5379
if (
5480
node.type !== 'MemberExpression' ||
55-
PROPERTY_TERMINATORS.includes(node.property.name) ||
81+
validTerminators.includes(node.property.name) ||
5682
node.property.name === 'expect'
5783
) {
5884
return null;
@@ -77,9 +103,15 @@ module.exports = {
77103
return null;
78104
}
79105

106+
const source = context.getSourceCode();
107+
const calleeText = source.getText(node);
108+
const expectText = source.getText(expectCall);
109+
const assertionText = calleeText.substring(expectText.length + 1);
110+
80111
context.report({
81112
node,
82-
messageId: 'methodNotInvoked',
113+
// message: `"${assertionText}" used as function`
114+
message: `expect().${assertionText} must be invoked with parentheses, e.g., expect().${assertionText}()`,
83115
});
84116
},
85117
};

configs/eslint-plugin-devtools/rules/no-expect-method-without-call.test.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,24 @@ it('should test something', () => {
8282
code: 'expect(promise).to.be.rejectedWith(Error)',
8383
parserOptions: { ecmaVersion: 2021 },
8484
},
85+
{
86+
options: [{ properties: ['throw'] }],
87+
code: `
88+
it("does not fail passed property", function() {
89+
expect(result).to.throw;
90+
});
91+
`,
92+
},
8593
],
94+
8695
invalid: [
8796
{
8897
code: 'expect(() => someFn()).to.throw',
8998
parserOptions: { ecmaVersion: 2021 },
9099
errors: [
91100
{
92-
messageId: 'methodNotInvoked',
101+
message:
102+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
93103
},
94104
],
95105
},
@@ -98,16 +108,8 @@ it('should test something', () => {
98108
parserOptions: { ecmaVersion: 2021 },
99109
errors: [
100110
{
101-
messageId: 'methodNotInvoked',
102-
},
103-
],
104-
},
105-
{
106-
code: 'expect("pineapple").to.include',
107-
parserOptions: { ecmaVersion: 2021 },
108-
errors: [
109-
{
110-
messageId: 'methodNotInvoked',
111+
message:
112+
'expect().to.be.a must be invoked with parentheses, e.g., expect().to.be.a()',
111113
},
112114
],
113115
},
@@ -116,7 +118,8 @@ it('should test something', () => {
116118
parserOptions: { ecmaVersion: 2021 },
117119
errors: [
118120
{
119-
messageId: 'methodNotInvoked',
121+
message:
122+
'expect().to.not.include must be invoked with parentheses, e.g., expect().to.not.include()',
120123
},
121124
],
122125
},
@@ -125,7 +128,8 @@ it('should test something', () => {
125128
parserOptions: { ecmaVersion: 2021 },
126129
errors: [
127130
{
128-
messageId: 'methodNotInvoked',
131+
message:
132+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
129133
},
130134
],
131135
},
@@ -134,7 +138,8 @@ it('should test something', () => {
134138
parserOptions: { ecmaVersion: 2021 },
135139
errors: [
136140
{
137-
messageId: 'methodNotInvoked',
141+
message:
142+
'expect().to.not.throw must be invoked with parentheses, e.g., expect().to.not.throw()',
138143
},
139144
],
140145
},
@@ -143,7 +148,8 @@ it('should test something', () => {
143148
parserOptions: { ecmaVersion: 2021 },
144149
errors: [
145150
{
146-
messageId: 'methodNotInvoked',
151+
message:
152+
'expect().to.not.throws must be invoked with parentheses, e.g., expect().to.not.throws()',
147153
},
148154
],
149155
},
@@ -152,7 +158,8 @@ it('should test something', () => {
152158
parserOptions: { ecmaVersion: 2021 },
153159
errors: [
154160
{
155-
messageId: 'methodNotInvoked',
161+
message:
162+
'expect().to.not.Throw must be invoked with parentheses, e.g., expect().to.not.Throw()',
156163
},
157164
],
158165
},
@@ -161,7 +168,8 @@ it('should test something', () => {
161168
parserOptions: { ecmaVersion: 2021 },
162169
errors: [
163170
{
164-
messageId: 'methodNotInvoked',
171+
message:
172+
'expect().not.to.throw must be invoked with parentheses, e.g., expect().not.to.throw()',
165173
},
166174
],
167175
},
@@ -170,7 +178,8 @@ it('should test something', () => {
170178
parserOptions: { ecmaVersion: 2021 },
171179
errors: [
172180
{
173-
messageId: 'methodNotInvoked',
181+
message:
182+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
174183
},
175184
],
176185
},
@@ -182,7 +191,8 @@ it('should test something', () => {
182191
parserOptions: { ecmaVersion: 2021 },
183192
errors: [
184193
{
185-
messageId: 'methodNotInvoked',
194+
message:
195+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
186196
},
187197
],
188198
},
@@ -191,7 +201,8 @@ it('should test something', () => {
191201
parserOptions: { ecmaVersion: 2021 },
192202
errors: [
193203
{
194-
messageId: 'methodNotInvoked',
204+
message:
205+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
195206
},
196207
],
197208
},
@@ -200,7 +211,8 @@ it('should test something', () => {
200211
parserOptions: { ecmaVersion: 2021 },
201212
errors: [
202213
{
203-
messageId: 'methodNotInvoked',
214+
message:
215+
'expect().to.throw must be invoked with parentheses, e.g., expect().to.throw()',
204216
},
205217
],
206218
},

0 commit comments

Comments
 (0)