Skip to content

Commit 92b1d5c

Browse files
tryggvigySimenB
authored andcommitted
feat(rules): add prefer-strict-equal (#134)
1 parent e42c9e3 commit 92b1d5c

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ for more information about extending configuration files.
9393
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
9494
| [no-test-return-statement][] | Disallow explicitly returning from tests | | |
9595
| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
96+
| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
9697
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
9798
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
9899
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
@@ -119,6 +120,7 @@ for more information about extending configuration files.
119120
[no-test-prefixes]: docs/rules/no-test-prefixes.md
120121
[no-test-return-statement]: docs/rules/no-test-return-statement.md
121122
[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
123+
[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
122124
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
123125
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
124126
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md

docs/rules/prefer-strict-equal.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Suggest using `toStrictEqual()` (prefer-strict-equal)
2+
3+
`toStrictEqual` not only checks that two objects contain the same data but also
4+
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.
5+
6+
## Rule details
7+
8+
This rule triggers a warning if `toEqual()` is used to assert equality.
9+
10+
### Default configuration
11+
12+
The following pattern is considered warning:
13+
14+
```js
15+
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true
16+
```
17+
18+
The following pattern is not warning:
19+
20+
```js
21+
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false
22+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const validExpect = require('./rules/valid-expect');
2020
const preferExpectAssertions = require('./rules/prefer-expect-assertions');
2121
const validExpectInPromise = require('./rules/valid-expect-in-promise');
2222
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
23+
const preferStrictEqual = require('./rules/prefer-strict-equal');
2324

2425
const snapshotProcessor = require('./processors/snapshot-processor');
2526

@@ -87,5 +88,6 @@ module.exports = {
8788
'prefer-expect-assertions': preferExpectAssertions,
8889
'valid-expect-in-promise': validExpectInPromise,
8990
'prefer-inline-snapshots': preferInlineSnapshots,
91+
'prefer-strict-equal': preferStrictEqual,
9092
},
9193
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../prefer-strict-equal');
5+
6+
const ruleTester = new RuleTester();
7+
8+
ruleTester.run('prefer-strict-equal', rule, {
9+
valid: ['expect(something).toStrictEqual(somethingElse);'],
10+
invalid: [
11+
{
12+
code: 'expect(something).toEqual(somethingElse);',
13+
errors: [
14+
{
15+
message: 'Use toStrictEqual() instead',
16+
column: 19,
17+
line: 1,
18+
},
19+
],
20+
output: 'expect(something).toStrictEqual(somethingElse);',
21+
},
22+
],
23+
});

rules/prefer-strict-equal.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const getDocsUrl = require('./util').getDocsUrl;
4+
5+
module.exports = {
6+
meta: {
7+
docs: {
8+
url: getDocsUrl(__filename),
9+
},
10+
fixable: 'code',
11+
},
12+
create(context) {
13+
return {
14+
CallExpression(node) {
15+
const propertyName = node.callee.property && node.callee.property.name;
16+
if (propertyName === 'toEqual') {
17+
context.report({
18+
fix(fixer) {
19+
return [fixer.replaceText(node.callee.property, 'toStrictEqual')];
20+
},
21+
message: 'Use toStrictEqual() instead',
22+
node: node.callee.property,
23+
});
24+
}
25+
},
26+
};
27+
},
28+
};

0 commit comments

Comments
 (0)