Skip to content

Commit 7dec202

Browse files
committed
feat: support ignoreTypeOfDescribeName in valid-title
Fixes #431
1 parent 1275471 commit 7dec202

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

docs/rules/valid-title.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ xtest('foo', () => {});
4343

4444
**titleMustBeString**
4545

46-
Titles should always be a string literal or expression.
46+
Titles for test blocks should always be a string literal or expression.
47+
48+
This is also applied to describe blocks by default, but can be turned off via
49+
the `ignoreTypeOfDescribeName` option:
4750

4851
Examples of **incorrect** code for this rule:
4952

@@ -52,18 +55,34 @@ it(123, () => {});
5255
describe(String(/.+/), () => {});
5356
describe(myFunction, () => {});
5457
xdescribe(myFunction, () => {});
55-
describe(6, function () {})
58+
describe(6, function() {});
5659
```
5760

5861
Examples of **correct** code for this rule:
5962

6063
```js
61-
it("is a string", () => {});
62-
test("is a string", () => {});
63-
xtest("is a string", () => {});
64-
describe("is a string", () => {});
65-
describe.skip("is a string", () => {});
66-
fdescribe("is a string", () => {});
64+
it('is a string', () => {});
65+
test('is a string', () => {});
66+
xtest('is a string', () => {});
67+
describe('is a string', () => {});
68+
describe.skip('is a string', () => {});
69+
fdescribe('is a string', () => {});
70+
```
71+
72+
Examples of **correct** code when `ignoreTypeOfDescribeName` is `true`:
73+
74+
```js
75+
it('is a string', () => {});
76+
test('is a string', () => {});
77+
xtest('is a string', () => {});
78+
describe('is a string', () => {});
79+
describe.skip('is a string', () => {});
80+
fdescribe('is a string', () => {});
81+
82+
describe(String(/.+/), () => {});
83+
describe(myFunction, () => {});
84+
xdescribe(myFunction, () => {});
85+
describe(6, function() {});
6786
```
6887

6988
**duplicatePrefix**

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@ ruleTester.run('title-must-be-string', rule, {
1212
'it("is a string", () => {});',
1313
'test("is a string", () => {});',
1414
'xtest("is a string", () => {});',
15+
'xtest(`${myFunc} is a string`, () => {});',
1516
'describe("is a string", () => {});',
1617
'describe.skip("is a string", () => {});',
18+
'describe.skip(`${myFunc} is a string`, () => {});',
1719
'fdescribe("is a string", () => {});',
20+
{
21+
code: 'describe(String(/.+/), () => {});',
22+
options: [{ ignoreTypeOfDescribeName: true }],
23+
},
24+
{
25+
code: 'describe(myFunction, () => {});',
26+
options: [{ ignoreTypeOfDescribeName: true }],
27+
},
28+
{
29+
code: 'xdescribe(skipFunction, () => {});',
30+
options: [{ ignoreTypeOfDescribeName: true }],
31+
},
1832
],
1933
invalid: [
2034
{
@@ -27,6 +41,17 @@ ruleTester.run('title-must-be-string', rule, {
2741
},
2842
],
2943
},
44+
{
45+
code: 'test.skip(123, () => {});',
46+
options: [{ ignoreTypeOfDescribeName: true }],
47+
errors: [
48+
{
49+
messageId: 'titleMustBeString',
50+
column: 11,
51+
line: 1,
52+
},
53+
],
54+
},
3055
{
3156
code: 'describe(String(/.+/), () => {});',
3257
errors: [
@@ -37,6 +62,17 @@ ruleTester.run('title-must-be-string', rule, {
3762
},
3863
],
3964
},
65+
{
66+
code: 'describe(myFunction, () => 1);',
67+
options: [{ ignoreTypeOfDescribeName: false }],
68+
errors: [
69+
{
70+
messageId: 'titleMustBeString',
71+
column: 10,
72+
line: 1,
73+
},
74+
],
75+
},
4076
{
4177
code: 'describe(myFunction, () => {});',
4278
errors: [

src/rules/valid-title.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,22 @@ export default createRule({
3131
accidentalSpace: 'should not have leading or trailing spaces',
3232
},
3333
type: 'suggestion',
34-
schema: [],
34+
schema: [
35+
{
36+
type: 'object',
37+
properties: {
38+
ignoreTypeOfDescribeName: {
39+
type: 'boolean',
40+
default: false,
41+
},
42+
},
43+
additionalProperties: false,
44+
},
45+
],
3546
fixable: 'code',
3647
},
37-
defaultOptions: [],
38-
create(context) {
48+
defaultOptions: [{ ignoreTypeOfDescribeName: false }],
49+
create(context, [{ ignoreTypeOfDescribeName }]) {
3950
return {
4051
CallExpression(node: TSESTree.CallExpression) {
4152
if (!(isDescribe(node) || isTestCase(node)) || !node.arguments.length) {
@@ -45,7 +56,10 @@ export default createRule({
4556
const [argument] = node.arguments;
4657

4758
if (!isStringNode(argument)) {
48-
if (argument.type !== AST_NODE_TYPES.TemplateLiteral) {
59+
if (
60+
argument.type !== AST_NODE_TYPES.TemplateLiteral &&
61+
!(ignoreTypeOfDescribeName && isDescribe(node))
62+
) {
4963
context.report({
5064
messageId: 'titleMustBeString',
5165
loc: argument.loc,

0 commit comments

Comments
 (0)