vitest-dev/vitest · error · Error

Method "getByRole" is not supported by the "${provider}" pro

Error message

Method "getByRole" is not supported by the "${provider}" provider.

What it means

page.getByRole is a throwing stub on the page object (packages/browser/src/client/tester/context.ts:439-441); it is only implemented when locators.extend({ getByRole }) registers a page-level implementation (context.ts:500-506). The default page object does not expose a role-based query at the top level — role queries are available on Locator instances obtained via page.elementLocator(element) (locators.ts:248). The message reports the active provider so you know which provider lacks the page-level shortcut.

Source

Thrown at packages/browser/src/client/tester/context.ts:440

          kind: bodyOrOptions?.kind ?? 'mark',
          stack: bodyOrOptions?.stack ?? error?.stack,
        })
      }
      if (!hasActiveTrace) {
        return Promise.resolve()
      }
      return triggerCommand(
        '__vitest_markTrace',
        [{
          name,
          stack: bodyOrOptions?.stack ?? error?.stack,
        }],
        error,
      )
    })
  },
  getByRole() {
    throw new Error(`Method "getByRole" is not supported by the "${provider}" provider.`)
  },
  getByLabelText() {
    throw new Error(`Method "getByLabelText" is not supported by the "${provider}" provider.`)
  },
  getByTestId() {
    throw new Error(`Method "getByTestId" is not supported by the "${provider}" provider.`)
  },
  getByAltText() {
    throw new Error(`Method "getByAltText" is not supported by the "${provider}" provider.`)
  },
  getByPlaceholder() {
    throw new Error(`Method "getByPlaceholder" is not supported by the "${provider}" provider.`)
  },
  getByText() {
    throw new Error(`Method "getByText" is not supported by the "${provider}" provider.`)
  },
  getByTitle() {
    throw new Error(`Method "getByTitle" is not supported by the "${provider}" provider.`)

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Use a Locator: const locator = page.elementLocator(document.body); locator.getByRole('button', { name: 'Save' }).
  2. Register a page-level shortcut via browser.locators.extend({ getByRole: (role, opts) => ... }) if you need the top-level API.
  3. Use the built-in locators from vitest/browser (e.g. const { getByRole } = server or the configured locators).

Example fix

// before
const btn = page.getByRole('button', { name: 'Save' })
// after
const btn = page.elementLocator(document.body).getByRole('button', { name: 'Save' })
Defensive patterns

Strategy: type-guard

Validate before calling

// Prefer going through a Locator rather than the page-level stub:
const root = page.elementLocator(document.body)
const btn = root.getByRole('button', { name: 'Save' })

Prevention

When it happens

Trigger: Calling page.getByRole('button') directly on the imported page object without first registering a locator extension, or without going through a Locator instance.

Common situations: Developers familiar with Playwright's page.getByRole assuming Vitest's page mirrors it; copying testing-library patterns that call getByRole at the top level.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/5458dc3e9e561f1f.json. Report an issue: GitHub.