Skip to content
Latchkey

Playwright "test.describe() called in a wrong scope" in CI

Playwright collects tests by running the file once at the top level. Calling test.describe() or test() inside a hook, a callback, or another test breaks that collection contract.

What this error means

Collection fails with "Playwright Test did not expect test.describe() to be called here". The offending call sits inside a beforeAll, a loop callback, or a dynamically imported module.

node
Error: Playwright Test did not expect test.describe() to be called here.
Most common reasons include:
- You are calling test.describe() in a configuration file.
- You are calling test.describe() inside a test() callback.

Common causes

describe/test called inside a hook or test

A test.describe was placed inside beforeAll or a test() body, so Playwright sees it during execution rather than collection.

Tests defined in an async-imported module

Defining tests inside a dynamically imported file runs them after collection has closed.

How to fix it

Move describe/test to module top level

Register all suites and tests synchronously at the top of the file, not inside hooks or callbacks.

test
test.describe('group', () => {
  test('case', async ({ page }) => { /* ... */ });
});

Parameterize with a synchronous loop

If you generate tests in a loop, run the loop at top level so each test registers during collection.

test
for (const name of ['a', 'b']) {
  test(`renders ${name}`, async ({ page }) => { /* ... */ });
}

How to prevent it

  • Keep test() and test.describe() at the top level of the spec file.
  • Avoid defining tests inside hooks, callbacks, or dynamic imports.
  • Use synchronous loops for parameterized tests.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →