Skip to content

Commit

Permalink
fix(prefer-equality-matcher): handle resolves and rejects modifie…
Browse files Browse the repository at this point in the history
…rs correctly (#1146)
  • Loading branch information
G-Rath authored Jun 6, 2022
1 parent f85f647 commit 0fad4df
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
130 changes: 130 additions & 0 deletions src/rules/__tests__/prefer-equality-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
},
],
},
{
code: 'expect(a === b).resolves.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).resolves.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).not.toBe(true);',
errors: [
Expand All @@ -89,6 +115,58 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
},
],
},
{
code: 'expect(a === b).resolves.not.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b).resolves.not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a === b)["resolves"].not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 29,
line: 1,
},
],
},
{
code: 'expect(a === b)["resolves"]["not"]["toBe"](false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 29,
line: 1,
},
],
},
],
});

Expand Down Expand Up @@ -125,6 +203,32 @@ ruleTester.run('prefer-equality-matcher: !==', rule, {
},
],
},
{
code: 'expect(a !== b).resolves.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).resolves.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).not.toBe(true);',
errors: [
Expand All @@ -151,5 +255,31 @@ ruleTester.run('prefer-equality-matcher: !==', rule, {
},
],
},
{
code: 'expect(a !== b).resolves.not.toBe(true);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
{
code: 'expect(a !== b).resolves.not.toBe(false);',
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a).resolves.not.${equalityMatcher}(b);`,
),
column: 26,
line: 1,
},
],
},
],
});
24 changes: 19 additions & 5 deletions src/rules/prefer-equality-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,33 @@ export default createRule({
matcher.arguments[0],
).value;

const negation = modifier?.negation
? { node: modifier.negation }
: modifier?.name === ModifierName.not
? modifier
: null;

// we need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
const addNotModifier =
(comparison.operator === '!==' ? !matcherValue : matcherValue) ===
!!modifier;
!!negation;

const buildFixer =
(equalityMatcher: string): TSESLint.ReportFixFunction =>
fixer => {
const sourceCode = context.getSourceCode();

// preserve the existing modifier if it's not a negation
let modifierText =
modifier && modifier?.node !== negation?.node
? `.${modifier.name}`
: '';

if (addNotModifier) {
modifierText += `.${ModifierName.not}`;
}

return [
// replace the comparison argument with the left-hand side of the comparison
fixer.replaceText(
Expand All @@ -105,9 +121,7 @@ export default createRule({
// replace the current matcher & modifier with the preferred matcher
fixer.replaceTextRange(
[expectCallEnd, matcher.node.range[1]],
addNotModifier
? `.${ModifierName.not}.${equalityMatcher}`
: `.${equalityMatcher}`,
`${modifierText}.${equalityMatcher}`,
),
// replace the matcher argument with the right-hand side of the comparison
fixer.replaceText(
Expand All @@ -126,7 +140,7 @@ export default createRule({
fix: buildFixer(equalityMatcher),
}),
),
node: (modifier || matcher).node.property,
node: (negation || matcher).node.property,
});
},
};
Expand Down

0 comments on commit 0fad4df

Please sign in to comment.