Vitest's describe()
function is commonly used with a string to label a test group, like this:
describe("myFunction", () => {
test("should return true", () => {
expect(myFunction()).toBe(true);
});
});
Did you know you can also pass a class or function instead of a string to describe()
? Vitest won't execute the function but will use its name as the label for the test group.
// ↓ Pass a function
describe(myFunction, () => {
test("should return true", () => {
expect(myFunction()).toBe(true);
});
});
This approach works well with React components too (since they are just functions):
describe(Greeting, () => {
test("should render correctly", () => {
render(<Greeting />);
expect(screen.getByText("Hello World")).toBeInTheDocument();
});
});