vitest-dev/vitest · error · Error

Cannot find iframe element. This is a bug in Vitest. Please,

Error message

Cannot find iframe element. This is a bug in Vitest. Please, open a new issue with reproduction.

What it means

Thrown by `getIframeScale` at tester-utils.ts:260-262 when `window.frameElement` is null — i.e. the current document is NOT running inside an `<iframe>`. Vitest's browser iframe UI uses iframe scaling for responsive layouts, and this helper computes the scale factor from the iframe's CSS transform; calling it from the top-level window has no iframe to read.

Source

Thrown at packages/browser/src/client/tester/tester-utils.ts:261

  }
  options_ = options_ || {} as T
  const currentTime = now()
  const endTime = startTime + timeout
  const remainingTime = Math.floor(endTime - currentTime)
  // keep some buffer to process the timeout, but always hand the provider a
  // positive value so it surfaces a descriptive, source-mapped locator error
  // instead of letting the task timer win the race with a generic timeout;
  // the buffer covers the provider->server->client round-trip of the rejection,
  // which can exceed 100ms on loaded CI machines running several browsers
  options_.timeout = Math.max(remainingTime - 250, 1)
  return options_
}

export function getIframeScale(): number {
  const iframe = window.frameElement

  if (!iframe) {
    throw new Error(`Cannot find iframe element. This is a bug in Vitest. Please, open a new issue with reproduction.`)
  }

  // DOMMatrix parses the computed 2D transform matrix [a, b, c, d, e, f]
  // `a` and `d` are the x and y scale factors - since we only apply uniform scaling, `a === d`
  const scale = new DOMMatrix(getComputedStyle(iframe).transform).a

  return scale
}

function escapeRegexForSelector(re: RegExp): string {
  // Unicode mode does not allow "identity character escapes", so we do not escape and
  // hope that it does not contain quotes and/or >> signs.
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape
  // TODO: rework RE usages in internal selectors away from literal representation to json, e.g. {source,flags}.
  if (re.unicode || (re as any).unicodeSets) {
    return String(re)
  }
  // Even number of backslashes followed by the quote -> insert a backslash.

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Only call scale-dependent code from within the Vitest tester iframe (the default test runtime).
  2. Guard with `if (window.frameElement) { const scale = getIframeScale() }` before invoking.
  3. If you hit this in normal use, it's a Vitest bug — report a repro as the message suggests.

Example fix

// before
const scale = getIframeScale() // throws in top window

// after
const scale = window.frameElement ? getIframeScale() : 1
Defensive patterns

Strategy: validation

Validate before calling

const scale = window.frameElement ? getIframeScale() : 1

Type guard

function isInIframe(): boolean {
  return !!window.frameElement
}

Prevention

When it happens

Trigger: Calling `getIframeScale()` (directly or via a utility that uses it) from the top-level Vitest UI window, from a popup window, or from a context where the test runner isn't embedded in the standard tester iframe.

Common situations: Custom tooling that runs against the Vitest UI window directly. Refactoring that moved a helper out of the iframe context. Loading the tester entry outside the normal iframe host.

Related errors


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