-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpublic-utils.ts
72 lines (63 loc) · 2.34 KB
/
public-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { type Locator, type LocatorSelectors, page } from '@vitest/browser/context'
import { asLocator } from 'ivya'
import { stringify, type StringifyOptions } from 'vitest/utils'
export function getElementLocatorSelectors(element: Element): LocatorSelectors {
const locator = page.elementLocator(element)
return {
getByAltText: (altText, options) => locator.getByAltText(altText, options),
getByLabelText: (labelText, options) => locator.getByLabelText(labelText, options),
getByPlaceholder: (placeholderText, options) => locator.getByPlaceholder(placeholderText, options),
getByRole: (role, options) => locator.getByRole(role, options),
getByTestId: testId => locator.getByTestId(testId),
getByText: (text, options) => locator.getByText(text, options),
getByTitle: (title, options) => locator.getByTitle(title, options),
}
}
type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'>
export function debug(
el?: Element | Locator | null | (Element | Locator)[],
maxLength?: number,
options?: PrettyDOMOptions,
): void {
if (Array.isArray(el)) {
// eslint-disable-next-line no-console
el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
}
else {
// eslint-disable-next-line no-console
console.log(prettyDOM(el, maxLength, options))
}
}
export function prettyDOM(
dom?: Element | Locator | undefined | null,
maxLength: number = Number(import.meta.env.DEBUG_PRINT_LIMIT ?? 7000),
prettyFormatOptions: PrettyDOMOptions = {},
): string {
if (maxLength === 0) {
return ''
}
if (!dom) {
dom = document.body
}
if ('element' in dom && 'all' in dom) {
dom = dom.element()
}
const type = typeof dom
if (type !== 'object' || !dom.outerHTML) {
const typeName = type === 'object' ? dom.constructor.name : type
throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`)
}
const pretty = stringify(dom, Number.POSITIVE_INFINITY, {
maxLength,
highlight: true,
...prettyFormatOptions,
})
return dom.outerHTML.length > maxLength
? `${pretty.slice(0, maxLength)}...`
: pretty
}
export function getElementError(selector: string, container: Element): Error {
const error = new Error(`Cannot find element with locator: ${asLocator('javascript', selector)}\n\n${prettyDOM(container)}`)
error.name = 'VitestBrowserElementError'
return error
}