Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matcher): add toStrictEqual as equality matcher #412

Merged
merged 3 commits into from Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/rules/prefer-to-be-null.md
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions docs/rules/prefer-to-be-undefined.md
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
18 changes: 8 additions & 10 deletions docs/rules/prefer-to-contain.md
Expand Up @@ -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);
Expand All @@ -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);
```
10 changes: 7 additions & 3 deletions docs/rules/prefer-to-have-length.md
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/rules/__tests__/prefer-to-be-null.test.ts
Expand Up @@ -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 }],
Expand All @@ -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();',
},
],
});

Expand Down
10 changes: 10 additions & 0 deletions src/rules/__tests__/prefer-to-be-undefined.test.ts
Expand Up @@ -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 }],
Expand All @@ -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();',
},
],
});

Expand Down
40 changes: 40 additions & 0 deletions src/rules/__tests__/prefer-to-contain.test.ts
Expand Up @@ -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 }],
Expand Down Expand Up @@ -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});',
},
],
});

Expand Down
5 changes: 5 additions & 0 deletions src/rules/__tests__/prefer-to-have-length.test.ts
Expand Up @@ -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);',
},
],
});
1 change: 1 addition & 0 deletions src/rules/utils.ts
Expand Up @@ -341,6 +341,7 @@ export enum ModifierName {
enum EqualityMatcher {
toBe = 'toBe',
toEqual = 'toEqual',
toStrictEqual = 'toStrictEqual',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, only code change. awesome, thanks for verifying by adding some tests, though!

Copy link
Collaborator

@G-Rath G-Rath Sep 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's one question I can cross off my list 😂

I had this pegged to ask about at some point, but never got around to it.
Cheers to all for wrapping up this loose end :)

}

export const isParsedEqualityMatcherCall = (
Expand Down