Skip to content

Commit

Permalink
feat(rules): add prefer-strict-equal (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
tryggvigy authored and SimenB committed Aug 11, 2018
1 parent e42c9e3 commit 92b1d5c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -93,6 +93,7 @@ for more information about extending configuration files.
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
| [no-test-return-statement][] | Disallow explicitly returning from tests | | |
| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
Expand All @@ -119,6 +120,7 @@ for more information about extending configuration files.
[no-test-prefixes]: docs/rules/no-test-prefixes.md
[no-test-return-statement]: docs/rules/no-test-return-statement.md
[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
Expand Down
22 changes: 22 additions & 0 deletions docs/rules/prefer-strict-equal.md
@@ -0,0 +1,22 @@
# 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:

```js
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true
```

The following pattern is not warning:

```js
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false
```
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -20,6 +20,7 @@ const validExpect = require('./rules/valid-expect');
const preferExpectAssertions = require('./rules/prefer-expect-assertions');
const validExpectInPromise = require('./rules/valid-expect-in-promise');
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
const preferStrictEqual = require('./rules/prefer-strict-equal');

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

Expand Down Expand Up @@ -87,5 +88,6 @@ module.exports = {
'prefer-expect-assertions': preferExpectAssertions,
'valid-expect-in-promise': validExpectInPromise,
'prefer-inline-snapshots': preferInlineSnapshots,
'prefer-strict-equal': preferStrictEqual,
},
};
23 changes: 23 additions & 0 deletions rules/__tests__/prefer-strict-equal.test.js
@@ -0,0 +1,23 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rule = require('../prefer-strict-equal');

const ruleTester = new RuleTester();

ruleTester.run('prefer-strict-equal', rule, {
valid: ['expect(something).toStrictEqual(somethingElse);'],
invalid: [
{
code: 'expect(something).toEqual(somethingElse);',
errors: [
{
message: 'Use toStrictEqual() instead',
column: 19,
line: 1,
},
],
output: 'expect(something).toStrictEqual(somethingElse);',
},
],
});
28 changes: 28 additions & 0 deletions rules/prefer-strict-equal.js
@@ -0,0 +1,28 @@
'use strict';

const getDocsUrl = require('./util').getDocsUrl;

module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
const propertyName = node.callee.property && node.callee.property.name;
if (propertyName === 'toEqual') {
context.report({
fix(fixer) {
return [fixer.replaceText(node.callee.property, 'toStrictEqual')];
},
message: 'Use toStrictEqual() instead',
node: node.callee.property,
});
}
},
};
},
};

0 comments on commit 92b1d5c

Please sign in to comment.