Hooks, describe and conditional tests
Hooks are tied to the worker and the test group: beforeAll and afterAll run once per worker for a file or a test.describe, without page. beforeEach and afterEach run around every test. For a known bug use test.fail() rather than test.skip(): the test still runs and reports by itself when the bug is fixed, whereas skip silently hides the lost coverage. A quick subset is chosen with tags and --grep.
Playwright · Fixtures
The bug is known, the ticket exists, and the test goes red every day. Skip, fixme or fail?
A suite grows, and so do the questions. Where should state be prepared before a test? What to do with a test that runs into a known bug, so that it neither breaks CI nor gets lost? How to run only the quick part of the suite? The answers come from describe, hooks, execution annotations and tags. This lesson covers what each of them guarantees, where it lets you down and when a fixture is the better choice.
How hooks work and why. test.describe only groups tests: a shared title in the report, shared hooks, shared options. Hooks are tied to the worker lifecycle. beforeAll and afterAll run once per worker, so only worker fixtures are available there (browser, playwright), and page is not: it does not exist yet. beforeEach and afterEach run around every test and have the same fixtures as the test, page included. The practical rule follows: anything that needs a page lives in beforeEach.
The order of hooks around a test
test.describe('Transfers', () => {
test.beforeAll(async () => {
// once per worker: only worker fixtures here, no page
});
test.beforeEach(async ({ page }) => {
await page.goto('/transfers'); // before every test
});
test.afterEach(async ({ page }, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus) {
await testInfo.attach('url', { body: page.url() }); // also after a failure
}
});
test('shows the daily limit', async ({ page }) => {
await expect(page.getByTestId('daily-limit')).toBeVisible();
});
});
In nested describe blocks the outer block's beforeEach runs first, then the inner one. afterEach also runs after a failed test, so diagnostics are collected there by testInfo.status. Hooks can be titled: test.beforeEach('login', …), and then they appear in the report as a separate step. There is a subtlety too: after a failed test Playwright restarts the worker, so beforeAll runs again in the new process, and module-level variables it filled are empty in the new worker.
Hook or fixture. A hook is simpler and more visible within a single file. A fixture is better when the logic repeats across many files, when a guaranteed teardown next to the setup is needed, and when the preparation should run only for the tests that asked for it (a hook runs for every test of the block). Hooks easily create a hidden coupling through module variables, while a fixture shows the dependency in the test signature.
Example: before → after. A test runs into a known bug and was "switched off":
test.skip('rounding of the commission', async ({ page }) => {
await page.goto('/fees');
await expect(page.getByTestId('fee')).toHaveText('1.25');
});
This works exactly as long as someone remembers the test. The bug gets fixed, the test stays skipped: coverage was lost silently and nobody learns about it. The fix: keep the test active and tell the runner that it must fail.
test('rounding of the commission', async ({ page }) => {
test.fail(true, 'Known bug PAY-731: rounds down');
await page.goto('/fees');
await expect(page.getByTestId('fee')).toHaveText('1.25');
});
Now the runner executes the test and expects a failure. While the bug exists, the test counts as passed (an expected failure) and does not turn CI red. As soon as the bug is fixed, the test starts passing, and the runner reports that a failure was expected: the signal arrives by itself, without reminders in a ticket.
A known bug: skip or fail
test.skip
The test does not run. When the bug is fixed, nobody notices, and coverage has quietly gone. It fits environment conditions, not bugs.
test.fail with a reason
The test runs and fails as expected. When the bug disappears, the runner itself reports that a failure was expected. The ticket number in the reason leads to the context.
Controlling execution. test.skip(condition, 'reason') skips by a condition, for example for a browser or platform; a callback with fixtures also works for a whole file or block. test.fixme() marks a test that is known to be broken and does not run it: a "needs fixing" marker, suitable when running it is slow or crashes the process. test.fail() runs the test and expects a failure. test.slow() triples the timeout for this test.
test('export to PDF', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'Bug PAY-512 in WebKit'); // by a condition
await page.goto('/reports');
});
// a whole file or describe block: a callback with fixtures
test.skip(({ isMobile }) => isMobile, 'The desktop layout only');
test('large import', async ({ page }) => {
test.slow(); // timeout x3, for this test only
await page.goto('/import');
});
Tags and annotations. A tag is set in the test or block details ({ tag: '@smoke' }) or directly in the title, and it must start with @. Filter with --grep and --grep-invert, both accept regular expressions. Annotations (annotation: { type, description }) do not affect the run but show up in the HTML report, so they are handy for ticket links. Tags let you pick a quick set for CI without splitting tests into files.
test('quick search', { tag: ['@smoke', '@search'] }, async ({ page }) => {
await page.goto('/search');
});
test.describe('Payments', { tag: '@payments' }, () => {
test(
'card payment',
{ annotation: { type: 'issue', description: 'PAY-512' } },
async ({ page }) => {
await page.goto('/pay');
},
);
});
npx playwright test --grep @smoke
npx playwright test --grep-invert "@slow|@flaky"
npx playwright test --grep "(?=.*@smoke)(?=.*@search)" # both tags
Limits and alternatives. A tag is just a string nobody validates: a test with a typo in its tag (@smok) silently drops out of the filtered run, so keep the list of tags in one place. beforeAll is not suitable for data that tests modify one after another, because the order and the parallel workers make the result unpredictable. Cypress has no native tags: selective runs are done with the @cypress/grep plugin; in the Selenium stack these are JUnit/TestNG categories or tags.
Common mistakes.
test.skipfor a known bug instead oftest.failortest.fixme: the gap stays unnoticed and nobody learns about the fix.- Trying to take
pageinbeforeAll: the fixture is test-scoped and unavailable there. - Order dependence:
beforeAllprepares data that tests modify one after another. - A conditional
if (browserName === …) returninside a test instead oftest.skip: the test "passes" with no checks at all, and the report does not show it. - Root-level hooks for logic that only some tests need: it runs for all of them, including those that do not need it.
Check yourself
You write await page.goto('/') in beforeAll and get an error. Why, and where should the line go?
Answer
beforeAll runs once per worker, while page is test-scoped and created for every test, so it is not there. Navigation moves to beforeEach. If an expensive shared setup really is needed once per worker, a worker fixture is used for it.
A test with a known bug was marked test.fail(). A month later the bug was fixed. What will the team see after a run?
Answer
The test starts passing, and the runner treats that as an error because a failure was expected: the run turns red with a message about it. Then the annotation is removed and the test works as a normal one again. With test.skip the run would stay green while the test never ran.
beforeEach runs before every test and has access to page, while beforeAll runs once per worker without page. Tests that should not run are skipped with test.skip. Middle/Senior: For a known bug I take test.fail with a ticket number, because the test itself tells when the bug is fixed, whereas skip hides the loss of coverage. I move repeated setup with teardown into fixtures, and pick the quick CI set with tags and --grep.