Skip to content

Commit ea89da9

Browse files
Nokel81G-Rath
andauthored
feat(valid-title): allow ignoring tests with non-string titles (#1460)
* feat(valid-title): allow disabling titleMustBeString on test titles Signed-off-by: Sebastian Malton <[email protected]> * docs(valid-title): update wording --------- Signed-off-by: Sebastian Malton <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent f2af519 commit ea89da9

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

docs/rules/valid-title.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ xtest('foo', () => {});
5151

5252
**titleMustBeString**
5353

54-
Titles for test blocks should always be a string.
55-
56-
This is also applied to `describe` blocks by default, but can be turned off via
57-
the `ignoreTypeOfDescribeName` option:
54+
Titles for `describe`, `test`, and `it` blocks should always be a string; you
55+
can disable this with the `ignoreTypeOfDescribeName` and `ignoreTypeOfTestName`
56+
options.
5857

5958
Examples of **incorrect** code for this rule:
6059

@@ -93,6 +92,18 @@ xdescribe(myFunction, () => {});
9392
describe(6, function () {});
9493
```
9594

95+
Examples of **correct** code when `ignoreTypeOfTestName` is `true`:
96+
97+
```js
98+
const myTestName = 'is a string';
99+
100+
it(String(/.+/), () => {});
101+
it(myFunction, () => {});
102+
it(myTestName, () => {});
103+
xit(myFunction, () => {});
104+
it(6, function () {});
105+
```
106+
96107
**duplicatePrefix**
97108

98109
A `describe` / `test` block should not start with `duplicatePrefix`

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

+30
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ ruleTester.run('title-must-be-string', rule, {
521521
code: 'describe(String(/.+/), () => {});',
522522
options: [{ ignoreTypeOfDescribeName: true }],
523523
},
524+
{
525+
code: 'it(String(/.+/), () => {});',
526+
options: [{ ignoreTypeOfTestName: true }],
527+
},
528+
{
529+
code: 'const foo = "my-title"; it(foo, () => {});',
530+
options: [{ ignoreTypeOfTestName: true }],
531+
},
524532
{
525533
code: 'describe(myFunction, () => {});',
526534
options: [{ ignoreTypeOfDescribeName: true }],
@@ -541,6 +549,28 @@ ruleTester.run('title-must-be-string', rule, {
541549
},
542550
],
543551
},
552+
{
553+
code: 'it(String(/.+/), () => {});',
554+
options: [{ ignoreTypeOfTestName: false }],
555+
errors: [
556+
{
557+
messageId: 'titleMustBeString',
558+
column: 4,
559+
line: 1,
560+
},
561+
],
562+
},
563+
{
564+
code: 'const foo = "my-title"; it(foo, () => {});',
565+
options: [{ ignoreTypeOfTestName: false }],
566+
errors: [
567+
{
568+
messageId: 'titleMustBeString',
569+
column: 28,
570+
line: 1,
571+
},
572+
],
573+
},
544574
{
545575
code: 'it.skip.each([])(1, () => {});',
546576
errors: [

src/rules/valid-title.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type MatcherGroups = 'describe' | 'test' | 'it';
8787
interface Options {
8888
ignoreSpaces?: boolean;
8989
ignoreTypeOfDescribeName?: boolean;
90+
ignoreTypeOfTestName?: boolean;
9091
disallowedWords?: string[];
9192
mustNotMatch?:
9293
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
@@ -141,6 +142,10 @@ export default createRule<[Options], MessageIds>({
141142
type: 'boolean',
142143
default: false,
143144
},
145+
ignoreTypeOfTestName: {
146+
type: 'boolean',
147+
default: false,
148+
},
144149
disallowedWords: {
145150
type: 'array',
146151
items: { type: 'string' },
@@ -170,6 +175,7 @@ export default createRule<[Options], MessageIds>({
170175
{
171176
ignoreSpaces: false,
172177
ignoreTypeOfDescribeName: false,
178+
ignoreTypeOfTestName: false,
173179
disallowedWords: [],
174180
},
175181
],
@@ -179,6 +185,7 @@ export default createRule<[Options], MessageIds>({
179185
{
180186
ignoreSpaces,
181187
ignoreTypeOfDescribeName,
188+
ignoreTypeOfTestName,
182189
disallowedWords = [],
183190
mustNotMatch,
184191
mustMatch,
@@ -216,8 +223,11 @@ export default createRule<[Options], MessageIds>({
216223
}
217224

218225
if (
219-
argument.type !== AST_NODE_TYPES.TemplateLiteral &&
220-
!(ignoreTypeOfDescribeName && jestFnCall.type === 'describe')
226+
!(
227+
(jestFnCall.type === 'describe' && ignoreTypeOfDescribeName) ||
228+
(jestFnCall.type === 'test' && ignoreTypeOfTestName)
229+
) &&
230+
argument.type !== AST_NODE_TYPES.TemplateLiteral
221231
) {
222232
context.report({
223233
messageId: 'titleMustBeString',

0 commit comments

Comments
 (0)