BDD & Test Naming — Testing

A test's name is the sentence you read when the test fails, so write the behaviour: the condition and the result, for example "should redirect to login when…

A test fails, and the report only says cart.test.js > testTotal. Total of what? Why? You have to open the file and read the test before you understand what broke.

A test's name is the sentence that appears in the report when that test fails, so a clear name saves time at exactly this moment. BDD (Behavior-Driven Development, development guided by behaviour) asks us to write tests as behaviour: under what condition, what happens, and what the result is. Picture the fuse box in a house. If the labels only say "1, 2, 3, 4", when the power is cut you have to try them one by one. If they say "kitchen", "bedroom", "AC", you know right away which part has no power. In this picture, each fuse is one test, its label is the test's name, and the part of the house with no power is the test that failed.

A test's name is read when the test fails

The example in this lesson is a shopping cart module with a createCart() function. One line of it is broken: it forgets to add the item's price to the total. Here is Vitest's report (npx vitest run --reporter=verbose, the mode that writes the name of every test) for the same test with two different names:

 × cart.test.js > testTotal 3ms
   → expected +0 to be 50 // Object.is equality
 × cart.test.js > Cart > should add the price to total when product is added 3ms
   → expected +0 to be 50 // Object.is equality

Given, When, Then

BDD splits one behaviour into three parts, always in this order:

Product and QA teams (the people who test the product) often write it in Gherkin. Gherkin is everyday language with keywords such as Given, When and Then. Tools like Cucumber can run it:

Feature: Shopping cart
  Scenario: Adding an item to an empty cart
    Given the cart is empty
    When the user adds "Book" priced 50
    Then the cart holds 1 item
    And its total is 50

In Vitest, those three parts are written as comments inside the test:

import { describe, it, expect } from "vitest";
import { createCart } from "./cart";

describe("Cart", () => {
  it("should add item to cart when valid product is given", () => {
    // Given: an empty cart
    const cart = createCart();
    // When: the user adds Book
    cart.add({ name: "Book", price: 50 });
    // Then: it holds 1 item
    expect(cart.items).toHaveLength(1);
  });
});

The name pattern: should ... when ...

The most common pattern is "should [result] when [condition]". The result comes first, then the condition.

it("should add item to cart when valid product is given");
it("should throw when price is negative");
it("should redirect to login when user is not authenticated");

Two other patterns are used too: "when cart is empty, checkout button is disabled", or a plain sentence like "adds item to empty cart". Pick one pattern per project, then use it consistently.

Bad names share one trait: they don't say the condition or the result.

Compared with the names in that list, should redirect to login when user is not authenticated is the most informative. It has a result (the user is sent to the login page) and a condition (the user isn't logged in).

describe and it: names that tell a story

Vitest joins the file name, the describe name and the it name with a > sign. So the test should throw when price is negative inside describe("Cart") appears as:

 FAIL  cart.test.js > Cart > should throw when price is negative

Because the names are joined, describe only needs the subject ("Cart"), and it only needs the behaviour. No need to write "Cart" again in every it.

The exercise: renameTest and isGoodName

In the exercise, you write two functions. renameTest(oldName) swaps an old name for a BDD-style name, and isGoodName(name) checks whether a name holds the word should or when.

const NEW_NAMES = {
  test1: "should run first test case",
  testAdd: "should add two numbers correctly",
  cart: "should add item to cart when valid product is given",
};

function renameTest(oldName) {
  return NEW_NAMES[oldName] || oldName;
}

function isGoodName(name) {
  const text = name.toLowerCase();
  return text.includes("should") || text.includes("when");
}

console.log(renameTest("cart"));             // should add item to cart when valid product is given
console.log(isGoodName("should add item"));  // true
console.log(isGoodName("when user clicks")); // true
console.log(isGoodName("test1"));            // false

⚠️ Names that don't help when a test goes red

TL;DR: A test's name is the sentence you read when the test fails, so write the behaviour: the condition and the result, for example "should redirect to login when user is not authenticated". The BDD structure (writing tests as behaviour): Given (the starting condition), When (the action), Then (the result), always in that order. This layout is the same as AAA (Arrange, Act, Assert). Vitest joins the file name, describe and it with >, so describe holds the subject and it holds the behaviour. In the exercise, renameTest uses an object as a table of names, and isGoodName checks includes("should") || includes("when") after toLowerCase().

⚡ A test's name is read when it fails

Meaning: When a test fails, its name is what Vitest prints in the report. The report cart.test.js > testTotal only says "testTotal failed". The report Cart > should add the price to total when product is added is already a sentence (the price must go into the total when a product is added). So you know what broke without opening the file.

⚡ Given, When, Then

Write:

it("should add item to cart when valid product is given", () => {
  // Given: an empty cart
  const cart = createCart();
  // When: the user adds Book
  cart.add({ name: "Book", price: 50 });
  // Then: it holds 1 item
  expect(cart.items).toHaveLength(1);
});

Meaning: With BDD (Behavior-Driven Development), you write a test as behaviour, in three parts. Given is the starting condition (an empty cart), When is the action (adding Book), and Then is the expected result (it holds 1 item), always in that order. Another name for it is AAA: Arrange, Act, Assert.

⚡ The pattern should ... when ...

Guess first: should call the redirect helper or should redirect to login when user is not authenticated, which helps more when the test fails?

The second. That name has a result (the user is sent to the login page) and a condition (the user isn't logged in). The first only names how the code works.

Meaning: Write the result first, then the condition: "should [result] when [condition]".

What you will learn