Skip to content

Commit

Permalink
fix(no-restricted-matchers): allow restricting negated resolves and…
Browse files Browse the repository at this point in the history
… `rejects` modifiers
  • Loading branch information
G-Rath committed Jun 4, 2022
1 parent 8f567b1 commit bba5108
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/rules/__tests__/no-restricted-matchers.test.ts
Expand Up @@ -99,6 +99,36 @@ ruleTester.run('no-restricted-matchers', rule, {
},
],
},
{
code: 'expect(a).resolves.toBe(b)',
options: [{ resolves: null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'resolves.not': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).not.toBe(b)',
options: [{ 'not.toBe': null }],
Expand All @@ -115,6 +145,22 @@ ruleTester.run('no-restricted-matchers', rule, {
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'resolves.not.toBe': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not.toBe',
},
endColumn: 28,
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).toBe(b)',
options: [{ toBe: 'Prefer `toStrictEqual` instead' }],
Expand Down
14 changes: 12 additions & 2 deletions src/rules/no-restricted-matchers.ts
Expand Up @@ -54,7 +54,11 @@ export default createRule<
}

if (modifier) {
const chain = modifier.name;
let chain: string = modifier.name;

if (modifier.negation) {
chain += '.not';
}

if (chain in restrictedChains) {
const message = restrictedChains[chain];
Expand All @@ -72,7 +76,13 @@ export default createRule<
}

if (matcher && modifier) {
const chain = `${modifier.name}.${matcher.name}`;
let chain: string = modifier.name;

if (modifier.negation) {
chain += '.not';
}

chain += `.${matcher.name}`;

if (chain in restrictedChains) {
const message = restrictedChains[chain];
Expand Down

0 comments on commit bba5108

Please sign in to comment.