Skip to content

Commit

Permalink
chore: add prefer-to-contain rule
Browse files Browse the repository at this point in the history
  • Loading branch information
katakonst committed Oct 14, 2018
1 parent 0276985 commit 8227f3e
Show file tree
Hide file tree
Showing 6 changed files with 7,631 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -97,6 +97,7 @@ for more information about extending configuration files.
| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
| [prefer-to-contain][] | Suggest using `toContain()` | | ![fixable-green][] |
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
| [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] |
| [require-tothrow-message][] | Require that `toThrow()` and `toThrowError` includes a message | | |
Expand Down Expand Up @@ -126,6 +127,7 @@ for more information about extending configuration files.
[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
[prefer-to-contain]: docs/rules/prefer-to-contain.md
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
[prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md
[require-tothrow-message]: docs/rules/require-tothrow-message.md
Expand Down
39 changes: 39 additions & 0 deletions docs/rules/prefer-to-contain.md
@@ -0,0 +1,39 @@
# 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)).toBe(false);
```

### Default configuration

The following patterns are considered a warning:

```js
expect(a.includes(b)).toBe(true);
```

```js
expect(a.includes(b)).toBe(false);
```

The following patterns are not a warning:

```js
expect(a).toContain(b);
```

```js
expect(a).toContain(b);
```
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -14,6 +14,7 @@ const noTestPrefixes = require('./rules/no-test-prefixes');
const noTestReturnStatement = require('./rules/no-test-return-statement');
const preferToBeNull = require('./rules/prefer-to-be-null');
const preferToBeUndefined = require('./rules/prefer-to-be-undefined');
const preferToContain = require('./rules/prefer-to-contain');
const preferToHaveLength = require('./rules/prefer-to-have-length');
const validDescribe = require('./rules/valid-describe');
const validExpect = require('./rules/valid-expect');
Expand Down Expand Up @@ -84,6 +85,7 @@ module.exports = {
'no-test-return-statement': noTestReturnStatement,
'prefer-to-be-null': preferToBeNull,
'prefer-to-be-undefined': preferToBeUndefined,
'prefer-to-contain': preferToContain,
'prefer-to-have-length': preferToHaveLength,
'valid-describe': validDescribe,
'valid-expect': validExpect,
Expand Down

0 comments on commit 8227f3e

Please sign in to comment.