Skip to content

Commit

Permalink
fix(no-restricted-matchers): improve check to not be solely based on …
Browse files Browse the repository at this point in the history
…the start of the matcher chain (#1236)

Resolves #1235
  • Loading branch information
G-Rath committed Sep 8, 2022
1 parent f0ef2df commit 5fe4568
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/rules/__tests__/no-restricted-matchers.test.ts
Expand Up @@ -33,6 +33,10 @@ ruleTester.run('no-restricted-matchers', rule, {
code: 'expect(a).toBe(b)',
options: [{ 'not.toBe': null }],
},
{
code: 'expect(a).toBeUndefined(b)',
options: [{ toBe: null }],
},
{
code: 'expect(a)["toBe"](b)',
options: [{ 'not.toBe': null }],
Expand Down
20 changes: 18 additions & 2 deletions src/rules/no-restricted-matchers.ts
@@ -1,4 +1,20 @@
import { createRule, getAccessorValue, parseJestFnCall } from './utils';
import {
ModifierName,
createRule,
getAccessorValue,
parseJestFnCall,
} from './utils';

const isChainRestricted = (chain: string, restriction: string): boolean => {
if (
ModifierName.hasOwnProperty(restriction) ||
restriction.endsWith('.not')
) {
return chain.startsWith(restriction);
}

return chain === restriction;
};

export default createRule<
[Record<string, string | null>],
Expand Down Expand Up @@ -40,7 +56,7 @@ export default createRule<
.join('.');

for (const [restriction, message] of Object.entries(restrictedChains)) {
if (chain.startsWith(restriction)) {
if (isChainRestricted(chain, restriction)) {
context.report({
messageId: message
? 'restrictedChainWithMessage'
Expand Down

0 comments on commit 5fe4568

Please sign in to comment.