Skip to content

Commit

Permalink
feat(rules): add prefer-strict-equal
Browse files Browse the repository at this point in the history
  • Loading branch information
tryggvigy committed Jul 29, 2018
1 parent 5bc0eea commit 1bfcd98
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -91,6 +91,7 @@ for more information about extending configuration files.
| [no-large-snapshots][] | Disallow large snapshots | | |
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
| [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 @@ -115,6 +116,7 @@ for more information about extending configuration files.
[no-large-snapshots]: docs/rules/no-large-snapshots.md
[no-test-prefixes]: docs/rules/no-test-prefixes.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
25 changes: 25 additions & 0 deletions docs/rules/prefer-strict-equal.md
@@ -0,0 +1,25 @@
# Suggest using `toStrictEqual()` ` (prefer-strict-equal)

`toStrictEqual` not only checks that two objects contain the same data but also
that they have the same shape (keys). The belive is that imposing a stricter
equality results in fewer undesired behavious slipping through tests.

## Rule details

This rule triggers a warning if `toEqual()` is used to assert equality.

This rule is enabled by default.

### Default configuration

The following pattern is considered warning:

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

The following pattern is not warning:

```js
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' });
```
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -18,6 +18,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 @@ -83,5 +84,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 1bfcd98

Please sign in to comment.