@vitest/browser 2.0.4 → 2.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/aria-role.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ type ARIAWidgetRole =
2
+ | "button"
3
+ | "checkbox"
4
+ | "gridcell"
5
+ | "link"
6
+ | "menuitem"
7
+ | "menuitemcheckbox"
8
+ | "menuitemradio"
9
+ | "option"
10
+ | "progressbar"
11
+ | "radio"
12
+ | "scrollbar"
13
+ | "searchbox"
14
+ | "slider"
15
+ | "spinbutton"
16
+ | "switch"
17
+ | "tab"
18
+ | "tabpanel"
19
+ | "textbox"
20
+ | "treeitem";
21
+
22
+ type ARIACompositeWidgetRole =
23
+ | "combobox"
24
+ | "grid"
25
+ | "listbox"
26
+ | "menu"
27
+ | "menubar"
28
+ | "radiogroup"
29
+ | "tablist"
30
+ | "tree"
31
+ | "treegrid";
32
+
33
+ type ARIADocumentStructureRole =
34
+ | "application"
35
+ | "article"
36
+ | "blockquote"
37
+ | "caption"
38
+ | "cell"
39
+ | "columnheader"
40
+ | "definition"
41
+ | "deletion"
42
+ | "directory"
43
+ | "document"
44
+ | "emphasis"
45
+ | "feed"
46
+ | "figure"
47
+ | "generic"
48
+ | "group"
49
+ | "heading"
50
+ | "img"
51
+ | "insertion"
52
+ | "list"
53
+ | "listitem"
54
+ | "math"
55
+ | "meter"
56
+ | "none"
57
+ | "note"
58
+ | "paragraph"
59
+ | "presentation"
60
+ | "row"
61
+ | "rowgroup"
62
+ | "rowheader"
63
+ | "separator"
64
+ | "strong"
65
+ | "subscript"
66
+ | "superscript"
67
+ | "table"
68
+ | "term"
69
+ | "time"
70
+ | "toolbar"
71
+ | "tooltip";
72
+
73
+ type ARIALandmarkRole =
74
+ | "banner"
75
+ | "complementary"
76
+ | "contentinfo"
77
+ | "form"
78
+ | "main"
79
+ | "navigation"
80
+ | "region"
81
+ | "search";
82
+
83
+ type ARIALiveRegionRole = "alert" | "log" | "marquee" | "status" | "timer";
84
+
85
+ type ARIAWindowRole = "alertdialog" | "dialog";
86
+
87
+ type ARIAUncategorizedRole = "code";
88
+
89
+ export type ARIARole =
90
+ | ARIAWidgetRole
91
+ | ARIACompositeWidgetRole
92
+ | ARIADocumentStructureRole
93
+ | ARIALandmarkRole
94
+ | ARIALiveRegionRole
95
+ | ARIAWindowRole
96
+ | ARIAUncategorizedRole;
package/context.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { ResolvedConfig } from 'vitest'
1
+ import type { SerializedConfig } from 'vitest'
2
+ import { ARIARole } from './aria-role'
2
3
 
3
4
  export type BufferEncoding =
4
5
  | 'ascii'
@@ -24,9 +25,9 @@ export interface CDPSession {
24
25
  }
25
26
 
