What is `describe()` and when should you use it?

Beginner

Answer

describe() is used to group related tests together. It creates a test suite and helps organize tests logically:

describe('Calculator', () => {
  test('should add two numbers', () => {
    expect(add(2, 3)).toBe(5);
  });
  
  test('should subtract two numbers', () => {
    expect(subtract(5, 3)).toBe(2);
  });
});

Benefits of using describe():

  • Better test organization and readability
  • Scoped setup and teardown hooks
  • Grouped test output in reports
  • Easier to run specific test suites