Skip to content
Permalink
Browse files
feat(matchers): add toStrictEqual as equality matcher (#412)
Fixes #411
  • Loading branch information
JrSchild authored and SimenB committed Sep 5, 2019
1 parent 1c40d1a commit eacb78a5e1f14cc6ebf6f6ad2a2bfc167abdc348
@@ -5,7 +5,8 @@ asserting expections on null value.

## Rule details

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

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

### Default configuration

The following pattern is considered warning:
The following patterns are considered warnings:

```js
expect(null).toBe(null);
expect(null).isEqual(null);
expect(null).toStrictEqual(null);
```

The following pattern is not warning:
@@ -5,7 +5,8 @@ asserting expections on undefined value.

## Rule details

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

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

### Default configuration

The following pattern is considered warning:
The following patterns are considered warnings:

```js
expect(undefined).toBe(undefined);
expect(undefined).isEqual(undefined);
expect(undefined).toStrictEqual(undefined);
```

The following pattern is not warning:
@@ -5,8 +5,8 @@ asserting expectations on an array containing an object.

## Rule details

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

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

### Default configuration

The following patterns are considered a warning:
The following patterns are considered warnings:

```js
expect(a.includes(b)).toBe(true);
```
```js
expect(a.includes(b)).not.toBe(true);
```
```js
expect(a.includes(b)).toBe(false);
expect(a.includes(b)).toEqual(true);
expect(a.includes(b)).toStrictEqual(true);
```

The following patterns are not a warning:
The following patterns are not considered warnings:

```js
expect(a).toContain(b);
```
```js
expect(a).not.toContain(b);
```
@@ -5,8 +5,8 @@ asserting expectations on object's length property.

## Rule details

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

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

### Default configuration

The following pattern is considered warning:
The following patterns are considered warnings:

```js
expect(files.length).toBe(1);
expect(files.length).toEqual(1);
expect(files.length).toStrictEqual(1);
```

The following pattern is not warning:
@@ -31,6 +31,11 @@ ruleTester.run('prefer-to-be-null', rule, {
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
output: 'expect(null).toBeNull();',
},
{
code: 'expect(null).toStrictEqual(null);',
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
output: 'expect(null).toBeNull();',
},
{
code: 'expect("a string").not.toBe(null);',
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
@@ -41,6 +46,11 @@ ruleTester.run('prefer-to-be-null', rule, {
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
output: 'expect("a string").not.toBeNull();',
},
{
code: 'expect("a string").not.toStrictEqual(null);',
errors: [{ messageId: 'useToBeNull', column: 24, line: 1 }],
output: 'expect("a string").not.toBeNull();',
},
],
});

@@ -29,6 +29,11 @@ ruleTester.run('prefer-to-be-undefined', rule, {
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }],
output: 'expect(undefined).toBeUndefined();',
},
{
code: 'expect(undefined).toStrictEqual(undefined);',
errors: [{ messageId: 'useToBeUndefined', column: 19, line: 1 }],
output: 'expect(undefined).toBeUndefined();',
},
{
code: 'expect("a string").not.toBe(undefined);',
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
@@ -39,6 +44,11 @@ ruleTester.run('prefer-to-be-undefined', rule, {
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
output: 'expect("a string").not.toBeUndefined();',
},
{
code: 'expect("a string").not.toStrictEqual(undefined);',
errors: [{ messageId: 'useToBeUndefined', column: 24, line: 1 }],
output: 'expect("a string").not.toBeUndefined();',
},
],
});

@@ -72,6 +72,26 @@ ruleTester.run('prefer-to-contain', rule, {
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).not.toContain(b);',
},
{
code: 'expect(a.includes(b)).toStrictEqual(true);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).toContain(b);',
},
{
code: 'expect(a.includes(b)).toStrictEqual(false);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).not.toContain(b);',
},
{
code: 'expect(a.includes(b)).not.toStrictEqual(false);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).toContain(b);',
},
{
code: 'expect(a.includes(b)).not.toStrictEqual(true);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).not.toContain(b);',
},
{
code: 'expect(a.test(t).includes(b.test(p))).toEqual(true);',
errors: [{ messageId: 'useToContain', column: 39, line: 1 }],
@@ -112,6 +132,26 @@ ruleTester.run('prefer-to-contain', rule, {
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
output: 'expect([{a:1}]).toContain({a:1});',
},
{
code: 'expect([{a:1}].includes({a:1})).toStrictEqual(true);',
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
output: 'expect([{a:1}]).toContain({a:1});',
},
{
code: 'expect([{a:1}].includes({a:1})).toStrictEqual(false);',
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
output: 'expect([{a:1}]).not.toContain({a:1});',
},
{
code: 'expect([{a:1}].includes({a:1})).not.toStrictEqual(true);',
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
output: 'expect([{a:1}]).not.toContain({a:1});',
},
{
code: 'expect([{a:1}].includes({a:1})).not.toStrictEqual(false);',
errors: [{ messageId: 'useToContain', column: 33, line: 1 }],
output: 'expect([{a:1}]).toContain({a:1});',
},
],
});

@@ -36,5 +36,10 @@ ruleTester.run('prefer-to-have-length', rule, {
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
output: 'expect(files).toHaveLength(1);',
},
{
code: 'expect(files.length).toStrictEqual(1);',
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
output: 'expect(files).toHaveLength(1);',
},
],
});
@@ -341,6 +341,7 @@ export enum ModifierName {
enum EqualityMatcher {
toBe = 'toBe',
toEqual = 'toEqual',
toStrictEqual = 'toStrictEqual',
}

export const isParsedEqualityMatcherCall = (

0 comments on commit eacb78a

Please sign in to comment.