Skip to content

Commit bb2d09e

Browse files
committed
chore: merge no-empty-title into valid-title
BREAKING CHANEG: delete `no-empty-title` - use `valid-title` instead
1 parent 2ce233c commit bb2d09e

File tree

8 files changed

+156
-145
lines changed

8 files changed

+156
-145
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ installations requiring long-term consistency.
117117
| [no-commented-out-tests][] | Disallow commented out tests | | |
118118
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
119119
| [no-duplicate-hooks][] | Disallow duplicate hooks within a `describe` block | | |
120-
| [no-empty-title][] | Disallow empty titles | | |
121120
| [no-expect-resolves][] | Disallow using `expect().resolves` | | |
122121
| [no-export][] | Disallow export from test files | | |
123122
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
@@ -173,7 +172,6 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
173172
[no-commented-out-tests]: docs/rules/no-commented-out-tests.md
174173
[no-disabled-tests]: docs/rules/no-disabled-tests.md
175174
[no-duplicate-hooks]: docs/rules/no-duplicate-hooks.md
176-
[no-empty-title]: docs/rules/no-empty-title.md
177175
[no-expect-resolves]: docs/rules/no-expect-resolves.md
178176
[no-export]: docs/rules/no-export.md
179177
[no-focused-tests]: docs/rules/no-focused-tests.md

docs/rules/no-empty-title.md

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

docs/rules/valid-title.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,44 @@
22

33
Checks that the title of Jest blocks are valid by ensuring that titles are:
44

5+
- not empty,
56
- not prefixed with their block name,
67
- have no leading or trailing spaces
78

89
## Rule Details
910

11+
**emptyTitle**
12+
13+
An empty title is not informative, and serves little purpose.
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```js
18+
describe('', () => {});
19+
describe('foo', () => {
20+
it('', () => {});
21+
});
22+
it('', () => {});
23+
test('', () => {});
24+
xdescribe('', () => {});
25+
xit('', () => {});
26+
xtest('', () => {});
27+
```
28+
29+
Examples of **correct** code for this rule:
30+
31+
```js
32+
describe('foo', () => {});
33+
describe('foo', () => {
34+
it('bar', () => {});
35+
});
36+
test('foo', () => {});
37+
it('foo', () => {});
38+
xdescribe('foo', () => {});
39+
xit('foo', () => {});
40+
xtest('foo', () => {});
41+
```
42+
1043
**duplicatePrefix**
1144

1245
A describe/ test block should not start with duplicatePrefix

src/__tests__/rules.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'path';
33
import plugin from '../';
44

55
const ruleNames = Object.keys(plugin.rules);
6-
const numberOfRules = 41;
6+
const numberOfRules = 40;
77

88
describe('rules', () => {
99
it('should have a corresponding doc for each rule', () => {

src/rules/__tests__/no-empty-title.test.ts

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

src/rules/__tests__/valid-title.test.ts

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,117 @@ const ruleTester = new TSESLint.RuleTester({
77
},
88
});
99

10+
ruleTester.run('no-empty-title', rule, {
11+
valid: [
12+
'describe()',
13+
'someFn("", function () {})',
14+
'describe(1, function () {})',
15+
'describe("foo", function () {})',
16+
'describe("foo", function () { it("bar", function () {}) })',
17+
'test("foo", function () {})',
18+
'test(`foo`, function () {})',
19+
'test(`${foo}`, function () {})',
20+
"it('foo', function () {})",
21+
"xdescribe('foo', function () {})",
22+
"xit('foo', function () {})",
23+
"xtest('foo', function () {})",
24+
],
25+
invalid: [
26+
{
27+
code: 'describe("", function () {})',
28+
errors: [
29+
{
30+
messageId: 'emptyTitle',
31+
column: 1,
32+
line: 1,
33+
data: { jestFunctionName: 'describe' },
34+
},
35+
],
36+
},
37+
{
38+
code: ["describe('foo', () => {", "it('', () => {})", '})'].join('\n'),
39+
errors: [
40+
{
41+
messageId: 'emptyTitle',
42+
column: 1,
43+
line: 2,
44+
data: { jestFunctionName: 'test' },
45+
},
46+
],
47+
},
48+
{
49+
code: 'it("", function () {})',
50+
errors: [
51+
{
52+
messageId: 'emptyTitle',
53+
column: 1,
54+
line: 1,
55+
data: { jestFunctionName: 'test' },
56+
},
57+
],
58+
},
59+
{
60+
code: 'test("", function () {})',
61+
errors: [
62+
{
63+
messageId: 'emptyTitle',
64+
column: 1,
65+
line: 1,
66+
data: { jestFunctionName: 'test' },
67+
},
68+
],
69+
},
70+
{
71+
code: 'test(``, function () {})',
72+
errors: [
73+
{
74+
messageId: 'emptyTitle',
75+
column: 1,
76+
line: 1,
77+
data: { jestFunctionName: 'test' },
78+
},
79+
],
80+
},
81+
{
82+
code: "xdescribe('', () => {})",
83+
errors: [
84+
{
85+
messageId: 'emptyTitle',
86+
column: 1,
87+
line: 1,
88+
data: { jestFunctionName: 'describe' },
89+
},
90+
],
91+
},
92+
{
93+
code: "xit('', () => {})",
94+
errors: [
95+
{
96+
messageId: 'emptyTitle',
97+
column: 1,
98+
line: 1,
99+
data: { jestFunctionName: 'test' },
100+
},
101+
],
102+
},
103+
{
104+
code: "xtest('', () => {})",
105+
errors: [
106+
{
107+
messageId: 'emptyTitle',
108+
column: 1,
109+
line: 1,
110+
data: { jestFunctionName: 'test' },
111+
},
112+
],
113+
},
114+
],
115+
});
116+
10117
ruleTester.run('no-accidental-space', rule, {
11118
valid: [
12119
'it()',
13120
'describe()',
14-
'it("")',
15121
'it.each()()',
16122
'describe("foo", function () {})',
17123
'describe(6, function () {})',

src/rules/no-empty-title.ts

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

src/rules/valid-title.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
TSESTree,
44
} from '@typescript-eslint/experimental-utils';
55
import {
6+
DescribeAlias,
7+
TestCaseName,
68
createRule,
79
getNodeName,
810
getStringValue,
@@ -23,6 +25,7 @@ export default createRule({
2325
recommended: false,
2426
},
2527
messages: {
28+
emptyTitle: '{{ jestFunctionName }} should not have an empty title',
2629
duplicatePrefix: 'should not have duplicate prefix',
2730
accidentalSpace: 'should not have leading or trailing spaces',
2831
},
@@ -47,6 +50,18 @@ export default createRule({
4750
const title = getStringValue(argument);
4851

4952
if (!title) {
53+
if (typeof title === 'string') {
54+
context.report({
55+
messageId: 'emptyTitle',
56+
data: {
57+
jestFunctionName: isDescribe(node)
58+
? DescribeAlias.describe
59+
: TestCaseName.test,
60+
},
61+
node,
62+
});
63+
}
64+
5065
return;
5166
}
5267

0 commit comments

Comments
 (0)