vitest/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 the name of its function, as in test("test adds an item"). Options let you add your own naming rules.
--fix removes the spaces and the repeated word.
Configure#
Off by default. To turn it on:
[lint.rules]
"vitest/valid-title" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"vitest/valid-title" = ["error", {
allowArguments = false,
ignoreTypeOfDescribeName = false,
ignoreTypeOfTestName = false,
ignoreSpaces = false,
disallowedWords = [], # list of strings
# mustMatch = <value>,
# mustNotMatch = <list of strings>,
}]Examples#
import { describe, test } from "vitest";
describe("describe cart"Should not have duplicate prefix, () => {
test(""Should not have an empty title, () => {});
test(" adds an item"Should not have leading or trailing spaces, () => {});
test("test removes an item"Should not have duplicate prefix, () => {});
test(404Title must be a string, () => {});
});import { describe, test } from "vitest";
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. |
If settings.vitest.typecheck is true in your config, describe titles aren't checked at all.
A variable is reported by default, because the rule can't tell what it holds:
import { test } from "vitest";
test(titleTitle must be a string, () => {});{ "allowArguments": true }No problemsimport { test } from "vitest";
test(title, () => {});ignoreTypeOfDescribeName lets you name a block after the class or function it tests:
{ "ignoreTypeOfDescribeName": true }No problemsimport { describe, test } from "vitest";
describe(Cart, () => {
test("adds an item", () => {});
});{ "disallowedWords": ["should"] }1 problemimport { test } from "vitest";
test("Should add an item"Should is not allowed in test title, () => {});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]:
{ "mustNotMatch": ["\\.$", "Do not end a title with a period"] }1 problemimport { test } from "vitest";
test("adds an item."Do not end a title with a period, () => {});To use a different pattern for describe, test and it, pass an object. A function that isn't in the object isn't checked:
{ "mustMatch": { "it": "^should " } }1 problemimport { it, test } from "vitest";
it("adds an item"it should match (?u)^should, () => {});
test("adds an item", () => {});