Skip to content

Commit ee9578f

Browse files
noahbenhamSimenB
authored andcommitted
fix(no-alias-methods): support .not, .resolves & .rejects (#210)
1 parent 73c9187 commit ee9578f

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

rules/__tests__/no-alias-methods.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,41 @@ ruleTester.run('no-alias-methods', rule, {
153153
],
154154
output: 'expect(a).toThrow()',
155155
},
156+
{
157+
code: 'expect(a).resolves.toThrowError()',
158+
errors: [
159+
{
160+
message:
161+
'Replace toThrowError() with its canonical name of toThrow()',
162+
column: 20,
163+
line: 1,
164+
},
165+
],
166+
output: 'expect(a).resolves.toThrow()',
167+
},
168+
{
169+
code: 'expect(a).rejects.toThrowError()',
170+
errors: [
171+
{
172+
message:
173+
'Replace toThrowError() with its canonical name of toThrow()',
174+
column: 19,
175+
line: 1,
176+
},
177+
],
178+
output: 'expect(a).rejects.toThrow()',
179+
},
180+
{
181+
code: 'expect(a).not.toThrowError()',
182+
errors: [
183+
{
184+
message:
185+
'Replace toThrowError() with its canonical name of toThrow()',
186+
column: 15,
187+
line: 1,
188+
},
189+
],
190+
output: 'expect(a).not.toThrow()',
191+
},
156192
],
157193
});

rules/no-alias-methods.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ module.exports = {
3232
return;
3333
}
3434

35-
// Check if the method used matches any of ours.
36-
const propertyName = method(node) && method(node).name;
37-
const methodItem = methodNames.find(item => item[1] === propertyName);
35+
let targetNode = method(node);
36+
if (
37+
targetNode.name === 'resolves' ||
38+
targetNode.name === 'rejects' ||
39+
targetNode.name === 'not'
40+
) {
41+
targetNode = method(node.parent);
42+
}
43+
44+
// Check if the method used matches any of ours
45+
const methodItem = methodNames.find(
46+
item => item[1] === targetNode.name
47+
);
3848

3949
if (methodItem) {
4050
context.report({
@@ -43,9 +53,9 @@ module.exports = {
4353
replace: methodItem[1],
4454
canonical: methodItem[0],
4555
},
46-
node: method(node),
56+
node: targetNode,
4757
fix(fixer) {
48-
return [fixer.replaceText(method(node), methodItem[0])];
58+
return [fixer.replaceText(targetNode, methodItem[0])];
4959
},
5060
});
5161
}

0 commit comments

Comments
 (0)