From 5fe45680c93ff50745fc8f8f271607c21d9cae87 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 8 Sep 2022 21:06:15 +1200 Subject: [PATCH] fix(no-restricted-matchers): improve check to not be solely based on the start of the matcher chain (#1236) Resolves #1235 --- .../__tests__/no-restricted-matchers.test.ts | 4 ++++ src/rules/no-restricted-matchers.ts | 20 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rules/__tests__/no-restricted-matchers.test.ts b/src/rules/__tests__/no-restricted-matchers.test.ts index 1d134ec38..60cfd963b 100644 --- a/src/rules/__tests__/no-restricted-matchers.test.ts +++ b/src/rules/__tests__/no-restricted-matchers.test.ts @@ -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 }], diff --git a/src/rules/no-restricted-matchers.ts b/src/rules/no-restricted-matchers.ts index ebe2f52fd..f05cd4367 100644 --- a/src/rules/no-restricted-matchers.ts +++ b/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], @@ -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'