{"id":"e09c1159165d3760","repo":"vitest-dev/vitest","slug":"cannot-find-iframe-element-this-is-a-bug-in-vites","errorCode":null,"errorMessage":"Cannot find iframe element. This is a bug in Vitest. Please, open a new issue with reproduction.","messagePattern":"Cannot find iframe element\\. This is a bug in Vitest\\. Please, open a new issue with reproduction\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/client/tester/tester-utils.ts","lineNumber":261,"sourceCode":"  }\n  options_ = options_ || {} as T\n  const currentTime = now()\n  const endTime = startTime + timeout\n  const remainingTime = Math.floor(endTime - currentTime)\n  // keep some buffer to process the timeout, but always hand the provider a\n  // positive value so it surfaces a descriptive, source-mapped locator error\n  // instead of letting the task timer win the race with a generic timeout;\n  // the buffer covers the provider->server->client round-trip of the rejection,\n  // which can exceed 100ms on loaded CI machines running several browsers\n  options_.timeout = Math.max(remainingTime - 250, 1)\n  return options_\n}\n\nexport function getIframeScale(): number {\n  const iframe = window.frameElement\n\n  if (!iframe) {\n    throw new Error(`Cannot find iframe element. This is a bug in Vitest. Please, open a new issue with reproduction.`)\n  }\n\n  // DOMMatrix parses the computed 2D transform matrix [a, b, c, d, e, f]\n  // `a` and `d` are the x and y scale factors - since we only apply uniform scaling, `a === d`\n  const scale = new DOMMatrix(getComputedStyle(iframe).transform).a\n\n  return scale\n}\n\nfunction escapeRegexForSelector(re: RegExp): string {\n  // Unicode mode does not allow \"identity character escapes\", so we do not escape and\n  // hope that it does not contain quotes and/or >> signs.\n  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape\n  // TODO: rework RE usages in internal selectors away from literal representation to json, e.g. {source,flags}.\n  if (re.unicode || (re as any).unicodeSets) {\n    return String(re)\n  }\n  // Even number of backslashes followed by the quote -> insert a backslash.","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/client/tester/tester-utils.ts#L243-L279","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only call scale-dependent code from within the Vitest tester iframe (the default test runtime).","Guard with `if (window.frameElement) { const scale = getIframeScale() }` before invoking.","If you hit this in normal use, it's a Vitest bug — report a repro as the message suggests."],"exampleFix":"// before\nconst scale = getIframeScale() // throws in top window\n\n// after\nconst scale = window.frameElement ? getIframeScale() : 1","handlingStrategy":"validation","validationCode":"const scale = window.frameElement ? getIframeScale() : 1","typeGuard":"function isInIframe(): boolean {\n  return !!window.frameElement\n}","tryCatchPattern":null,"preventionTips":["Only call getIframeScale() from inside the Vitest tester iframe.","Guard with window.frameElement before invoking.","Treat a hit outside the iframe as a setup bug and file a repro."],"tags":["browser","iframe","internal-api","tester-utils","vitest-bug"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}