Skip to content

Commit 5fe4568

Browse files
authoredSep 8, 2022
fix(no-restricted-matchers): improve check to not be solely based on the start of the matcher chain (#1236)
Resolves #1235
1 parent f0ef2df commit 5fe4568

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎src/rules/__tests__/no-restricted-matchers.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ ruleTester.run('no-restricted-matchers', rule, {
3333
code: 'expect(a).toBe(b)',
3434
options: [{ 'not.toBe': null }],
3535
},
36+
{
37+
code: 'expect(a).toBeUndefined(b)',
38+
options: [{ toBe: null }],
39+
},
3640
{
3741
code: 'expect(a)["toBe"](b)',
3842
options: [{ 'not.toBe': null }],

‎src/rules/no-restricted-matchers.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
import { createRule, getAccessorValue, parseJestFnCall } from './utils';
1+
import {
2+
ModifierName,
3+
createRule,
4+
getAccessorValue,
5+
parseJestFnCall,
6+
} from './utils';
7+
8+
const isChainRestricted = (chain: string, restriction: string): boolean => {
9+
if (
10+
ModifierName.hasOwnProperty(restriction) ||
11+
restriction.endsWith('.not')
12+
) {
13+
return chain.startsWith(restriction);
14+
}
15+
16+
return chain === restriction;
17+
};
218

319
export default createRule<
420
[Record<string, string | null>],
@@ -40,7 +56,7 @@ export default createRule<
4056
.join('.');
4157

4258
for (const [restriction, message] of Object.entries(restrictedChains)) {
43-
if (chain.startsWith(restriction)) {
59+
if (isChainRestricted(chain, restriction)) {
4460
context.report({
4561
messageId: message
4662
? 'restrictedChainWithMessage'

0 commit comments

Comments
 (0)
Please sign in to comment.