Describe Blocks and Hooks
Group tests well and reuse setup with hooks.
What you'll learn
- Group related tests with describe.
- Use beforeEach for shared setup.
- Use afterEach for cleanup.
Playwright AutomationlessonsJump to another lesson
Describe Blocks and Hooks
Group tests well and reuse setup with hooks.
Big Picture
One small picture can make this idea easier to hold.
Well grouped
- Related tests stay together.
- Setup lives in one place.
- The file is easier to scan.
- Failures are easier to read.
Scattered
- Same setup gets copied.
- Tests are hard to follow.
- Cleanup gets missed.
- The file grows messy.
VerdictGroups and hooks keep test files tidy.
How It Moves
Short steps make the flow easier to see.
Group the goal
Put similar tests in one describe block.
Add setup
Use beforeEach for common prep.
Run tests
Each test starts from the same base state.
Clean up
Use afterEach if you need to reset things.
Step By Step
This is the same idea, stretched across time.
- 1
Write the group
Name the related tests with describe.
- 2
Add shared setup
Use a hook for repeated prep.
- 3
Run each test
Let the hook handle the base state.
- 4
Reset if needed
Clean up after the test ends.
One Small Model
Think of this like a tiny card you can keep in your pocket.
Test tree
5 fieldsQuick Check
What is beforeEach for?
Map It
One more picture helps you see where this lesson matters most.
Structure map
Best
One topic per describe block
Good
Shared setup in beforeEach
Weak
Copy the same prep everywhere
Okay
Small file with no hooks