lecture 15 of 190 completed

Hooks: setup that runs for you

before, beforeEach, after and afterEach. Which one to use is a decision about test independence, not style.

What you'll learn

  • What before, beforeEach, after and afterEach do
  • Why beforeEach protects test independence
  • Why assertions do not belong inside hooks

You write five tests for the contacts page. Every one starts with the same two lines: log in, open the page. Copying them is tedious, and when the login flow changes you edit five places.

Hooks solve this. A hook is code that runs around your tests automatically.

members only

Keep reading with the full course

The Foundations track and the first 5 lectures of each framework are free. This lecture is part of the paid course, which unlocks every lecture, all three frameworks, and downloads.

See the options →