Skip to content

Commit d7c2c33

Browse files
committed
feat(rules): add prefer-strict-equal (#134)
1 parent 5bc0eea commit d7c2c33

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ for more information about extending configuration files.
9191
| [no-large-snapshots][] | Disallow large snapshots | | |
9292
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
9393
| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
94+
| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
9495
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
9596
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
9697
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
@@ -115,6 +116,7 @@ for more information about extending configuration files.
115116
[no-large-snapshots]: docs/rules/no-large-snapshots.md
116117
[no-test-prefixes]: docs/rules/no-test-prefixes.md
117118
[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
119+
[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
118120
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
119121
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
120122
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md

docs/rules/prefer-strict-equal.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 shape. The believe is that imposing a stricter equality
5+
results in safer tests.
6+
7+
## Rule details
8+
9+
This rule triggers a warning if `toEqual()` is used to assert equality.
10+
11+
This rule is enabled by default.
12+
13+
### Default configuration
14+
15+
The following pattern is considered warning:
16+
17+
```js
18+
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true
19+
```
20+
21+
The following pattern is not warning:
22+
23+
```js
24+
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false
25+
```

index.js

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

2223
const snapshotProcessor = require('./processors/snapshot-processor');
2324

@@ -83,5 +84,6 @@ module.exports = {
8384
'prefer-expect-assertions': preferExpectAssertions,
8485
'valid-expect-in-promise': validExpectInPromise,
8586
'prefer-inline-snapshots': preferInlineSnapshots,
87+
'prefer-strict-equal': preferStrictEqual,
8688
},
8789
};
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)