Skip to content

Commit f6fff8d

Browse files
authored
Add ESLint 9 support and flat/recommended config (#10)
* Add ESLint 9 support and `flat/recommended` config * Add documentation for Flat Config format * Update readme
1 parent cd97ce5 commit f6fff8d

11 files changed

+151
-36
lines changed

.eslint-doc-generatorrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('eslint-doc-generator').GenerateOptions} */
2+
const config = {
3+
configEmoji: [['flat/recommended', '☑️']],
4+
};
5+
6+
module.exports = config;

README.md

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,49 @@ npm install -D eslint-plugin-proper-tests
1313
```
1414

1515
> [!NOTE]
16-
> For `@typescript-eslint` v7 use version ^1.0.0 of this plugin. For `@typescript-eslint` v8 use version ^2.0.0 of this plugin.
16+
> For `@typescript-eslint` v7 use version ^1.0.0 of this plugin.
17+
> For `@typescript-eslint` v8 use version ^2.0.0 of this plugin.
18+
> For ESLint 9 and flat config use version ^2.1.0 of this plugin.
1719
1820
## Usage
1921

20-
Use the `recommended` shared config in your `.eslintrc` configuration file:
22+
Flat configuration format:
23+
24+
With flat configuration, use the `flat/recommended` shared config in your `eslint.config.mjs` configuration file:
25+
26+
```js
27+
import pluginProperTests from 'eslint-plugin-proper-tests';
28+
29+
export default [
30+
{
31+
...pluginProperTests.configs['flat/recommended']
32+
},
33+
]
34+
```
35+
36+
Run ESLint and enjoy the results.
37+
38+
Or, alternatively, add `proper-tests` to the plugins section of your `eslint.config.mjs` configuration file and configure the rules one by one:
39+
40+
```js
41+
import pluginProperTests from 'eslint-plugin-proper-tests';
42+
43+
export default [
44+
{
45+
plugins: {
46+
'proper-tests': pluginProperTests,
47+
},
48+
rules: {
49+
"proper-tests/no-useless-matcher-to-be-defined": "error"
50+
// other rules...
51+
}
52+
},
53+
]
54+
```
55+
56+
Old configuration format:
57+
58+
With old configuration, use the `recommended` shared config in your `.eslintrc` configuration file:
2159

2260
```js
2361
module.exports = {
@@ -47,8 +85,27 @@ module.exports = {
4785
}
4886
```
4987

88+
## Power of Types
89+
5090
This plugin uses TypeScript to provide more accurate results. To enable this, you need to [configure ESLint to work with TypeScript](https://typescript-eslint.io/getting-started/typed-linting):
5191

92+
Flat configuration format:
93+
94+
```js
95+
import pluginProperTests from 'eslint-plugin-proper-tests';
96+
97+
export default [
98+
{
99+
languageOptions: {
100+
parserOptions: { project: true },
101+
},
102+
},
103+
// ...
104+
]
105+
```
106+
107+
Old configuration format:
108+
52109
```js
53110
module.exports = {
54111
"parser": "@typescript-eslint/parser",
@@ -59,13 +116,37 @@ module.exports = {
59116
}
60117
```
61118

62-
### Running rules only on test-related files
119+
If `parserOptions` is not configured, you will get an error:
120+
121+
```shell
122+
Error while loading rule 'proper-tests/no-useless-matcher-to-be-defined': You have used a rule which requires type information,
123+
but don't have parserOptions set to generate type information for this file
124+
```
125+
126+
## Running rules only on test-related files
63127
64128
The rules provided by this plugin assume that the files they are checking are
65129
test-related. This means it's generally not suitable to include them in your
66130
top-level configuration as that applies to all files being linted which can
67131
include source files.
68132

133+
Flat configuration format:
134+
135+
For `eslint.config.mjs` configs you can use the following syntax:
136+
137+
```js
138+
import pluginProperTests from 'eslint-plugin-proper-tests';
139+
140+
export default [
141+
{
142+
files: ['**/*.test.ts'], // limit rules to test files only
143+
...pluginProperTests.configs['flat/recommended']
144+
},
145+
]
146+
```
147+
148+
Old configuration format:
149+
69150
For `.eslintrc` configs you can use
70151
[overrides](https://eslint.org/docs/user-guide/configuring/configuration-files#how-do-overrides-work)
71152
to have ESLint apply additional rules to specific files:
@@ -94,6 +175,22 @@ module.exports = {
94175
This plugin exports a recommended configuration that enforces good testing
95176
practices.
96177
178+
Flat configuration format:
179+
180+
To enable this configuration with `eslint.config.mjs`, use the `flat/recommended` shared config:
181+
182+
```js
183+
import pluginProperTests from 'eslint-plugin-proper-tests';
184+
185+
export default [
186+
{
187+
...pluginProperTests.configs['flat/recommended']
188+
},
189+
]
190+
```
191+
192+
Old configuration format:
193+
97194
To enable this configuration with `.eslintrc`, use the `extends` property:
98195
99196
```json
@@ -109,14 +206,15 @@ and you are done, no other configuration is needed.
109206
<!-- begin auto-generated rules list -->
110207
111208
💼 Configurations enabled in.\
209+
☑️ Set in the `flat/recommended` configuration.\
112210
✅ Set in the `recommended` configuration.
113211
114-
| Name                             | Description | 💼 |
115-
| :--------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------- | :- |
116-
| [no-long-arrays-in-test-each](docs/rules/no-long-arrays-in-test-each.md) | Disallow using long arrays with objects inside `test.each()` or `it.each()`. Force moving them out of the file. | |
117-
| [no-mixed-expectation-groups](docs/rules/no-mixed-expectation-groups.md) | Disallow mixing expectations for different variables between each other. | |
118-
| [no-useless-matcher-to-be-defined](docs/rules/no-useless-matcher-to-be-defined.md) | Disallow using `.toBeDefined()` matcher when it is known that variable is always defined. | |
119-
| [no-useless-matcher-to-be-null](docs/rules/no-useless-matcher-to-be-null.md) | Disallow using `.toBeNull()` when TypeScript types conflict with it. | |
212+
| Name                             | Description | 💼 |
213+
| :--------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------- | :--- |
214+
| [no-long-arrays-in-test-each](docs/rules/no-long-arrays-in-test-each.md) | Disallow using long arrays with objects inside `test.each()` or `it.each()`. Force moving them out of the file. | ☑️ ✅ |
215+
| [no-mixed-expectation-groups](docs/rules/no-mixed-expectation-groups.md) | Disallow mixing expectations for different variables between each other. | ☑️ ✅ |
216+
| [no-useless-matcher-to-be-defined](docs/rules/no-useless-matcher-to-be-defined.md) | Disallow using `.toBeDefined()` matcher when it is known that variable is always defined. | ☑️ ✅ |
217+
| [no-useless-matcher-to-be-null](docs/rules/no-useless-matcher-to-be-null.md) | Disallow using `.toBeNull()` when TypeScript types conflict with it. | ☑️ ✅ |
120218
121219
<!-- end auto-generated rules list -->
122220

docs/rules/no-long-arrays-in-test-each.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow using long arrays with objects inside `test.each()` or `it.each()`. Force moving them out of the file (`proper-tests/no-long-arrays-in-test-each`)
22

3-
💼 This rule is enabled in the `recommended` config.
3+
💼 This rule is enabled in the following configs: ☑️ `flat/recommended`, ✅ `recommended`.
44

55
<!-- end auto-generated rule header -->
66

docs/rules/no-mixed-expectation-groups.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow mixing expectations for different variables between each other (`proper-tests/no-mixed-expectation-groups`)
22

3-
💼 This rule is enabled in the `recommended` config.
3+
💼 This rule is enabled in the following configs: ☑️ `flat/recommended`, ✅ `recommended`.
44

55
<!-- end auto-generated rule header -->
66

docs/rules/no-useless-matcher-to-be-defined.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow using `.toBeDefined()` matcher when it is known that variable is always defined (`proper-tests/no-useless-matcher-to-be-defined`)
22

3-
💼 This rule is enabled in the `recommended` config.
3+
💼 This rule is enabled in the following configs: ☑️ `flat/recommended`, ✅ `recommended`.
44

55
<!-- end auto-generated rule header -->
66

docs/rules/no-useless-matcher-to-be-null.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow using `.toBeNull()` when TypeScript types conflict with it (`proper-tests/no-useless-matcher-to-be-null`)
22

3-
💼 This rule is enabled in the `recommended` config.
3+
💼 This rule is enabled in the following configs: ☑️ `flat/recommended`, ✅ `recommended`.
44

55
<!-- end auto-generated rule header -->
66

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-proper-tests",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "ESLint rules for writing more proper tests.",
55
"keywords": [
66
"eslint",
@@ -34,7 +34,8 @@
3434
"lint:docs": "markdownlint \"**/*.md\"",
3535
"lint:eslint-docs": "npm run update:eslint-docs -- --check",
3636
"update:eslint-docs": "npm run build && eslint-doc-generator",
37-
"build": "tsc --build ./tsconfig.build.json"
37+
"build": "tsc --build ./tsconfig.build.json",
38+
"ci:build": "npm run build"
3839
},
3940
"dependencies": {
4041
"@typescript-eslint/eslint-plugin": "^8.8.1",
@@ -66,7 +67,7 @@
6667
"dist"
6768
],
6869
"peerDependencies": {
69-
"eslint": ">= 8",
70+
"eslint": "^8.0.0 || ^9.0.0",
7071
"typescript": "*"
7172
},
7273
"peerDependenciesMeta": {

src/configs/recommended.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/plugin.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ describe('proper tests plugin', (): void => {
1111
});
1212

1313
test('it exposes recommended plugin', (): void => {
14-
expect(Object.keys(plugin.configs)).toEqual(['recommended']);
14+
expect(Object.keys(plugin.configs)).toEqual(['recommended', 'flat/recommended']);
1515
});
1616
});

0 commit comments

Comments
 (0)