Course contentsShow
← JavaScript
Lesson 2665 of 2,72354. Testing in JavaScriptFree lesson

Test Naming Conventions and Best Practices

Write clear, descriptive test names that explain what is being tested and the expected behavior.

Test Naming Conventions and Best Practices

You'll learn how to write test names that serve as living documentation for your code's behavior.

Why Test Names Matter

Your test names are the first thing developers (including future you) see when a test fails. A vague name like "works correctly" or "test 1" forces someone to read the entire test body to understand what broke. A clear, descriptive name instantly communicates both what is being tested and what behavior is expected.

Think of test names as newspaper headlines: they should tell the whole story at a glance, making someone want to read the details only if necessary.

The Anatomy of a Good Test Name

A well-crafted test name typically includes:

  1. The unit under test (function, method, or component)
  2. The specific scenario or input (edge case, error condition, normal case)
  3. The expected outcome (what should happen)
// Weak: vague and unhelpful
it('works', () => { /* ... */ });

// Better: describes what and how
it('returns sum of two positive numbers', () => { /* ... */ });

// Best: complete behavioral description
it('should return 0 when adding two negative numbers that cancel out', () => { /* ... */ });

Common Patterns

Use action-oriented language: "should throw error when...", "returns null if...", "calculates discount for...". Start with the behavior, not just the function name. Group related scenarios in describe blocks so test names can focus on the specific case.

Avoid testing implementation details in names—focus on behavior and outcomes users care about.

Key Takeaway: Write test names that clearly state what is tested, under what conditions, and what should happen—turning your test suite into readable, self-documenting specifications.