-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prefer-strict-equal matcher (#105)
* Add prefer-strict-equal matcher * Update to `parseExpectCall` * Rename
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Suggest using `toStrictEqual()` (`prefer-strict-equal`) | ||
|
||
`toStrictEqual` not only checks that two objects contain the same data but also | ||
that they have the same structure. It is common to expect objects to not only | ||
have identical values but also to have identical keys. A stricter equality will | ||
catch cases where two objects do not have identical keys. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toEqual()` is used to assert equality. | ||
|
||
### Default configuration | ||
|
||
The following pattern is considered warning: | ||
|
||
```javascript | ||
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```javascript | ||
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Rule } from 'eslint'; | ||
import { replaceAccessorFixer } from '../utils/fixer'; | ||
import { parseExpectCall } from '../utils/parseExpectCall'; | ||
|
||
export default { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const expectCall = parseExpectCall(node); | ||
|
||
if (expectCall?.matcherName === 'toEqual') { | ||
context.report({ | ||
node: expectCall.matcher, | ||
messageId: 'useToStrictEqual', | ||
suggest: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
fix: (fixer) => { | ||
return replaceAccessorFixer( | ||
fixer, | ||
expectCall.matcher, | ||
'toStrictEqual' | ||
); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Suggest using `toStrictEqual()`', | ||
recommended: false, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-strict-equal.md', | ||
}, | ||
messages: { | ||
useToStrictEqual: 'Use toStrictEqual() instead', | ||
suggestReplaceWithStrictEqual: 'Replace with `toStrictEqual()`', | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
hasSuggestions: true, | ||
schema: [], | ||
}, | ||
} as Rule.RuleModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { runRuleTester } from '../utils/rule-tester'; | ||
import rule from '../../src/rules/prefer-strict-equal'; | ||
|
||
runRuleTester('prefer-strict-equal', rule, { | ||
valid: [ | ||
'expect(something).toStrictEqual(somethingElse);', | ||
"a().toEqual('b')", | ||
'expect(a);', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(something).toEqual(somethingElse);', | ||
errors: [ | ||
{ | ||
messageId: 'useToStrictEqual', | ||
column: 19, | ||
line: 1, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
output: 'expect(something).toStrictEqual(somethingElse);', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(something)["toEqual"](somethingElse);', | ||
errors: [ | ||
{ | ||
messageId: 'useToStrictEqual', | ||
column: 19, | ||
line: 1, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestReplaceWithStrictEqual', | ||
output: 'expect(something)["toStrictEqual"](somethingElse);', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |