MCPcopy
hub / github.com/vitest-dev/vitest / prettyDOM

Function prettyDOM

packages/browser/src/client/tester/context.ts:509–541  ·  view source on GitHub ↗
(
  dom?: Element | Locator | undefined | null,
  maxLength: number = Number(defaultOptions?.maxLength ?? import.meta.env.DEBUG_PRINT_LIMIT ?? 7000),
  prettyFormatOptions: PrettyDOMOptions = {},
)

Source from the content-addressed store, hash-verified

507}
508
509function prettyDOM(
510 dom?: Element | Locator | undefined | null,
511 maxLength: number = Number(defaultOptions?.maxLength ?? import.meta.env.DEBUG_PRINT_LIMIT ?? 7000),
512 prettyFormatOptions: PrettyDOMOptions = {},
513): string {
514 if (maxLength === 0) {
515 return ''
516 }
517
518 if (!dom) {
519 dom = document.body
520 }
521
522 if ('element' in dom && 'all' in dom) {
523 dom = dom.element()
524 }
525
526 const type = typeof dom
527 if (type !== 'object' || !dom.outerHTML) {
528 const typeName = type === 'object' ? dom.constructor.name : type
529 throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`)
530 }
531
532 const pretty = stringify(dom, Number.POSITIVE_INFINITY, {
533 maxLength,
534 highlight: true,
535 ...defaultOptions,
536 ...prettyFormatOptions,
537 })
538 return dom.outerHTML.length > maxLength
539 ? `${pretty.slice(0, maxLength)}...`
540 : pretty
541}
542
543function getElementError(selector: string, container: Element): Error {
544 const error = new Error(`Cannot find element with locator: ${__INTERNAL._asLocator('javascript', selector)}\n\n${prettyDOM(container)}`)

Callers 3

debugFunction · 0.85
getElementErrorFunction · 0.85
console.test.tsFile · 0.85

Calls 2

stringifyFunction · 0.85
elementMethod · 0.80

Tested by

no test coverage detected