|
1 | 1 | import type { Locator } from '@vitest/browser/context' |
2 | 2 | import type { ExpectPollOptions } from 'vitest' |
3 | 3 | import { chai, expect } from 'vitest' |
| 4 | +import { getType } from 'vitest/internal/browser' |
4 | 5 | import { matchers } from './expect' |
5 | 6 | import { processTimeoutOptions } from './utils' |
6 | 7 |
|
7 | | -function element<T extends Element | null | Locator>(elementOrLocator: T, options?: ExpectPollOptions): unknown { |
8 | | - if (elementOrLocator != null && !(elementOrLocator instanceof Element) && !('element' in elementOrLocator)) { |
9 | | - throw new Error(`Invalid element or locator: ${elementOrLocator}. Expected an instance of Element or Locator, received ${typeof elementOrLocator}`) |
| 8 | +const kLocator = Symbol.for('$$vitest:locator') |
| 9 | + |
| 10 | +function element<T extends HTMLElement | SVGElement | null | Locator>(elementOrLocator: T, options?: ExpectPollOptions): unknown { |
| 11 | + if (elementOrLocator != null && !(elementOrLocator instanceof HTMLElement) && !(elementOrLocator instanceof SVGElement) && !(kLocator in elementOrLocator)) { |
| 12 | + throw new Error(`Invalid element or locator: ${elementOrLocator}. Expected an instance of HTMLElement, SVGElement or Locator, received ${getType(elementOrLocator)}`) |
10 | 13 | } |
11 | 14 |
|
12 | | - return expect.poll<Element | null>(function element(this: object) { |
| 15 | + return expect.poll<HTMLElement | SVGElement | null>(function element(this: object) { |
13 | 16 | if (elementOrLocator instanceof Element || elementOrLocator == null) { |
14 | 17 | return elementOrLocator |
15 | 18 | } |
16 | 19 | chai.util.flag(this, '_poll.element', true) |
17 | 20 |
|
18 | 21 | const isNot = chai.util.flag(this, 'negate') as boolean |
19 | 22 | const name = chai.util.flag(this, '_name') as string |
20 | | - // element selector uses prettyDOM under the hood, which is an expensive call |
21 | | - // that should not be called on each failed locator attempt to avoid memory leak: |
22 | | - // https://github.com/vitest-dev/vitest/issues/7139 |
23 | | - const isLastPollAttempt = chai.util.flag(this, '_isLastPollAttempt') |
24 | 23 | // special case for `toBeInTheDocument` matcher |
25 | 24 | if (isNot && name === 'toBeInTheDocument') { |
26 | 25 | return elementOrLocator.query() |
27 | 26 | } |
| 27 | + if (name === 'toHaveLength') { |
| 28 | + // we know that `toHaveLength` requires multiple elements, |
| 29 | + // but types generally expect a single one |
| 30 | + return elementOrLocator.elements() as unknown as HTMLElement |
| 31 | + } |
| 32 | + |
| 33 | + // element selector uses prettyDOM under the hood, which is an expensive call |
| 34 | + // that should not be called on each failed locator attempt to avoid memory leak: |
| 35 | + // https://github.com/vitest-dev/vitest/issues/7139 |
| 36 | + const isLastPollAttempt = chai.util.flag(this, '_isLastPollAttempt') |
28 | 37 |
|
29 | 38 | if (isLastPollAttempt) { |
30 | 39 | return elementOrLocator.element() |
|
0 commit comments