๐Ÿ“ฆ Turbo87 / eslint-plugin-chai-expect

๐Ÿ“„ README.md ยท 138 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138# eslint-plugin-chai-expect

[![Build Status](https://img.shields.io/travis/Turbo87/eslint-plugin-chai-expect/master.svg)](https://travis-ci.org/Turbo87/eslint-plugin-chai-expect)

ESLint plugin that checks for common chai.js `expect()` mistakes

> [!IMPORTANT]
> The `recommended` preset is for the ESLint legacy configuration system
> (`.eslintrc.json`). The `recommended-flat` configuration is for the new flat
> configuration system.

## Requirements

- Node.js 6 or above
- ESLint 4.x or 5.x or 6.x


## Installation

```
npm install --save-dev eslint-plugin-chai-expect
```


## Configuration

### Legacy ESLint Configuration Format (.eslintrc.json)

Add a `plugins` section and specify `chai-expect` as a plugin:

```json
{
  "plugins": [
    "chai-expect"
  ]
}
```

Enable the rules that you would like to use:

```json
{
  "rules": {
    "chai-expect/no-inner-compare": 2,
    "chai-expect/no-inner-literal": 2,
    "chai-expect/missing-assertion": 2,
    "chai-expect/terminating-properties": 2
  }
}
```

Or, if you just want the above defaults, you can avoid all of the above
and just extend the config:

```json
{
  "extends": ["plugin:chai-expect/recommended"]
}
```

### Flat ESLint Configuration Format (eslint.config.js)

Add a `plugins` section and specify `chai-expect` as a plugin and enable the rules that you would like to use:

```js
import chaiExpectPlugin from 'eslint-plugin-chai-expect';

export default [
  {
    "plugins": {
      "chai-expect": chaiExpectPlugin
    },
    "rules": {
      "chai-expect/no-inner-compare": 2,
      "chai-expect/no-inner-literal": 2,
      "chai-expect/missing-assertion": 2,
      "chai-expect/terminating-properties": 2
    }
  }
];
```

Or, if you just want the above defaults, you can avoid all of the above
and just extend the config:

```js
import chaiExpectPlugin from 'eslint-plugin-chai-expect';

export default [
  chaiExpectPlugin.configs["recommended-flat"],
  {
    // ...
  },
];
```

## Rules

- `no-inner-compare` - Prevent using comparisons in the `expect()` argument
- `no-inner-literal` - Prevent using literals in the `expect()` argument
  (`undefined`, `null`, `NaN`, `(+|-)Infinity`, `this`, booleans, numbers,
  strings, and BigInt or regex literals)
- `missing-assertion` - Prevent calling `expect(...)` without an assertion
  like `.to.be.ok`
- `terminating-properties` - Prevent calling `to.be.ok` and other assertion
  properties as functions


### Additional configuration

#### terminating-properties rule

A number of extensions to chai add additional terminating properties.  For example [chai-http](https://github.com/chaijs/chai-http) adds:

 - headers
 - html
 - ip
 - json
 - redirect
 - text

The terminating-properties rule can be configured to ensure these (or other) additional properties are not used as functions:

```json
{
  "rules": {
    "chai-expect/terminating-properties": ["error", {
      "properties": ["headers", "html", "ip", "json", "redirect", "test"]
    }]
  }
}
```


## License

eslint-plugin-chai-expect is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).