Skip to content

Commit 74078ee

Browse files
authored
feat: import formatting rules from eslint-plugin-jest-formatting (#1563)
* feat: naively copy and paste `eslint-plugin-jest-formatting` rules * refactor: remove unneeded exports * refactor: give each rule a dedicated file * refactor: rename and convert test files to typescript * refactor: use `import` instead of `require` and clean up other imports * refactor: use `createRule` * fix: pass in the correct `name` for each padding rule * refactor: use `@typescript-eslint` types and constants * fix: address some initial typescript errors * refactor: use `messageId` instead of `message` * test: use flat compat `RuleTester` * test: specify full errors instead of counts * test: add basic file for `padding-around-all` * docs: add entries for new padding rules * refactor: address remaining TypeScript issues * refactor: address deprecated `context` method usage * test: remove comments * test: cleanup cases * test: add more cases for coverage * refactor: remove unneeded checks
1 parent 11ef4fc commit 74078ee

30 files changed

+2204
-56
lines changed

README.md

+63-55
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Enforce padding around `afterAll` blocks (`padding-around-after-all-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `afterAll`
11+
statements.
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope.
15+
16+
Examples of **incorrect** code for this rule:
17+
18+
```js
19+
const someText = 'abc';
20+
afterAll(() => {});
21+
describe('someText', () => {});
22+
```
23+
24+
Examples of **correct** code for this rule:
25+
26+
```js
27+
const someText = 'abc';
28+
29+
afterAll(() => {});
30+
31+
describe('someText', () => {});
32+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Enforce padding around `afterEach` blocks (`padding-around-after-each-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `afterEach`
11+
statements.
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope.
15+
16+
Examples of **incorrect** code for this rule:
17+
18+
```js
19+
const something = 123;
20+
afterEach(() => {
21+
// more stuff
22+
});
23+
describe('foo', () => {});
24+
```
25+
26+
Examples of **correct** code for this rule:
27+
28+
```js
29+
const something = 123;
30+
31+
afterEach(() => {
32+
// more stuff
33+
});
34+
35+
describe('foo', () => {});
36+
```

docs/rules/padding-around-all.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Enforce padding around Jest functions (`padding-around-all`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This is a meta rule that simply enables all of the following rules:
11+
12+
- [padding-around-after-all-blocks](padding-around-after-all-blocks.md)
13+
- [padding-around-after-each-blocks](padding-around-after-each-blocks.md)
14+
- [padding-around-before-all-blocks](padding-around-before-all-blocks.md)
15+
- [padding-around-before-each-blocks](padding-around-before-each-blocks.md)
16+
- [padding-around-expect-groups](padding-around-expect-groups.md)
17+
- [padding-around-describe-blocks](padding-around-describe-blocks.md)
18+
- [padding-around-test-blocks](padding-around-test-blocks.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Enforce padding around `beforeAll` blocks (`padding-around-before-all-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after `beforeAll` statements.
11+
12+
Note that it doesn't add/enforce a padding line if it's the last statement in
13+
its scope.
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```js
18+
const something = 123;
19+
beforeAll(() => {
20+
// more stuff
21+
});
22+
describe('foo', () => {});
23+
```
24+
25+
Examples of **correct** code for this rule:
26+
27+
```js
28+
const something = 123;
29+
30+
beforeAll(() => {
31+
// more stuff
32+
});
33+
34+
describe('foo', () => {});
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Enforce padding around `beforeEach` blocks (`padding-around-before-each-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `beforeEach`
11+
statements
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope
15+
16+
Examples of **incorrect** code for this rule:
17+
18+
```js
19+
const something = 123;
20+
beforeEach(() => {
21+
// more stuff
22+
});
23+
describe('foo', () => {});
24+
```
25+
26+
Examples of **correct** code for this rule:
27+
28+
```js
29+
const something = 123;
30+
31+
beforeEach(() => {
32+
// more stuff
33+
});
34+
35+
describe('foo', () => {});
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Enforce padding around `describe` blocks (`padding-around-describe-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `describe`
11+
statements
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope
15+
16+
Examples of **incorrect** code for this rule:
17+
18+
```js
19+
const thing = 123;
20+
describe('foo', () => {
21+
// stuff
22+
});
23+
describe('bar', () => {
24+
// more stuff
25+
});
26+
```
27+
28+
Examples of **correct** code for this rule:
29+
30+
```js
31+
const thing = 123;
32+
33+
describe('foo', () => {
34+
// stuff
35+
});
36+
37+
describe('bar', () => {
38+
// more stuff
39+
});
40+
```
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Enforce padding around `expect` groups (`padding-around-expect-groups`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `expect`
11+
statements
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope and it doesn't add/enforce padding between two or more adjacent
15+
`expect` statements.
16+
17+
Examples of **incorrect** code for this rule:
18+
19+
```js
20+
test('thing one', () => {
21+
let abc = 123;
22+
expect(abc).toEqual(123);
23+
expect(123).toEqual(abc);
24+
abc = 456;
25+
expect(abc).toEqual(456);
26+
});
27+
```
28+
29+
Examples of **correct** code for this rule:
30+
31+
```js
32+
test('thing one', () => {
33+
let abc = 123;
34+
35+
expect(abc).toEqual(123);
36+
expect(123).toEqual(abc);
37+
38+
abc = 456;
39+
40+
expect(abc).toEqual(456);
41+
});
42+
```
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Enforce padding around afterAll blocks (`padding-around-test-blocks`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
## Rule Details
9+
10+
This rule enforces a line of padding before _and_ after 1 or more `test`/`it`
11+
statements
12+
13+
Note that it doesn't add/enforce a padding line if it's the last statement in
14+
its scope
15+
16+
Examples of **incorrect** code for this rule:
17+
18+
```js
19+
const thing = 123;
20+
test('foo', () => {});
21+
test('bar', () => {});
22+
```
23+
24+
```js
25+
const thing = 123;
26+
it('foo', () => {});
27+
it('bar', () => {});
28+
```
29+
30+
Examples of **correct** code for this rule:
31+
32+
```js
33+
const thing = 123;
34+
35+
test('foo', () => {});
36+
37+
test('bar', () => {});
38+
```
39+
40+
```js
41+
const thing = 123;
42+
43+
it('foo', () => {});
44+
45+
it('bar', () => {});
46+
```

src/__tests__/__snapshots__/rules.test.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ exports[`rules should export configs that refer to actual rules 1`] = `
3737
"jest/no-test-prefixes": "error",
3838
"jest/no-test-return-statement": "error",
3939
"jest/no-untyped-mock-factory": "error",
40+
"jest/padding-around-after-all-blocks": "error",
41+
"jest/padding-around-after-each-blocks": "error",
42+
"jest/padding-around-all": "error",
43+
"jest/padding-around-before-all-blocks": "error",
44+
"jest/padding-around-before-each-blocks": "error",
45+
"jest/padding-around-describe-blocks": "error",
46+
"jest/padding-around-expect-groups": "error",
47+
"jest/padding-around-test-blocks": "error",
4048
"jest/prefer-called-with": "error",
4149
"jest/prefer-comparison-matcher": "error",
4250
"jest/prefer-each": "error",
@@ -120,6 +128,14 @@ exports[`rules should export configs that refer to actual rules 1`] = `
120128
"jest/no-test-prefixes": "error",
121129
"jest/no-test-return-statement": "error",
122130
"jest/no-untyped-mock-factory": "error",
131+
"jest/padding-around-after-all-blocks": "error",
132+
"jest/padding-around-after-each-blocks": "error",
133+
"jest/padding-around-all": "error",
134+
"jest/padding-around-before-all-blocks": "error",
135+
"jest/padding-around-before-each-blocks": "error",
136+
"jest/padding-around-describe-blocks": "error",
137+
"jest/padding-around-expect-groups": "error",
138+
"jest/padding-around-test-blocks": "error",
123139
"jest/prefer-called-with": "error",
124140
"jest/prefer-comparison-matcher": "error",
125141
"jest/prefer-each": "error",

src/__tests__/rules.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22
import { resolve } from 'path';
33
import plugin from '../';
44

5-
const numberOfRules = 54;
5+
const numberOfRules = 62;
66
const ruleNames = Object.keys(plugin.rules);
77
const deprecatedRules = Object.entries(plugin.rules)
88
.filter(([, rule]) => rule.meta.deprecated)

0 commit comments

Comments
 (0)