useDescribeFunctionTitle (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useDescribeFunctionTitle - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
vitest/prefer-describe-function-title
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useDescribeFunctionTitle": "error" } } }}Description
Section titled “Description”Require passing an imported function as a test suite title instead of a string with its name.
Vitest and Jest accept a function as the title of a test suite (a describe or suite block),
and use the name of the function as the title.
When a test suite is about a single function, passing the function itself keeps the title correct
even if the function is renamed later, because the import has to be renamed too.
A string with the old name would silently stay out of date.
This rule reports a test suite, including variants such as describe.only(...) and describe.each(table)(...), when its title is:
- a string that matches the name of an imported value, such as
describe("myFunction", ...); - the
nameproperty of an imported value, such asdescribe(myFunction.name, ...).
A describe or suite function that you define yourself or import from a module other than
vitest or @jest/globals, such as node:test, is ignored, because it may only accept a string title.
Mocha’s global describe also only accepts a string title, and can’t be told apart from the one of Jest or Vitest,
so this rule shouldn’t be enabled in projects that use Mocha.
Type-only imports are ignored, because they don’t exist when the tests run.
Namespace imports (import * as utils from "./utils") are also ignored,
because they are objects without a name, not functions.
The fix is unsafe because the title can change.
The name of a function doesn’t always match the name it was imported with,
for example with import { parse as parseJson } from "./parse" or with a default import.
Also, the rule doesn’t check that the imported value is a function.
Test runners use titles to name stored snapshots and to select which tests to run,
so a changed title can leave a stored snapshot unused.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import { myFunction } from "./myFunction";
describe("myFunction", () => {});code-block.js:3:10 lint/nursery/useDescribeFunctionTitle FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This test suite title repeats the name of the imported myFunction.
1 │ import { myFunction } from "./myFunction";
2 │
> 3 │ describe("myFunction", () => {});
│ ^^^^^^^^^^^^
4 │
ℹ Passing myFunction itself as the title keeps the title correct when the function is renamed.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use myFunction as the title.
3 │ describe("myFunction",·()·=>·{});
│ - -
import { myFunction } from "./myFunction";
describe(myFunction.name, () => {});code-block.js:3:10 lint/nursery/useDescribeFunctionTitle FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This test suite title repeats the name of the imported myFunction.
1 │ import { myFunction } from "./myFunction";
2 │
> 3 │ describe(myFunction.name, () => {});
│ ^^^^^^^^^^^^^^^
4 │
ℹ Passing myFunction itself as the title keeps the title correct when the function is renamed.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use myFunction as the title.
1 1 │ import { myFunction } from "./myFunction";
2 2 │
3 │ - describe(myFunction.name,·()·=>·{});
3 │ + describe(myFunction,·()·=>·{});
4 4 │
import { myFunction } from "./myFunction";
describe(myFunction, () => {});describe("other things", () => {});test("myFunction", () => {});Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.