26
27
  export interface ScreenshotOptions {
27
- element?: Element
28
+ element?: Element | Locator
28
29
  /**
29
- * Path relative to the `screenshotDirectory` in the test config.
30
+ * Path relative to the current test file.
30
31
  */
31
32
  path?: string
32
33
  /**
@@ -49,6 +50,14 @@ export interface BrowserCommands {
49
50
  }
50
51
 
51
52
  export interface UserEvent {
53
+ /**
54
+ * Creates a new user event instance. This is useful if you need to keep the
55
+ * state of keyboard to press and release buttons correctly.
56
+ *
57
+ * **Note:** Unlike `@testing-library/user-event`, the default `userEvent` instance
58
+ * from `@vitest/browser/context` is created once, not every time its methods are called!
59
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api.html#userevent-setup}
60
+ */
52
61
  setup: () => UserEvent
53
62
  /**
54
63
  * Click on an element. Uses provider's API under the hood and supports all its options.
@@ -56,21 +65,21 @@ export interface UserEvent {
56
65
  * @see {@link https://webdriver.io/docs/api/element/click/} WebdriverIO API
57
66
  * @see {@link https://testing-library.com/docs/user-event/convenience/#click} testing-library API
58
67
  */
59
- click: (element: Element, options?: UserEventClickOptions) => Promise<void>
68
+ click: (element: Element | Locator, options?: UserEventClickOptions) => Promise<void>
60
69
  /**
61
70
  * Triggers a double click event on an element. Uses provider's API under the hood.
62
71
  * @see {@link https://playwright.dev/docs/api/class-locator#locator-dblclick} Playwright API
63
72
  * @see {@link https://webdriver.io/docs/api/element/doubleClick/} WebdriverIO API
64
73
  * @see {@link https://testing-library.com/docs/user-event/convenience/#dblClick} testing-library API
65
74
  */
66
- dblClick: (element: Element, options?: UserEventDoubleClickOptions) => Promise<void>
75
+ dblClick: (element: Element | Locator, options?: UserEventDoubleClickOptions) => Promise<void>
67
76
  /**
68
77
  * Triggers a triple click event on an element. Uses provider's API under the hood.
69
78
  * @see {@link https://playwright.dev/docs/api/class-locator#locator-click} Playwright API: using `click` with `clickCount: 3`
70
79
  * @see {@link https://webdriver.io/docs/api/browser/actions/} WebdriverIO API: using actions api with `move` plus three `down + up + pause` events in a row
71
80
  * @see {@link https://testing-library.com/docs/user-event/convenience/#tripleclick} testing-library API
72
81
  */
73
- tripleClick: (element: Element, options?: UserEventTripleClickOptions) => Promise<void>
82
+ tripleClick: (element: Element | Locator, options?: UserEventTripleClickOptions) => Promise<void>
74
83
  /**
75
84
  * Choose one or more values from a select element. Uses provider's API under the hood.
76
85
  * If select doesn't have `multiple` attribute, only the first value will be selected.
@@ -92,7 +101,7 @@ export interface UserEvent {
92
101
  */
93
102
  selectOptions: (
94
103
  element: Element,
95
- values: HTMLElement | HTMLElement[] | string | string[],
104
+ values: HTMLElement | HTMLElement[] | Locator | Locator[] | string | string[],
96
105
  options?: UserEventSelectOptions,
97
106
  ) => Promise<void>
98
107
  /**
@@ -103,7 +112,7 @@ export interface UserEvent {
103
112
  * await userEvent.keyboard('foo') // translates to: f, o, o
104
113
  * await userEvent.keyboard('{{a[[') // translates to: {, a, [
105
114
  * await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
106
- * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
115
+ * @see {@link https://playwright.dev/docs/api/class-keyboard} Playwright API
107
116
  * @see {@link https://webdriver.io/docs/api/browser/keys} WebdriverIO API
108
117
  * @see {@link https://testing-library.com/docs/user-event/keyboard} testing-library API
109
118
  */
@@ -111,6 +120,7 @@ export interface UserEvent {
111
120
  /**
112
121
  * Types text into an element. Uses provider's API under the hood.
113
122
  * **Supports** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (e.g., `{Shift}`) even with `playwright` and `webdriverio` providers.
123
+ * This method can be significantly slower than `userEvent.fill`, so it should be used only when necessary.
114
124
  * @example
115
125
  * await userEvent.type(input, 'foo') // translates to: f, o, o
116
126
  * await userEvent.type(input, '{{a[[') // translates to: {, a, [
@@ -119,17 +129,17 @@ export interface UserEvent {
119
129
  * @see {@link https://webdriver.io/docs/api/browser/action#key-input-source} WebdriverIO API
120
130
  * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
121
131
  */
122
- type: (element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>
132
+ type: (element: Element | Locator, text: string, options?: UserEventTypeOptions) => Promise<void>
123
133
  /**
124
134
  * Removes all text from an element. Uses provider's API under the hood.
125
135
  * @see {@link https://playwright.dev/docs/api/class-locator#locator-clear} Playwright API
126
136
  * @see {@link https://webdriver.io/docs/api/element/clearValue} WebdriverIO API
127
137
  * @see {@link https://testing-library.com/docs/user-event/utility/#clear} testing-library API
128
138
  */
129
- clear: (element: Element) => Promise<void>
139
+ clear: (element: Element | Locator) => Promise<void>
130
140
  /**
131
141
  * Sends a `Tab` key event. Uses provider's API under the hood.
132
- * @see {@link https://playwright.dev/docs/api/class-locator#locator-press} Playwright API
142
+ * @see {@link https://playwright.dev/docs/api/class-keyboard} Playwright API
133
143
  * @see {@link https://webdriver.io/docs/api/element/keys} WebdriverIO API
134
144
  * @see {@link https://testing-library.com/docs/user-event/convenience/#tab} testing-library API
135
145
  */
@@ -140,7 +150,7 @@ export interface UserEvent {
140
150
  * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
141
151
  * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
142
152
  */
143
- hover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
153
+ hover: (element: Element | Locator, options?: UserEventHoverOptions) => Promise<void>
144
154
  /**
145
155
  * Moves cursor position to the body element. Uses provider's API under the hood.
146
156
  * By default, the cursor position is in the center (in webdriverio) or in some visible place (in playwright)
@@ -149,7 +159,7 @@ export interface UserEvent {
149
159
  * @see {@link https://webdriver.io/docs/api/element/moveTo/} WebdriverIO API
150
160
  * @see {@link https://testing-library.com/docs/user-event/convenience/#hover} testing-library API
151
161
  */
152
- unhover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
162
+ unhover: (element: Element | Locator, options?: UserEventHoverOptions) => Promise<void>
153
163
  /**
154
164
  * Fills an input element with text. This will remove any existing text in the input before typing the new text.
155
165
  * Uses provider's API under the hood.
@@ -162,13 +172,13 @@ export interface UserEvent {
162
172
  * @see {@link https://webdriver.io/docs/api/element/setValue} WebdriverIO API
163
173
  * @see {@link https://testing-library.com/docs/user-event/utility/#type} testing-library API
164
174
  */
165
- fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise<void>
175
+ fill: (element: Element | Locator, text: string, options?: UserEventFillOptions) => Promise<void>
166
176
  /**
167
177
  * Drags a source element on top of the target element. This API is not supported by "preview" provider.
168
178
  * @see {@link https://playwright.dev/docs/api/class-frame#frame-drag-and-drop} Playwright API
169
179
  * @see {@link https://webdriver.io/docs/api/element/dragAndDrop/} WebdriverIO API
170
180
  */
171
- dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
181
+ dragAndDrop: (source: Element | Locator, target: Element | Locator, options?: UserEventDragAndDropOptions) => Promise<void>
172
182
  }
173
183
 
174
184
  export interface UserEventFillOptions {}
@@ -179,6 +189,199 @@ export interface UserEventDoubleClickOptions {}
179
189
  export interface UserEventTripleClickOptions {}
180
190
  export interface UserEventDragAndDropOptions {}
181
191
 
192
+ export interface LocatorOptions {
193
+ /**
194
+ * Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a
195
+ * regular expression. Note that exact match still trims whitespace.
196
+ */
197
+ exact?: boolean
198
+ }
199
+
200
+ export interface LocatorByRoleOptions extends LocatorOptions {
201
+ /**
202
+ * Should checked elements (set by `aria-checked` or `<input type="checkbox"/>`) be included or not. By default, the filter is not applied.
203
+ *
204
+ * See [`aria-checked`](https://www.w3.org/TR/wai-aria-1.2/#aria-checked) for more information
205
+ */
206
+ checked?: boolean
207
+ /**
208
+ * Should disabled elements be included or not. By default, the filter is not applied. Note that unlike other attributes, `disable` state is inherited.
209
+ *
210
+ * See [`aria-disabled`](https://www.w3.org/TR/wai-aria-1.2/#aria-disabled) for more information
211
+ */
212
+ disabled?: boolean
213
+ /**
214
+ * Should expanded elements be included or not. By default, the filter is not applied.
215
+ *
216
+ * See [`aria-expanded`](https://www.w3.org/TR/wai-aria-1.2/#aria-expanded) for more information
217
+ */
218
+ expanded?: boolean
219
+ /**
220
+ * Should elements that are [normally excluded](https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion) from the accessibility tree be queried. By default, only non-hidden elements are matched by role selector.
221
+ *
222
+ * Note that roles `none` and `presentation` are always included.
223
+ * @default false
224
+ */
225
+ includeHidden?: boolean
226
+ /**
227
+ * A number attribute that is usually present for `heading`, `listitem`, `row`, `treeitem` roles with default values for `<h1>-<h6>` elements. By default, the filter is not applied.
228
+ *
229
+ * See [`aria-level`](https://www.w3.org/TR/wai-aria-1.2/#aria-level) for more information
230
+ */
231
+ level?: number
232
+ /**
233
+ * Option to match the [accessible name](https://w3c.github.io/accname/#dfn-accessible-name). By default, matching is
234
+ * case-insensitive and searches for a substring, use `exact` to control this behavior.
235
+ */
236
+ name?: string | RegExp
237
+ /**
238
+ * Should pressed elements be included or not. By default, the filter is not applied.
239
+ *
240
+ * See [`aria-pressed`](https://www.w3.org/TR/wai-aria-1.2/#aria-pressed) for more information
241
+ */
242
+ pressed?: boolean
243
+ /**
244
+ * Should selected elements be included or not. By default, the filter is not applied.
245
+ *
246
+ * See [`aria-selected`](https://www.w3.org/TR/wai-aria-1.2/#aria-selected) for more information
247
+ */
248
+ selected?: boolean
249
+ }
250
+
251
+ interface LocatorScreenshotOptions extends Omit<ScreenshotOptions, 'element'> {}
252
+
253
+ interface LocatorSelectors {
254
+ /**
255
+ * Creates a way to locate an element by its [ARIA role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles), [ARIA attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes) and [accessible name](https://developer.mozilla.org/en-US/docs/Glossary/Accessible_name).
256
+ * @see {@link https://vitest.dev/guide/browser/locators#getbyrole}
257
+ */
258
+ getByRole(role: ARIARole | ({} & string), options?: LocatorByRoleOptions): Locator
259
+ /**
260
+ * @see {@link https://vitest.dev/guide/browser/locators#getbylabeltext}
261
+ */
262
+ getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
263
+ /**
264
+ * Creates a locator capable of finding an element with an `alt` attribute that matches the text. Unlike testing-library's implementation, Vitest will match any element that has an `alt` attribute.
265
+ * @see {@link https://vitest.dev/guide/browser/locators#getbyalttext}
266
+ */
267
+ getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
268
+ /**
269
+ * Creates a locator capable of finding an element that has the specified placeholder text. Vitest will match any element that has a matching `placeholder` attribute, not just `input`.
270
+ * @see {@link https://vitest.dev/guide/browser/locators#getbyplaceholder}
271
+ */
272
+ getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
273
+ /**
274
+ * Creates a locator capable of finding an element that contains the specified text. The text will be matched against TextNode's [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue) or input's value if the type is `button` or `reset`.
275
+ * Matching by text always normalizes whitespace, even with exact match.
276
+ * For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
277
+ * @see {@link https://vitest.dev/guide/browser/locators#getbytext}
278
+ */
279
+ getByText(text: string | RegExp, options?: LocatorOptions): Locator
280
+ /**
281
+ * Creates a locator capable of finding an element that has the specified `title` attribute. Unlike testing-library's `getByTitle`, Vitest cannot find `title` elements within an SVG.
282
+ * @see {@link https://vitest.dev/guide/browser/locators#getbytitle}
283
+ */
284
+ getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
285
+ /**
286
+ * Creates a locator capable of finding an element that matches the specified test id attribute. You can configure the attribute name with [`browser.locators.testIdAttribute`](/config/#browser-locators-testidattribute).
287
+ * @see {@link https://vitest.dev/guide/browser/locators#getbytestid}
288
+ */
289
+ getByTestId(text: string | RegExp): Locator
290
+ }
291
+
292
+ export interface Locator extends LocatorSelectors {
293
+ /**
294
+ * Click on an element. You can use the options to set the cursor position.
295
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-click}
296
+ */
297
+ click(options?: UserEventClickOptions): Promise<void>
298
+ /**
299
+ * Triggers a double click event on an element. You can use the options to set the cursor position.
300
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-dblclick}
301
+ */
302
+ dblClick(options?: UserEventDoubleClickOptions): Promise<void>
303
+ /**
304
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-tripleclick}
305
+ */
306
+ tripleClick(options?: UserEventTripleClickOptions): Promise<void>
307
+ /**
308
+ * Clears the input element content
309
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-clear}
310
+ */
311
+ clear(): Promise<void>
312
+ /**
313
+ * Moves the cursor position to the selected element
314
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-hover}
315
+ */
316
+ hover(options?: UserEventHoverOptions): Promise<void>
317
+ /**
318
+ * This works the same as `locator.hover`, but moves the cursor to the `document.body` element instead.
319
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-unhover}
320
+ */
321
+ unhover(options?: UserEventHoverOptions): Promise<void>
322
+ /**
323
+ * Sets the value of the current `input`, `textarea` or `conteneditable` element.
324
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-fill}
325
+ */
326
+ fill(text: string, options?: UserEventFillOptions): Promise<void>
327
+ /**
328
+ * Drags the current element to the target location.
329
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-dropto}
330
+ */
331
+ dropTo(target: Locator, options?: UserEventDragAndDropOptions): Promise<void>
332
+ /**
333
+ * Choose one or more values from a `<select>` element.
334
+ * @see {@link https://vitest.dev/guide/browser/interactivity-api#userevent-selectoptions}
335
+ */
336
+ selectOptions(
337
+ values: HTMLElement | HTMLElement[] | Locator | Locator[] | string | string[],
338
+ options?: UserEventSelectOptions,
339
+ ): Promise<void>
340
+
341
+ /**
342
+ * Make a screenshot of an element matching the locator.
343
+ * @see {@link https://vitest.dev/guide/browser/locators#screenshot}
344
+ */
345
+ screenshot(options: Omit<LocatorScreenshotOptions, 'base64'> & { base64: true }): Promise<{
346
+ path: string
347
+ base64: string
348
+ }>
349
+ screenshot(options?: LocatorScreenshotOptions): Promise<string>
350
+
351
+ /**
352
+ * Returns an element matching the selector.
353
+ *
354
+ * - If multiple elements match the selector, an error is thrown.
355
+ * - If no elements match the selector, an error is thrown.
356
+ *
357
+ * @see {@link https://vitest.dev/guide/browser/locators#element}
358
+ */
359
+ element(): Element
360
+ /**
361
+ * Returns an array of elements matching the selector.
362
+ *
363
+ * If no elements match the selector, an empty array is returned.
364
+ *
365
+ * @see {@link https://vitest.dev/guide/browser/locators#elements}
366
+ */
367
+ elements(): Element[]
368
+ /**
369
+ * Returns an element matching the selector.
370
+ *
371
+ * - If multiple elements match the selector, an error is thrown.
372
+ * - If no elements match the selector, returns `null`.
373
+ *
374
+ * @see {@link https://vitest.dev/guide/browser/locators#query}
375
+ */
376
+ query(): Element | null
377
+ /**
378
+ * Wraps an array of `.elements()` matching the selector in a new `Locator`.
379
+ *
380
+ * @see {@link https://vitest.dev/guide/browser/locators#all}
381
+ */
382
+ all(): Locator[]
383
+ }
384
+
182
385
  export interface UserEventTabOptions {
183
386
  shift?: boolean
184
387
  }
@@ -225,6 +428,10 @@ export const server: {
225
428
  * @see {@link https://vitest.dev/guide/browser/commands}
226
429
  */
227
430
  commands: BrowserCommands
431
+ /**
432
+ * Serialized test config.
433
+ */
434
+ config: SerializedConfig
228
435
  }
229
436
 
230
437
  /**
@@ -241,11 +448,7 @@ export const userEvent: UserEvent
241
448
  */
242
449
  export const commands: BrowserCommands
243
450
 
244
- export interface BrowserPage {
245
- /**
246
- * Serialized test config.
247
- */
248
- config: ResolvedConfig
451
+ export interface BrowserPage extends LocatorSelectors {
249
452
  /**
250
453
  * Change the size of iframe's viewport.
251
454
  */
@@ -259,6 +462,15 @@ export interface BrowserPage {
259
462
  base64: string
260
463
  }>
261
464
  screenshot(options?: ScreenshotOptions): Promise<string>
465
+ /**
466
+ * Extend default `page` object with custom methods.
467
+ */
468
+ extend(methods: Partial<BrowserPage>): BrowserPage
469
+ /**
470
+ * Wrap an HTML element in a `Locator`. When querying for elements, the search will always return this element.
471
+ * @see {@link https://vitest.dev/guide/browser/locators}
472
+ */
473
+ elementLocator(element: Element): Locator
262
474
  }
263
475
 
264
476
  export const page: BrowserPage
@@ -4,7 +4,7 @@
4
4
  "name": "preload-helper"
5
5
  },
6
6
  "orchestrator.html": {
7
- "file": "__vitest_browser__/orchestrator-CNOAigTE.js",
7
+ "file": "__vitest_browser__/orchestrator-BObhEEvc.js",
8
8
  "name": "orchestrator",
9
9
  "src": "orchestrator.html",
10
10
  "isEntry": true,
@@ -13,7 +13,7 @@
13
13
  ]
14
14
  },
15
15
  "tester/tester.html": {
16
- "file": "__vitest_browser__/tester-BSSDPE9E.js",
16
+ "file": "__vitest_browser__/tester-CRcWWhrn.js",
17
17
  "name": "tester",
18
18
  "src": "tester/tester.html",
19
19
  "isEntry": true,