Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(valid-title): allow disabling titleMustBeString on test titles #1460

Merged
merged 2 commits into from Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions docs/rules/valid-title.md
Expand Up @@ -51,10 +51,9 @@ xtest('foo', () => {});

**titleMustBeString**

Titles for test blocks should always be a string.

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

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

Expand Down Expand Up @@ -93,6 +92,18 @@ xdescribe(myFunction, () => {});
describe(6, function () {});
```

Examples of **correct** code when `ignoreTypeOfTestName` is `true`:

```js
const myTestName = 'is a string';

it(String(/.+/), () => {});
it(myFunction, () => {});
it(myTestName, () => {});
xit(myFunction, () => {});
it(6, function () {});
```

**duplicatePrefix**

A `describe` / `test` block should not start with `duplicatePrefix`
Expand Down
30 changes: 30 additions & 0 deletions src/rules/__tests__/valid-title.test.ts
Expand Up @@ -521,6 +521,14 @@ ruleTester.run('title-must-be-string', rule, {
code: 'describe(String(/.+/), () => {});',
options: [{ ignoreTypeOfDescribeName: true }],
},
{
code: 'it(String(/.+/), () => {});',
options: [{ ignoreTypeOfTestName: true }],
},
{
code: 'const foo = "my-title"; it(foo, () => {});',
options: [{ ignoreTypeOfTestName: true }],
},
{
code: 'describe(myFunction, () => {});',
options: [{ ignoreTypeOfDescribeName: true }],
Expand All @@ -541,6 +549,28 @@ ruleTester.run('title-must-be-string', rule, {
},
],
},
{
code: 'it(String(/.+/), () => {});',
options: [{ ignoreTypeOfTestName: false }],
errors: [
{
messageId: 'titleMustBeString',
column: 4,
line: 1,
},
],
},
{
code: 'const foo = "my-title"; it(foo, () => {});',
options: [{ ignoreTypeOfTestName: false }],
errors: [
{
messageId: 'titleMustBeString',
column: 28,
line: 1,
},
],
},
{
code: 'it.skip.each([])(1, () => {});',
errors: [
Expand Down
14 changes: 12 additions & 2 deletions src/rules/valid-title.ts
Expand Up @@ -87,6 +87,7 @@ type MatcherGroups = 'describe' | 'test' | 'it';
interface Options {
ignoreSpaces?: boolean;
ignoreTypeOfDescribeName?: boolean;
ignoreTypeOfTestName?: boolean;
disallowedWords?: string[];
mustNotMatch?:
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
Expand Down Expand Up @@ -141,6 +142,10 @@ export default createRule<[Options], MessageIds>({
type: 'boolean',
default: false,
},
ignoreTypeOfTestName: {
type: 'boolean',
default: false,
},
disallowedWords: {
type: 'array',
items: { type: 'string' },
Expand Down Expand Up @@ -170,6 +175,7 @@ export default createRule<[Options], MessageIds>({
{
ignoreSpaces: false,
ignoreTypeOfDescribeName: false,
ignoreTypeOfTestName: false,
disallowedWords: [],
},
],
Expand All @@ -179,6 +185,7 @@ export default createRule<[Options], MessageIds>({
{
ignoreSpaces,
ignoreTypeOfDescribeName,
ignoreTypeOfTestName,
disallowedWords = [],
mustNotMatch,
mustMatch,
Expand Down Expand Up @@ -216,8 +223,11 @@ export default createRule<[Options], MessageIds>({
}

if (
argument.type !== AST_NODE_TYPES.TemplateLiteral &&
!(ignoreTypeOfDescribeName && jestFnCall.type === 'describe')
!(
(jestFnCall.type === 'describe' && ignoreTypeOfDescribeName) ||
(jestFnCall.type === 'test' && ignoreTypeOfTestName)
) &&
argument.type !== AST_NODE_TYPES.TemplateLiteral
) {
context.report({
messageId: 'titleMustBeString',
Expand Down