Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-restricted-matchers): improve check to not be solely based on the start of the matcher chain #1236

Merged
merged 1 commit into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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