-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefer-to-contain rule (#174)
Fixes #100
- Loading branch information
Showing
5 changed files
with
385 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,47 @@ | ||
# Suggest using `toContain()` (prefer-to-contain) | ||
|
||
In order to have a better failure message, `toContain()` should be used upon | ||
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 | ||
|
||
```js | ||
expect(a.includes(b)).toBe(true); | ||
``` | ||
|
||
```js | ||
expect(a.includes(b)).not.toBe(true); | ||
``` | ||
|
||
```js | ||
expect(a.includes(b)).toBe(false); | ||
``` | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered a warning: | ||
|
||
```js | ||
expect(a.includes(b)).toBe(true); | ||
``` | ||
|
||
```js | ||
expect(a.includes(b)).not.toBe(true); | ||
``` | ||
|
||
```js | ||
expect(a.includes(b)).toBe(false); | ||
``` | ||
|
||
The following patterns are not a warning: | ||
|
||
```js | ||
expect(a).toContain(b); | ||
``` | ||
|
||
```js | ||
expect(a).not.toContain(b); | ||
``` |
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,207 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../prefer-to-contain'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer-to-contain', rule, { | ||
valid: [ | ||
'expect(a).toContain(b);', | ||
"expect(a.name).toBe('b');", | ||
'expect(a).toBe(true);', | ||
`expect(a).toEqual(b)`, | ||
`expect(a.test(c)).toEqual(b)`, | ||
`expect(a.includes(b)).toEqual()`, | ||
`expect(a.includes(b)).toEqual("test")`, | ||
`expect(a.includes(b)).toBe("test")`, | ||
`expect(a.includes()).toEqual()`, | ||
`expect(a.includes()).toEqual(true)`, | ||
`expect(a.includes(b,c)).toBe(true)`, | ||
`expect([{a:1}]).toContain({a:1})`, | ||
`expect([1].includes(1)).toEqual`, | ||
`expect([1].includes).toEqual`, | ||
`expect([1].includes).not`, | ||
`expect(a.test(b)).resolves.toEqual(true)`, | ||
`expect(a.test(b)).resolves.not.toEqual(true)`, | ||
`expect(a).not.toContain(b)`, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(a.includes(b)).toEqual(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).toEqual(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).not.toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).not.toEqual(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).not.toEqual(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).not.toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).toBe(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).not.toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).not.toBe(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.includes(b)).not.toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 23, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a).not.toContain(b);', | ||
}, | ||
{ | ||
code: 'expect(a.test(t).includes(b.test(p))).toEqual(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 39, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a.test(t)).toContain(b.test(p));', | ||
}, | ||
{ | ||
code: 'expect(a.test(t).includes(b.test(p))).toEqual(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 39, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a.test(t)).not.toContain(b.test(p));', | ||
}, | ||
{ | ||
code: 'expect(a.test(t).includes(b.test(p))).not.toEqual(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 39, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a.test(t)).not.toContain(b.test(p));', | ||
}, | ||
{ | ||
code: 'expect(a.test(t).includes(b.test(p))).not.toEqual(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 39, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(a.test(t)).toContain(b.test(p));', | ||
}, | ||
{ | ||
code: 'expect([{a:1}].includes({a:1})).toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 33, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect([{a:1}]).toContain({a:1});', | ||
}, | ||
{ | ||
code: 'expect([{a:1}].includes({a:1})).toBe(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 33, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect([{a:1}]).not.toContain({a:1});', | ||
}, | ||
{ | ||
code: 'expect([{a:1}].includes({a:1})).not.toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 33, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect([{a:1}]).not.toContain({a:1});', | ||
}, | ||
{ | ||
code: 'expect([{a:1}].includes({a:1})).not.toBe(false);', | ||
errors: [ | ||
{ | ||
message: 'Use toContain() instead', | ||
column: 33, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect([{a:1}]).toContain({a:1});', | ||
}, | ||
], | ||
}); |
Oops, something went wrong.