Skip to content

Commit

Permalink
feat(valid-title): allow ignoring tests with non-string titles (#1460)
Browse files Browse the repository at this point in the history
* feat(valid-title): allow disabling titleMustBeString on test titles

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* docs(valid-title): update wording

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
Co-authored-by: Gareth Jones <Jones258@Gmail.com>
  • Loading branch information
Nokel81 and G-Rath committed Oct 26, 2023
1 parent f2af519 commit ea89da9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
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

0 comments on commit ea89da9

Please sign in to comment.