Testing / Playwright Interview questions
What is the difference between test.beforeEach and test.beforeAll?
Both are setup hooks, but they run at different scopes within a test file.
| test.beforeEach | test.beforeAll |
| Runs before every single test in the file/describe block | Runs once before all tests in the file/describe block |
| Receives per-test fixtures like page | Runs in a worker-scoped context, not tied to one test's page |
| Good for resetting state each test | Good for expensive one-time setup shared across tests |
A common mistake is using beforeAll to log in and reusing that same page across tests - since beforeAll isn't given a fresh per-test page fixture the way beforeEach is, this pattern tends to leak state between tests and is generally discouraged in favor of saved authentication state instead.
More Related questions...