# jest/valid-title

> Require test and `describe` titles to be non-empty strings with no stray spaces or repeated prefix

The title is how you find a test in the output when it fails. This rule reports a title that's empty, isn't a string, starts or ends with a space, or repeats its function's name, as in `test("test adds an item")`. You can add your own naming rules with options.

`--fix` removes the spaces and the repeated word.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"jest/valid-title" = "error" # or "warn"
```

Every option, set to its default. The ones that are commented out are unset by default.

```toml bunfig.toml
[lint.rules]
"jest/valid-title" = ["error", {
  allowArguments = false,
  ignoreTypeOfDescribeName = false,
  ignoreTypeOfTestName = false,
  ignoreSpaces = false,
  disallowedWords = [], # list of strings
  # mustMatch = <value>,
  # mustNotMatch = <list of strings>,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 5} */}
```ts
import { describe, test } from "@jest/globals";

describe("describe cart", () => {
  test("", () => {});
  test(" adds an item", () => {});
  test("test removes an item", () => {});
  test(404, () => {});
});
```

Not reported:

{/* good */}
```ts
import { describe, test } from "@jest/globals";

describe("cart", () => {
  test("adds an item", () => {});
  test(`removes ${count} items`, () => {});
  test("responds to " + path, () => {});
});
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allowArguments` | `false` | Allow a title that's a plain variable, such as `title`. |
| `ignoreTypeOfDescribeName` | `false` | Allow a `describe` title that isn't a string. |
| `ignoreTypeOfTestName` | `false` | Allow a `test` or `it` title that isn't a string. |
| `ignoreSpaces` | `false` | Allow spaces at the start and the end. |
| `disallowedWords` | `[]` | Whole words that a title can't contain. Case doesn't matter. |
| `mustMatch` | none | A pattern that every title must match. |
| `mustNotMatch` | none | A pattern that no title can match. |

A variable is reported by default, because the rule can't tell what it holds:

{/* bad */}
```ts
import { test } from "@jest/globals";

test(title, () => {});
```

{/* good {"options": [{"allowArguments": true}]} */}
```ts
import { test } from "@jest/globals";

test(title, () => {});
```

`ignoreTypeOfDescribeName` lets you name a block after the class or function it tests:

{/* good {"options": [{"ignoreTypeOfDescribeName": true}]} */}
```ts
import { describe, test } from "@jest/globals";

describe(Cart.name, () => {
  test("adds an item", () => {});
});
```

{/* bad {"options": [{"disallowedWords": ["should"]}]} */}
```ts
import { test } from "@jest/globals";

test("Should add an item", () => {});
```

`mustMatch` and `mustNotMatch` take a regular expression as a string, either `"^adds "` or `"/^adds /"`. Flags after the last `/` are ignored. To set the report's message, pass `[pattern, message]`:

{/* bad {"options": [{"mustNotMatch": ["\\.$", "Do not end a title with a period"]}]} */}
```ts
import { test } from "@jest/globals";

test("adds an item.", () => {});
```

To use a different pattern for `describe`, `test` and `it`, pass an object. A function that isn't in the object isn't checked:

{/* bad {"options": [{"mustMatch": {"it": "^should "}}], "count": 1} */}
```ts
import { it, test } from "@jest/globals";

it("adds an item", () => {});

test("adds an item", () => {});
```
