Skip to content

Commit eacb78a

Browse files
JrSchildSimenB
authored andcommitted
feat(matchers): add toStrictEqual as equality matcher (#412)
Fixes #411
1 parent 1c40d1a commit eacb78a

9 files changed

+95
-17
lines changed

docs/rules/prefer-to-be-null.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ asserting expections on null value.
55

66
## Rule details
77

8-
This rule triggers a warning if `toBe()` is used to assert a null value.
8+
This rule triggers a warning if `toBe()`, `isEqual()` or `toStrictEqual()` is
9+
used to assert a null value.
910

1011
```js
1112
expect(null).toBe(null);
@@ -15,10 +16,14 @@ This rule is enabled by default.
1516

1617
### Default configuration
1718

18-
The following pattern is considered warning:
19+
The following patterns are considered warnings:
1920

2021
```js
2122
expect(null).toBe(null);
23+
24+
expect(null).isEqual(null);
25+
26+
expect(null).toStrictEqual(null);
2227
```
2328

2429
The following pattern is not warning:

docs/rules/prefer-to-be-undefined.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ asserting expections on undefined value.
55

66
## Rule details
77

8-
This rule triggers a warning if `toBe()` is used to assert a undefined value.
8+
This rule triggers a warning if `toBe()`, `isEqual()` or `toStrictEqual()` is
9+
used to assert an undefined value.
910

1011
```js
1112
expect(undefined).toBe(undefined);
@@ -15,10 +16,14 @@ This rule is enabled by default.
1516

1617
### Default configuration
1718

18-
The following pattern is considered warning:
19+
The following patterns are considered warnings:
1920

2021
```js
2122
expect(undefined).toBe(undefined);
23+
24+
expect(undefined).isEqual(undefined);
25+
26+
expect(undefined).toStrictEqual(undefined);
2227
```
2328

2429
The following pattern is not warning:

docs/rules/prefer-to-contain.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ asserting expectations on an array containing an object.
55

66
## Rule details
77

8-
This rule triggers a warning if `toBe()` or `isEqual()` is used to assert object
9-
inclusion in an array
8+
This rule triggers a warning if `toBe()`, `isEqual()` or `toStrictEqual()` is
9+
used to assert object inclusion in an array
1010

1111
```js
1212
expect(a.includes(b)).toBe(true);
@@ -22,26 +22,24 @@ expect(a.includes(b)).toBe(false);
2222

2323
### Default configuration
2424

25-
The following patterns are considered a warning:
25+
The following patterns are considered warnings:
2626

2727
```js
2828
expect(a.includes(b)).toBe(true);
29-
```
3029

31-
```js
3230
expect(a.includes(b)).not.toBe(true);
33-
```
3431

35-
```js
3632
expect(a.includes(b)).toBe(false);
33+
34+
expect(a.includes(b)).toEqual(true);
35+
36+
expect(a.includes(b)).toStrictEqual(true);
3737
```
3838

39-
The following patterns are not a warning:
39+
The following patterns are not considered warnings:
4040

4141
```js
4242
expect(a).toContain(b);
43-
```
4443

45-
```js
4644
expect(a).not.toContain(b);
4745
```

docs/rules/prefer-to-have-length.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ asserting expectations on object's length property.
55

66
## Rule details
77

8-
This rule triggers a warning if `toBe()` is used to assert object's length
9-
property.
8+
This rule triggers a warning if `toBe()`, `isEqual()` or `toStrictEqual()` is
9+
used to assert object's length property.
1010

1111
```js
1212
expect(files.length).toBe(1);
@@ -16,10 +16,14 @@ This rule is enabled by default.
1616

1717
### Default configuration
1818

19-
The following pattern is considered warning:
19+
The following patterns are considered warnings:
2020

2121
```js
2222
expect(files.length).toBe(1);
23+
24+
expect(files.length).toEqual(1);
25+
26+
expect(files.length).toStrictEqual(1);
2327
```
2428

2529
The following pattern is not warning:

src/rules/__tests__/prefer-to-be-null.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ ruleTester.run('prefer-to-be-null', rule, {
3131
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
3232
output: 'expect(null).toBeNull();',
3333
},
34+
{
35+
code: 'expect(null).toStrictEqual(null);',
36+
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
37+
output: 'expect(null).toBeNull();',
38+
},
3439
{
3540
code: 'expect("a string").not.toBe(null);',
3641
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
@@ -41,6 +46,11 @@ ruleTester.run('prefer-to-be-null', rule, {
4146
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
4247
output: 'expect("a string").not.toBeNull();',
4348
},
49+
{
50+
code: 'expect("a string").not.toStrictEqual(null);',
51+
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
52+
output: 'expect("a string").not.toBeNull();',
53+
},
4454
],
4555
});
4656

src/rules/__tests__/prefer-to-be-undefined.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ ruleTester.run('prefer-to-be-undefined', rule, {
2929
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }],
3030
output: 'expect(undefined).toBeUndefined();',
3131
},
32+
{
33+
code: 'expect(undefined).toStrictEqual(undefined);',
34+
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }],
35+
output: 'expect(undefined).toBeUndefined();',
36+
},
3237
{
3338
code: 'expect("a string").not.toBe(undefined);',
3439
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
@@ -39,6 +44,11 @@ ruleTester.run('prefer-to-be-undefined', rule, {
3944
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
4045
output: 'expect("a string").not.toBeUndefined();',
4146
},
47+
{
48+
code: 'expect("a string").not.toStrictEqual(undefined);',
49+
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
50+
output: 'expect("a string").not.toBeUndefined();',
51+
},
4252
],
4353
});
4454

