Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add prefer-strict-equal
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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);', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |