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

The It Block: Defining Individual Test Cases

Understand how it() or test() defines a single test case with a description and assertions.

The It Block: Defining Individual Test Cases

You'll learn how to define a single, focused test case using it() or test() functions.

What Is an It Block?

While describe() blocks group related tests together, the it() block (or its alias test()) defines one specific test case. Each it() block contains a human-readable description of what's being tested and the actual assertions that verify the behavior.

Think of it like writing a checklist: the describe() block is the category ("Kitchen Appliances"), and each it() block is one specific check ("toaster should brown bread evenly"). Each test case stands alone and tests one thing.

The Structure

An it() block takes two arguments:

  1. A description string explaining what should happen
  2. A callback function containing the test code and assertions
describe('Calculator', () => {
  it('should add two numbers correctly', () => {
    const result = 2 + 2;
    assert.strictEqual(result, 4);
  });

  it('should handle negative numbers', () => {
    const result = -5 + 3;
    assert.strictEqual(result, -2);
  });
});

Why the Name "It"?

The name it() creates readable sentences: "it should add two numbers correctly" reads like natural language. Some runners offer test() as an alias for developers who prefer "test should add two numbers correctly."

Each it() block should test one specific behavior. If a test fails, the description tells you exactly what broke without needing to read the code.

Key Takeaway: Use it() or test() to define individual test cases with clear descriptions and focused assertions—each test should verify one specific behavior in isolation.