src/rules/__tests__/prefer-to-contain.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ ruleTester.run('prefer-to-contain', rule, {
7272
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
7373
output: 'expect(a).not.toContain(b);',
7474
},
75+
{
76+
code: 'expect(a.includes(b)).toStrictEqual(true);',
77+
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
78+
output: 'expect(a).toContain(b);',
79+
},
80+
{
81+
code: 'expect(a.includes(b)).toStrictEqual(false);',
82+
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
83+
output: 'expect(a).not.toContain(b);',
84+
},
85+
{
86+
code: 'expect(a.includes(b)).not.toStrictEqual(false);',
87+
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
88+
output: 'expect(a).toContain(b);',
89+
},
90+
{
91+
code: 'expect(a.includes(b)).not.toStrictEqual(true);',
92+
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
93+
output: 'expect(a).not.toContain(b);',
94+
},
7595
{
7696
code: 'expect(a.test(t).includes(b.test(p))).toEqual(true);',
7797
errors: [{ messageId: 'useToContain', column: 39, line: 1 }],
@@ -112,6 +132,26 @@ ruleTester.run('prefer-to-contain', rule, {
112132
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
113133
output: 'expect([{a:1}]).toContain({a:1});',
114134
},
135+
{
136+
code: 'expect([{a:1}].includes({a:1})).toStrictEqual(true);',
137+
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
138+
output: 'expect([{a:1}]).toContain({a:1});',
139+
},
140+
{
141+
code: 'expect([{a:1}].includes({a:1})).toStrictEqual(false);',
142+
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
143+
output: 'expect([{a:1}]).not.toContain({a:1});',
144+
},
145+
{
146+
code: 'expect([{a:1}].includes({a:1})).not.toStrictEqual(true);',
147+
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
148+
output: 'expect([{a:1}]).not.toContain({a:1});',
149+
},
150+
{
151+
code: 'expect([{a:1}].includes({a:1})).not.toStrictEqual(false);',
152+
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
153+
output: 'expect([{a:1}]).toContain({a:1});',
154+
},
115155
],
116156
});
117157

src/rules/__tests__/prefer-to-have-length.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ ruleTester.run('prefer-to-have-length', rule, {
3636
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
3737
output: 'expect(files).toHaveLength(1);',
3838
},
39+
{
40+
code: 'expect(files.length).toStrictEqual(1);',
41+
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
42+
output: 'expect(files).toHaveLength(1);',
43+
},
3944
],
4045
});

src/rules/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ export enum ModifierName {
341341
enum EqualityMatcher {
342342
toBe = 'toBe',
343343
toEqual = 'toEqual',
344+
toStrictEqual = 'toStrictEqual',
344345
}
345346

346347
export const isParsedEqualityMatcherCall = (

0 commit comments

Comments
 (0)