File tree Expand file tree Collapse file tree 5 files changed +77
-0
lines changed Expand file tree Collapse file tree 5 files changed +77
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ const validExpect = require('./rules/valid-expect');
2020const preferExpectAssertions = require ( './rules/prefer-expect-assertions' ) ;
2121const validExpectInPromise = require ( './rules/valid-expect-in-promise' ) ;
2222const preferInlineSnapshots = require ( './rules/prefer-inline-snapshots' ) ;
23+ const preferStrictEqual = require ( './rules/prefer-strict-equal' ) ;
2324
2425const 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} ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments