Skip to content

Commit

Permalink
Add prefer-strict-equal matcher (#105)
Browse files Browse the repository at this point in the history
* Add prefer-strict-equal matcher

* Update to `parseExpectCall`

* Rename
  • Loading branch information
mskelton authored Oct 19, 2022
1 parent afeb0a9 commit 8cdbf74
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ command line option.\
|| | 💡 | [no-skipped-test](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-skipped-test.md) | Disallow usage of the `.skip` annotation |
|| 🔧 | | [no-useless-not](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-useless-not.md) | Disallow usage of `not` matchers when a specific matcher exists |
|| | 💡 | [no-wait-for-timeout](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-wait-for-timeout.md) | Disallow usage of `page.waitForTimeout` |
| | | 💡 | [prefer-strict-equal](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` |
| | 🔧 | | [prefer-lowercase-title](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names |
| | 🔧 | | [prefer-to-be](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-be.md) | Suggest using `toBe()` |
| | 🔧 | | [prefer-to-have-length](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` |
Expand Down
24 changes: 24 additions & 0 deletions docs/rules/prefer-strict-equal.md
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
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import noUselessNot from './rules/no-useless-not';
import preferLowercaseTitle from './rules/prefer-lowercase-title';
import preferToBe from './rules/prefer-to-be';
import preferToHaveLength from './rules/prefer-to-have-length';
import preferStrictEqual from './rules/prefer-strict-equal';
import requireTopLevelDescribe from './rules/require-top-level-describe';
import validExpect from './rules/valid-expect';

Expand Down Expand Up @@ -86,6 +87,7 @@ export = {
'no-useless-not': noUselessNot,
'no-restricted-matchers': noRestrictedMatchers,
'prefer-lowercase-title': preferLowercaseTitle,
'prefer-strict-equal': preferStrictEqual,
'prefer-to-be': preferToBe,
'prefer-to-have-length': preferToHaveLength,
'require-top-level-describe': requireTopLevelDescribe,
Expand Down
48 changes: 48 additions & 0 deletions src/rules/prefer-strict-equal.ts
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;
44 changes: 44 additions & 0 deletions test/spec/prefer-strict-equal.spec.ts
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);',
},
],
},
],
},
],
});

0 comments on commit 8cdbf74

Please sign in to comment.