Skip to content

Commit c1909ab

Browse files
authored
fix(valid-title): report emptyTitle for string-like types (#688)
* fix: report emptyTitle when type is string-like and value is empty * test: add test * docs: fix sample code
1 parent 1dc5c4f commit c1909ab

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

docs/rules/valid-title.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Examples of **incorrect** code for this rule with the `{ "ignoreTypeOfDescribeNa
4040
```js
4141
describe(1, () => {
4242
it('should be a number', () => {
43-
expect(1).toBeNumber()
43+
expect(1).toBe(1)
4444
})
4545
})
4646
```
@@ -50,7 +50,7 @@ Examples of **correct** code for this rule with the `{ "ignoreTypeOfDescribeName
5050
```js
5151
describe('1', () => {
5252
it('should be a number', () => {
53-
expect(1).toBeNumber()
53+
expect(1).toBe(1)
5454
})
5555
})
5656
```
@@ -88,7 +88,7 @@ Examples of **incorrect** code for this rule with the `{ "disallowedWords": ["sk
8888
```js
8989
describe('foo', () => {
9090
it.skip('should be skipped', () => {
91-
expect(1).toBeNumber()
91+
expect(1).toBe(1)
9292
})
9393
})
9494
```
@@ -98,7 +98,7 @@ Examples of **correct** code for this rule with the `{ "disallowedWords": ["skip
9898
```js
9999
describe('foo', () => {
100100
it('should be skipped', () => {
101-
expect(1).toBeNumber()
101+
expect(1).toBe(1)
102102
})
103103
})
104104
```
@@ -112,7 +112,7 @@ Examples of **incorrect** code for this rule with the `{ "mustNotMatch": ["^\\s+
112112
```js
113113
describe('foo', () => {
114114
it(' ', () => {
115-
expect(1).toBeNumber()
115+
expect(1).toBe(1)
116116
})
117117
})
118118
```
@@ -123,7 +123,7 @@ Examples of **correct** code for this rule with the `{ "mustNotMatch": ["^\\s+$"
123123

124124
describe('foo', () => {
125125
it('should be a number', () => {
126-
expect(1).toBeNumber()
126+
expect(1).toBe(1)
127127
})
128128
})
129129
```
@@ -145,7 +145,7 @@ Examples of **incorrect** code for this rule with the `{ "mustMatch": ["^\\s*\\w
145145
```js
146146
describe('foo', () => {
147147
it(' ', () => {
148-
expect(1).toBeNumber()
148+
expect(1).toBe(1)
149149
})
150150
})
151151
```
@@ -156,7 +156,7 @@ Examples of **correct** code for this rule with the `{ "mustMatch": ["^\\s*\\w+\
156156

157157
describe('foo', () => {
158158
it('should be a number', () => {
159-
expect(1).toBeNumber()
159+
expect(1).toBe(1)
160160
})
161161
})
162162
```
@@ -168,7 +168,7 @@ Examples of **incorrect** code for this rule with the `{ "mustMatch": { "it": ["
168168
describe('foo', () => {
169169
// This check fails because the title does not match the regex
170170
it('Should be a number', () => {
171-
expect(1).toBeNumber()
171+
expect(1).toBe(1)
172172
})
173173
})
174174
```
@@ -180,7 +180,7 @@ Examples of **correct** code for this rule with the `{ "mustMatch": { "describe"
180180
describe('foo', () => {
181181
// This check succeeds because the title matches the regex
182182
it('should be a number.', () => {
183-
expect(1).toBeNumber()
183+
expect(1).toBe(1)
184184
})
185185
})
186186
```

src/rules/valid-title.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,33 @@ export default createEslintRule<Options, MESSAGE_IDS>({
210210
return
211211
}
212212

213+
const reportEmptyTitle = (node: TSESTree.CallExpression) => {
214+
context.report({
215+
messageId: 'emptyTitle',
216+
data: {
217+
functionName: vitestFnCall.type === 'describe'
218+
? DescribeAlias.describe
219+
: TestCaseName.test
220+
},
221+
node
222+
})
223+
}
224+
213225
const [argument] = node.arguments
214226

215227
if (settings.typecheck) {
216228
const services = ESLintUtils.getParserServices(context)
217229

218230
const type = services.getTypeAtLocation(argument)
219231

220-
if (isFunctionType(type) || isClassType(type) || isStringLikeType(type)) return
232+
if (isFunctionType(type) || isClassType(type)) return
233+
234+
if (isStringLikeType(type)) {
235+
if (isStringNode(argument) && !getStringValue(argument)) {
236+
reportEmptyTitle(node)
237+
}
238+
return
239+
}
221240
}
222241

223242
if (!argument || (allowArguments && argument.type === AST_NODE_TYPES.Identifier)) return
@@ -240,15 +259,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({
240259
const title = getStringValue(argument)
241260

242261
if (!title) {
243-
context.report({
244-
messageId: 'emptyTitle',
245-
data: {
246-
functionName: vitestFnCall.type === 'describe'
247-
? DescribeAlias.describe
248-
: TestCaseName.test
249-
},
250-
node
251-
})
262+
reportEmptyTitle(node)
252263
return
253264
}
254265

tests/valid-title.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,18 @@ ruleTester.run(RULE_NAME, rule, {
514514
}
515515
]
516516
},
517+
{
518+
code: 'it("", function () {})',
519+
settings: { vitest: { typecheck: true } },
520+
errors: [
521+
{
522+
messageId: 'emptyTitle',
523+
column: 1,
524+
line: 1,
525+
data: { functionName: 'test' }
526+
}
527+
]
528+
},
517529
{
518530
code: 'it.concurrent("", function () {})',
519531
errors: [

0 commit comments

Comments
 (0)