@vitest-agent/mcp 1.0.1 → 1.2.0

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.
Files changed (229) hide show
  1. package/README.md +1 -2
  2. package/bin/vitest-agent-mcp.js +1 -17
  3. package/index.d.ts +324 -315
  4. package/index.js +2 -4
  5. package/middleware/idempotency.js +1 -1
  6. package/package.json +2 -2
  7. package/server.js +2 -4
  8. package/tools/acceptance-metrics.js +1 -1
  9. package/tools/cache-health.js +1 -1
  10. package/tools/commit-changes.js +1 -1
  11. package/tools/configure.js +1 -1
  12. package/tools/coverage.js +1 -1
  13. package/tools/errors.js +1 -1
  14. package/tools/failure-signature-get.js +1 -1
  15. package/tools/file-coverage.js +1 -1
  16. package/tools/history.js +1 -1
  17. package/tools/inventory.js +1 -1
  18. package/tools/overview.js +1 -1
  19. package/tools/run-tests.js +15 -3
  20. package/tools/settings-list.js +1 -1
  21. package/tools/status.js +1 -1
  22. package/tools/tdd-artifact.js +1 -1
  23. package/tools/tdd-task.js +1 -1
  24. package/tools/test.js +1 -1
  25. package/tools/trends.js +1 -1
  26. package/tools/turn-search.js +1 -1
  27. package/public/patterns/_meta.json +0 -67
  28. package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
  29. package/public/patterns/known-issues-and-caveats.md +0 -52
  30. package/public/patterns/operating-vitest-agent-as-an-agent.md +0 -53
  31. package/public/patterns/running-tests-via-mcp.md +0 -58
  32. package/public/patterns/silencing-leaking-output-in-tests.md +0 -91
  33. package/public/patterns/testing-effect-schema-definitions.md +0 -71
  34. package/public/patterns/testing-effect-services-with-mock-layers.md +0 -63
  35. package/public/vendor/vitest-docs/ATTRIBUTION.md +0 -5
  36. package/public/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
  37. package/public/vendor/vitest-docs/api/advanced/metadata.md +0 -68
  38. package/public/vendor/vitest-docs/api/advanced/plugin.md +0 -168
  39. package/public/vendor/vitest-docs/api/advanced/reporters.md +0 -342
  40. package/public/vendor/vitest-docs/api/advanced/runner.md +0 -334
  41. package/public/vendor/vitest-docs/api/advanced/test-case.md +0 -302
  42. package/public/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
  43. package/public/vendor/vitest-docs/api/advanced/test-module.md +0 -140
  44. package/public/vendor/vitest-docs/api/advanced/test-project.md +0 -321
  45. package/public/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
  46. package/public/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
  47. package/public/vendor/vitest-docs/api/advanced/vitest.md +0 -684
  48. package/public/vendor/vitest-docs/api/assert-type.md +0 -22
  49. package/public/vendor/vitest-docs/api/assert.md +0 -1960
  50. package/public/vendor/vitest-docs/api/browser/assertions.md +0 -1277
  51. package/public/vendor/vitest-docs/api/browser/commands.md +0 -154
  52. package/public/vendor/vitest-docs/api/browser/context.md +0 -338
  53. package/public/vendor/vitest-docs/api/browser/interactivity.md +0 -681
  54. package/public/vendor/vitest-docs/api/browser/locators.md +0 -1171
  55. package/public/vendor/vitest-docs/api/browser/react.md +0 -346
  56. package/public/vendor/vitest-docs/api/browser/svelte.md +0 -292
  57. package/public/vendor/vitest-docs/api/browser/vue.md +0 -222
  58. package/public/vendor/vitest-docs/api/describe.md +0 -374
  59. package/public/vendor/vitest-docs/api/expect-typeof.md +0 -571
  60. package/public/vendor/vitest-docs/api/expect.md +0 -2304
  61. package/public/vendor/vitest-docs/api/hooks.md +0 -463
  62. package/public/vendor/vitest-docs/api/mock.md +0 -701
  63. package/public/vendor/vitest-docs/api/test.md +0 -926
  64. package/public/vendor/vitest-docs/api/vi.md +0 -1372
  65. package/public/vendor/vitest-docs/config/alias.md +0 -13
  66. package/public/vendor/vitest-docs/config/allowonly.md +0 -32
  67. package/public/vendor/vitest-docs/config/api.md +0 -27
  68. package/public/vendor/vitest-docs/config/attachmentsdir.md +0 -6
  69. package/public/vendor/vitest-docs/config/bail.md +0 -9
  70. package/public/vendor/vitest-docs/config/benchmark.md +0 -65
  71. package/public/vendor/vitest-docs/config/browser/api.md +0 -23
  72. package/public/vendor/vitest-docs/config/browser/commands.md +0 -6
  73. package/public/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
  74. package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
  75. package/public/vendor/vitest-docs/config/browser/enabled.md +0 -40
  76. package/public/vendor/vitest-docs/config/browser/expect.md +0 -250
  77. package/public/vendor/vitest-docs/config/browser/headless.md +0 -7
  78. package/public/vendor/vitest-docs/config/browser/instances.md +0 -47
  79. package/public/vendor/vitest-docs/config/browser/isolate.md +0 -11
  80. package/public/vendor/vitest-docs/config/browser/locators.md +0 -24
  81. package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
  82. package/public/vendor/vitest-docs/config/browser/playwright.md +0 -214
  83. package/public/vendor/vitest-docs/config/browser/preview.md +0 -32
  84. package/public/vendor/vitest-docs/config/browser/provider.md +0 -79
  85. package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
  86. package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
  87. package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
  88. package/public/vendor/vitest-docs/config/browser/trace.md +0 -43
  89. package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
  90. package/public/vendor/vitest-docs/config/browser/ui.md +0 -7
  91. package/public/vendor/vitest-docs/config/browser/viewport.md +0 -6
  92. package/public/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
  93. package/public/vendor/vitest-docs/config/cache.md +0 -26
  94. package/public/vendor/vitest-docs/config/chaiconfig.md +0 -29
  95. package/public/vendor/vitest-docs/config/clearmocks.md +0 -22
  96. package/public/vendor/vitest-docs/config/coverage.md +0 -455
  97. package/public/vendor/vitest-docs/config/css.md +0 -47
  98. package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
  99. package/public/vendor/vitest-docs/config/deps.md +0 -127
  100. package/public/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
  101. package/public/vendor/vitest-docs/config/diff.md +0 -96
  102. package/public/vendor/vitest-docs/config/dir.md +0 -7
  103. package/public/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
  104. package/public/vendor/vitest-docs/config/env.md +0 -5
  105. package/public/vendor/vitest-docs/config/environment.md +0 -96
  106. package/public/vendor/vitest-docs/config/environmentoptions.md +0 -30
  107. package/public/vendor/vitest-docs/config/exclude.md +0 -49
  108. package/public/vendor/vitest-docs/config/execargv.md +0 -10
  109. package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
  110. package/public/vendor/vitest-docs/config/expect.md +0 -38
  111. package/public/vendor/vitest-docs/config/experimental.md +0 -510
  112. package/public/vendor/vitest-docs/config/faketimers.md +0 -51
  113. package/public/vendor/vitest-docs/config/fileparallelism.md +0 -11
  114. package/public/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
  115. package/public/vendor/vitest-docs/config/globals.md +0 -42
  116. package/public/vendor/vitest-docs/config/globalsetup.md +0 -72
  117. package/public/vendor/vitest-docs/config/hideskippedtests.md +0 -7
  118. package/public/vendor/vitest-docs/config/hooktimeout.md +0 -7
  119. package/public/vendor/vitest-docs/config/include-source.md +0 -115
  120. package/public/vendor/vitest-docs/config/include.md +0 -71
  121. package/public/vendor/vitest-docs/config/includetasklocation.md +0 -17
  122. package/public/vendor/vitest-docs/config/index.md +0 -85
  123. package/public/vendor/vitest-docs/config/isolate.md +0 -13
  124. package/public/vendor/vitest-docs/config/logheapusage.md +0 -7
  125. package/public/vendor/vitest-docs/config/maxconcurrency.md +0 -9
  126. package/public/vendor/vitest-docs/config/maxworkers.md +0 -49
  127. package/public/vendor/vitest-docs/config/mockreset.md +0 -22
  128. package/public/vendor/vitest-docs/config/mode.md +0 -7
  129. package/public/vendor/vitest-docs/config/name.md +0 -111
  130. package/public/vendor/vitest-docs/config/onconsolelog.md +0 -25
  131. package/public/vendor/vitest-docs/config/onstacktrace.md +0 -32
  132. package/public/vendor/vitest-docs/config/onunhandlederror.md +0 -35
  133. package/public/vendor/vitest-docs/config/open.md +0 -7
  134. package/public/vendor/vitest-docs/config/outputfile.md +0 -7
  135. package/public/vendor/vitest-docs/config/passwithnotests.md +0 -7
  136. package/public/vendor/vitest-docs/config/pool.md +0 -45
  137. package/public/vendor/vitest-docs/config/printconsoletrace.md +0 -6
  138. package/public/vendor/vitest-docs/config/projects.md +0 -6
  139. package/public/vendor/vitest-docs/config/provide.md +0 -45
  140. package/public/vendor/vitest-docs/config/reporters.md +0 -69
  141. package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
  142. package/public/vendor/vitest-docs/config/restoremocks.md +0 -22
  143. package/public/vendor/vitest-docs/config/retry.md +0 -140
  144. package/public/vendor/vitest-docs/config/root.md +0 -6
  145. package/public/vendor/vitest-docs/config/runner.md +0 -6
  146. package/public/vendor/vitest-docs/config/sequence.md +0 -158
  147. package/public/vendor/vitest-docs/config/server.md +0 -68
  148. package/public/vendor/vitest-docs/config/setupfiles.md +0 -40
  149. package/public/vendor/vitest-docs/config/silent.md +0 -9
  150. package/public/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
  151. package/public/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
  152. package/public/vendor/vitest-docs/config/snapshotformat.md +0 -28
  153. package/public/vendor/vitest-docs/config/snapshotserializers.md +0 -6
  154. package/public/vendor/vitest-docs/config/stricttags.md +0 -30
  155. package/public/vendor/vitest-docs/config/tags.md +0 -141
  156. package/public/vendor/vitest-docs/config/teardowntimeout.md +0 -7
  157. package/public/vendor/vitest-docs/config/testnamepattern.md +0 -21
  158. package/public/vendor/vitest-docs/config/testtimeout.md +0 -7
  159. package/public/vendor/vitest-docs/config/typecheck.md +0 -77
  160. package/public/vendor/vitest-docs/config/ui.md +0 -15
  161. package/public/vendor/vitest-docs/config/unstubenvs.md +0 -20
  162. package/public/vendor/vitest-docs/config/unstubglobals.md +0 -20
  163. package/public/vendor/vitest-docs/config/update.md +0 -16
  164. package/public/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
  165. package/public/vendor/vitest-docs/config/watch.md +0 -11
  166. package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
  167. package/public/vendor/vitest-docs/guide/advanced/index.md +0 -147
  168. package/public/vendor/vitest-docs/guide/advanced/pool.md +0 -148
  169. package/public/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
  170. package/public/vendor/vitest-docs/guide/advanced/tests.md +0 -125
  171. package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
  172. package/public/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
  173. package/public/vendor/vitest-docs/guide/browser/index.md +0 -630
  174. package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
  175. package/public/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
  176. package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
  177. package/public/vendor/vitest-docs/guide/cli-generated.md +0 -972
  178. package/public/vendor/vitest-docs/guide/cli.md +0 -234
  179. package/public/vendor/vitest-docs/guide/common-errors.md +0 -163
  180. package/public/vendor/vitest-docs/guide/coverage.md +0 -515
  181. package/public/vendor/vitest-docs/guide/debugging.md +0 -127
  182. package/public/vendor/vitest-docs/guide/environment.md +0 -101
  183. package/public/vendor/vitest-docs/guide/extending-matchers.md +0 -160
  184. package/public/vendor/vitest-docs/guide/features.md +0 -310
  185. package/public/vendor/vitest-docs/guide/filtering.md +0 -175
  186. package/public/vendor/vitest-docs/guide/ide.md +0 -43
  187. package/public/vendor/vitest-docs/guide/improving-performance.md +0 -245
  188. package/public/vendor/vitest-docs/guide/in-source.md +0 -159
  189. package/public/vendor/vitest-docs/guide/index.md +0 -128
  190. package/public/vendor/vitest-docs/guide/learn/async.md +0 -147
  191. package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
  192. package/public/vendor/vitest-docs/guide/learn/matchers.md +0 -277
  193. package/public/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
  194. package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
  195. package/public/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
  196. package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
  197. package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
  198. package/public/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
  199. package/public/vendor/vitest-docs/guide/lifecycle.md +0 -379
  200. package/public/vendor/vitest-docs/guide/migration.md +0 -863
  201. package/public/vendor/vitest-docs/guide/mocking/classes.md +0 -158
  202. package/public/vendor/vitest-docs/guide/mocking/dates.md +0 -52
  203. package/public/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
  204. package/public/vendor/vitest-docs/guide/mocking/functions.md +0 -61
  205. package/public/vendor/vitest-docs/guide/mocking/globals.md +0 -20
  206. package/public/vendor/vitest-docs/guide/mocking/modules.md +0 -414
  207. package/public/vendor/vitest-docs/guide/mocking/requests.md +0 -114
  208. package/public/vendor/vitest-docs/guide/mocking/timers.md +0 -48
  209. package/public/vendor/vitest-docs/guide/mocking.md +0 -239
  210. package/public/vendor/vitest-docs/guide/open-telemetry.md +0 -156
  211. package/public/vendor/vitest-docs/guide/parallelism.md +0 -82
  212. package/public/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
  213. package/public/vendor/vitest-docs/guide/projects.md +0 -291
  214. package/public/vendor/vitest-docs/guide/recipes.md +0 -59
  215. package/public/vendor/vitest-docs/guide/reporters.md +0 -723
  216. package/public/vendor/vitest-docs/guide/snapshot.md +0 -620
  217. package/public/vendor/vitest-docs/guide/test-annotations.md +0 -103
  218. package/public/vendor/vitest-docs/guide/test-context.md +0 -902
  219. package/public/vendor/vitest-docs/guide/test-tags.md +0 -314
  220. package/public/vendor/vitest-docs/guide/testing-types.md +0 -149
  221. package/public/vendor/vitest-docs/guide/ui.md +0 -160
  222. package/public/vendor/vitest-docs/guide/using-plugins.md +0 -5
  223. package/public/vendor/vitest-docs/manifest.json +0 -1691
  224. package/resources/index.js +0 -166
  225. package/resources/indexes.js +0 -77
  226. package/resources/manifest-schema.js +0 -46
  227. package/resources/paths.js +0 -20
  228. package/resources/patterns.js +0 -22
  229. package/resources/upstream-docs.js +0 -22
@@ -1,1277 +0,0 @@
1
- # Assertion API
2
-
3
- Vitest provides a wide range of DOM assertions out of the box forked from [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) library with the added support for locators and built-in retry-ability.
4
-
5
- ::: tip TypeScript Support
6
- If you are using [TypeScript](/guide/browser/#typescript) or want to have correct type hints in `expect`, make sure you have `vitest/browser` referenced somewhere. If you never imported from there, you can add a `reference` comment in any file that's covered by your `tsconfig.json`:
7
-
8
- ```ts
9
- /// <reference types="vitest/browser" />
10
- ```
11
- :::
12
-
13
- Tests in the browser might fail inconsistently due to their asynchronous nature. Because of this, it is important to have a way to guarantee that assertions succeed even if the condition is delayed (by a timeout, network request, or animation, for example). For this purpose, Vitest provides retriable assertions out of the box via the [`expect.poll`](/api/expect#poll) and `expect.element` APIs:
14
-
15
- ```ts
16
- import { expect, test } from 'vitest'
17
- import { page } from 'vitest/browser'
18
-
19
- test('error banner is rendered', async () => {
20
- triggerError()
21
-
22
- // This creates a locator that will try to find the element
23
- // when any of its methods are called.
24
- // This call by itself doesn't check the existence of the element.
25
- const banner = page.getByRole('alert', {
26
- name: /error/i,
27
- })
28
-
29
- // Vitest provides `expect.element` with built-in retry-ability
30
- // It will repeatedly check that the element exists in the DOM and that
31
- // the content of `element.textContent` is equal to "Error!"
32
- // until all the conditions are met
33
- await expect.element(banner).toHaveTextContent('Error!')
34
- })
35
- ```
36
-
37
- We recommend to always use `expect.element` when working with `page.getBy*` locators to reduce test flakiness. Note that `expect.element` accepts a second option:
38
-
39
- ```ts
40
- interface ExpectPollOptions {
41
- // The interval to retry the assertion for in milliseconds
42
- // Defaults to "expect.poll.interval" config option
43
- interval?: number
44
- // Time to retry the assertion for in milliseconds
45
- // Defaults to "expect.poll.timeout" config option
46
- timeout?: number
47
- // The message printed when the assertion fails
48
- message?: string
49
- }
50
- ```
51
-
52
- ::: tip
53
- `expect.element` is a shorthand for `expect.poll(() => element)` and works in exactly the same way.
54
-
55
- `toHaveTextContent` and all other assertions are still available on a regular `expect` without a built-in retry-ability mechanism:
56
-
57
- ```ts
58
- // will fail immediately if .textContent is not `'Error!'`
59
- expect(banner).toHaveTextContent('Error!')
60
- ```
61
- :::
62
-
63
- ## toBeDisabled
64
-
65
- ```ts
66
- function toBeDisabled(): Promise<void>
67
- ```
68
-
69
- Allows you to check whether an element is disabled from the user's perspective.
70
-
71
- Matches if the element is a form control and the `disabled` attribute is specified on this element or the
72
- element is a descendant of a form element with a `disabled` attribute.
73
-
74
- Note that only native control elements such as HTML `button`, `input`, `select`, `textarea`, `option`, `optgroup`
75
- can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored, unless it's a custom element.
76
-
77
- ```html
78
- <button
79
- data-testid="button"
80
- type="submit"
81
- disabled
82
- >
83
- submit
84
- </button>
85
- ```
86
-
87
- ```ts
88
- await expect.element(getByTestId('button')).toBeDisabled() // ✅
89
- await expect.element(getByTestId('button')).not.toBeDisabled() // ❌
90
- ```
91
-
92
- ## toBeEnabled
93
-
94
- ```ts
95
- function toBeEnabled(): Promise<void>
96
- ```
97
-
98
- Allows you to check whether an element is not disabled from the user's perspective.
99
-
100
- Works like [`not.toBeDisabled()`](#tobedisabled). Use this matcher to avoid double negation in your tests.
101
-
102
- ```html
103
- <button
104
- data-testid="button"
105
- type="submit"
106
- disabled
107
- >
108
- submit
109
- </button>
110
- ```
111
-
112
- ```ts
113
- await expect.element(getByTestId('button')).toBeEnabled() // ✅
114
- await expect.element(getByTestId('button')).not.toBeEnabled() // ❌
115
- ```
116
-
117
- ## toBeEmptyDOMElement
118
-
119
- ```ts
120
- function toBeEmptyDOMElement(): Promise<void>
121
- ```
122
-
123
- This allows you to assert whether an element has no visible content for the user. It ignores comments but will fail if the element contains white-space.
124
-
125
- ```html
126
- <span data-testid="not-empty"><span data-testid="empty"></span></span>
127
- <span data-testid="with-whitespace"> </span>
128
- <span data-testid="with-comment"><!-- comment --></span>
129
- ```
130
-
131
- ```ts
132
- await expect.element(getByTestId('empty')).toBeEmptyDOMElement()
133
- await expect.element(getByTestId('not-empty')).not.toBeEmptyDOMElement()
134
- await expect.element(
135
- getByTestId('with-whitespace')
136
- ).not.toBeEmptyDOMElement()
137
- ```
138
-
139
- ## toBeInTheDocument
140
-
141
- ```ts
142
- function toBeInTheDocument(): Promise<void>
143
- ```
144
-
145
- Assert whether an element is present in the document or not.
146
-
147
- ```html
148
- <svg data-testid="svg-element"></svg>
149
- ```
150
-
151
- ```ts
152
- await expect.element(getByTestId('svg-element')).toBeInTheDocument()
153
- await expect.element(getByTestId('does-not-exist')).not.toBeInTheDocument()
154
- ```
155
-
156
- ::: warning
157
- This matcher does not find detached elements. The element must be added to the document to be found by `toBeInTheDocument`. If you desire to search in a detached element, please use: [`toContainElement`](#tocontainelement).
158
- :::
159
-
160
- ## toBeInvalid
161
-
162
- ```ts
163
- function toBeInvalid(): Promise<void>
164
- ```
165
-
166
- This allows you to check if an element, is currently invalid.
167
-
168
- An element is invalid if it has an [`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-invalid) with no value or a value of `"true"`, or if the result of [`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation) is `false`.
169
-
170
- ```html
171
- <input data-testid="no-aria-invalid" />
172
- <input data-testid="aria-invalid" aria-invalid />
173
- <input data-testid="aria-invalid-value" aria-invalid="true" />
174
- <input data-testid="aria-invalid-false" aria-invalid="false" />
175
-
176
- <form data-testid="valid-form">
177
- <input />
178
- </form>
179
-
180
- <form data-testid="invalid-form">
181
- <input required />
182
- </form>
183
- ```
184
-
185
- ```ts
186
- await expect.element(getByTestId('no-aria-invalid')).not.toBeInvalid()
187
- await expect.element(getByTestId('aria-invalid')).toBeInvalid()
188
- await expect.element(getByTestId('aria-invalid-value')).toBeInvalid()
189
- await expect.element(getByTestId('aria-invalid-false')).not.toBeInvalid()
190
-
191
- await expect.element(getByTestId('valid-form')).not.toBeInvalid()
192
- await expect.element(getByTestId('invalid-form')).toBeInvalid()
193
- ```
194
-
195
- ## toBeRequired
196
-
197
- ```ts
198
- function toBeRequired(): Promise<void>
199
- ```
200
-
201
- This allows you to check if a form element is currently required.
202
-
203
- An element is required if it is having a `required` or `aria-required="true"` attribute.
204
-
205
- ```html
206
- <input data-testid="required-input" required />
207
- <input data-testid="aria-required-input" aria-required="true" />
208
- <input data-testid="conflicted-input" required aria-required="false" />
209
- <input data-testid="aria-not-required-input" aria-required="false" />
210
- <input data-testid="optional-input" />
211
- <input data-testid="unsupported-type" type="image" required />
212
- <select data-testid="select" required></select>
213
- <textarea data-testid="textarea" required></textarea>
214
- <div data-testid="supported-role" role="tree" required></div>
215
- <div data-testid="supported-role-aria" role="tree" aria-required="true"></div>
216
- ```
217
-
218
- ```ts
219
- await expect.element(getByTestId('required-input')).toBeRequired()
220
- await expect.element(getByTestId('aria-required-input')).toBeRequired()
221
- await expect.element(getByTestId('conflicted-input')).toBeRequired()
222
- await expect.element(getByTestId('aria-not-required-input')).not.toBeRequired()
223
- await expect.element(getByTestId('optional-input')).not.toBeRequired()
224
- await expect.element(getByTestId('unsupported-type')).not.toBeRequired()
225
- await expect.element(getByTestId('select')).toBeRequired()
226
- await expect.element(getByTestId('textarea')).toBeRequired()
227
- await expect.element(getByTestId('supported-role')).not.toBeRequired()
228
- await expect.element(getByTestId('supported-role-aria')).toBeRequired()
229
- ```
230
-
231
- ## toBeValid
232
-
233
- ```ts
234
- function toBeValid(): Promise<void>
235
- ```
236
-
237
- This allows you to check if the value of an element, is currently valid.
238
-
239
- An element is valid if it has no [`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-invalid) or an attribute value of "false". The result of [`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation) must also be `true` if it's a form element.
240
-
241
- ```html
242
- <input data-testid="no-aria-invalid" />
243
- <input data-testid="aria-invalid" aria-invalid />
244
- <input data-testid="aria-invalid-value" aria-invalid="true" />
245
- <input data-testid="aria-invalid-false" aria-invalid="false" />
246
-
247
- <form data-testid="valid-form">
248
- <input />
249
- </form>
250
-
251
- <form data-testid="invalid-form">
252
- <input required />
253
- </form>
254
- ```
255
-
256
- ```ts
257
- await expect.element(getByTestId('no-aria-invalid')).toBeValid()
258
- await expect.element(getByTestId('aria-invalid')).not.toBeValid()
259
- await expect.element(getByTestId('aria-invalid-value')).not.toBeValid()
260
- await expect.element(getByTestId('aria-invalid-false')).toBeValid()
261
-
262
- await expect.element(getByTestId('valid-form')).toBeValid()
263
- await expect.element(getByTestId('invalid-form')).not.toBeValid()
264
- ```
265
-
266
- ## toBeVisible
267
-
268
- ```ts
269
- function toBeVisible(): Promise<void>
270
- ```
271
-
272
- This allows you to check if an element is currently visible to the user.
273
-
274
- Element is considered visible when it has non-empty bounding box and does not have `visibility:hidden` computed style.
275
-
276
- Note that according to this definition:
277
-
278
- - Elements of zero size **are not** considered visible.
279
- - Elements with `display:none` **are not** considered visible.
280
- - Elements with `opacity:0` **are** considered visible.
281
-
282
- To check that at least one element from the list is visible, use `locator.first()`.
283
-
284
- ```ts
285
- // A specific element is visible.
286
- await expect.element(page.getByText('Welcome')).toBeVisible()
287
-
288
- // At least one item in the list is visible.
289
- await expect.element(page.getByTestId('todo-item').first()).toBeVisible()
290
-
291
- // At least one of the two elements is visible, possibly both.
292
- await expect.element(
293
- page.getByRole('button', { name: 'Sign in' })
294
- .or(page.getByRole('button', { name: 'Sign up' }))
295
- .first()
296
- ).toBeVisible()
297
- ```
298
-
299
- ## toBeInViewport <Version>4.0.0</Version> {#tobeinviewport}
300
-
301
- ```ts
302
- function toBeInViewport(options: { ratio?: number }): Promise<void>
303
- ```
304
-
305
- This allows you to check if an element is currently in viewport with [IntersectionObserver API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
306
-
307
- You can pass `ratio` argument as option, which means the minimal ratio of the element should be in viewport. `ratio` should be in 0~1.
308
-
309
- ```ts
310
- // A specific element is in viewport.
311
- await expect.element(page.getByText('Welcome')).toBeInViewport()
312
-
313
- // 50% of a specific element should be in viewport
314
- await expect.element(page.getByText('To')).toBeInViewport({ ratio: 0.5 })
315
-
316
- // Full of a specific element should be in viewport
317
- await expect.element(page.getByText('Vitest')).toBeInViewport({ ratio: 1 })
318
- ```
319
-
320
- ## toContainElement
321
-
322
- ```ts
323
- function toContainElement(element: HTMLElement | SVGElement | Locator | null): Promise<void>
324
- ```
325
-
326
- This allows you to assert whether an element contains another element as a descendant or not.
327
-
328
- ```html
329
- <span data-testid="ancestor"><span data-testid="descendant"></span></span>
330
- ```
331
-
332
- ```ts
333
- const ancestor = getByTestId('ancestor')
334
- const descendant = getByTestId('descendant')
335
- const nonExistingElement = getByTestId('does-not-exist')
336
-
337
- await expect.element(ancestor).toContainElement(descendant)
338
- await expect.element(descendant).not.toContainElement(ancestor)
339
- await expect.element(ancestor).not.toContainElement(nonExistingElement)
340
- ```
341
-
342
- ## toContainHTML
343
-
344
- ```ts
345
- function toContainHTML(htmlText: string): Promise<void>
346
- ```
347
-
348
- Assert whether a string representing a HTML element is contained in another element. The string should contain valid html, and not any incomplete html.
349
-
350
- ```html
351
- <span data-testid="parent"><span data-testid="child"></span></span>
352
- ```
353
-
354
- ```ts
355
- // These are valid usages
356
- await expect.element(getByTestId('parent')).toContainHTML('<span data-testid="child"></span>')
357
- await expect.element(getByTestId('parent')).toContainHTML('<span data-testid="child" />')
358
- await expect.element(getByTestId('parent')).not.toContainHTML('<br />')
359
-
360
- // These won't work
361
- await expect.element(getByTestId('parent')).toContainHTML('data-testid="child"')
362
- await expect.element(getByTestId('parent')).toContainHTML('data-testid')
363
- await expect.element(getByTestId('parent')).toContainHTML('</span>')
364
- ```
365
-
366
- ::: warning
367
- Chances are you probably do not need to use this matcher. We encourage testing from the perspective of how the user perceives the app in a browser. That's why testing against a specific DOM structure is not advised.
368
-
369
- It could be useful in situations where the code being tested renders html that was obtained from an external source, and you want to validate that html code was used as intended.
370
-
371
- It should not be used to check DOM structure that you control. Please, use [`toContainElement`](#tocontainelement) instead.
372
- :::
373
-
374
- ## toHaveAccessibleDescription
375
-
376
- ```ts
377
- function toHaveAccessibleDescription(description?: string | RegExp): Promise<void>
378
- ```
379
-
380
- This allows you to assert that an element has the expected
381
- [accessible description](https://w3c.github.io/accname/).
382
-
383
- You can pass the exact string of the expected accessible description, or you can
384
- make a partial match passing a regular expression, or by using
385
- [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching).
386
-
387
- ```html
388
- <a
389
- data-testid="link"
390
- href="/"
391
- aria-label="Home page"
392
- title="A link to start over"
393
- >Start</a
394
- >
395
- <a data-testid="extra-link" href="/about" aria-label="About page">About</a>
396
- <img src="avatar.jpg" data-testid="avatar" alt="User profile pic" />
397
- <img
398
- src="logo.jpg"
399
- data-testid="logo"
400
- alt="Company logo"
401
- aria-describedby="t1"
402
- />
403
- <span id="t1" role="presentation">The logo of Our Company</span>
404
- <img
405
- src="logo.jpg"
406
- data-testid="logo2"
407
- alt="Company logo"
408
- aria-description="The logo of Our Company"
409
- />
410
- ```
411
-
412
- ```ts
413
- await expect.element(getByTestId('link')).toHaveAccessibleDescription()
414
- await expect.element(getByTestId('link')).toHaveAccessibleDescription('A link to start over')
415
- await expect.element(getByTestId('link')).not.toHaveAccessibleDescription('Home page')
416
- await expect.element(getByTestId('extra-link')).not.toHaveAccessibleDescription()
417
- await expect.element(getByTestId('avatar')).not.toHaveAccessibleDescription()
418
- await expect.element(getByTestId('logo')).not.toHaveAccessibleDescription('Company logo')
419
- await expect.element(getByTestId('logo')).toHaveAccessibleDescription(
420
- 'The logo of Our Company',
421
- )
422
- await expect.element(getByTestId('logo2')).toHaveAccessibleDescription(
423
- 'The logo of Our Company',
424
- )
425
- ```
426
-
427
- ## toHaveAccessibleErrorMessage
428
-
429
- ```ts
430
- function toHaveAccessibleErrorMessage(message?: string | RegExp): Promise<void>
431
- ```
432
-
433
- This allows you to assert that an element has the expected
434
- [accessible error message](https://w3c.github.io/aria/#aria-errormessage).
435
-
436
- You can pass the exact string of the expected accessible error message.
437
- Alternatively, you can perform a partial match by passing a regular expression
438
- or by using
439
- [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching).
440
-
441
- ```html
442
- <input
443
- aria-label="Has Error"
444
- aria-invalid="true"
445
- aria-errormessage="error-message"
446
- />
447
- <div id="error-message" role="alert">This field is invalid</div>
448
-
449
- <input aria-label="No Error Attributes" />
450
- <input
451
- aria-label="Not Invalid"
452
- aria-invalid="false"
453
- aria-errormessage="error-message"
454
- />
455
- ```
456
-
457
- ```ts
458
- // Inputs with Valid Error Messages
459
- await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage()
460
- await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage(
461
- 'This field is invalid',
462
- )
463
- await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage(
464
- /invalid/i,
465
- )
466
- await expect.element(
467
- getByRole('textbox', { name: 'Has Error' }),
468
- ).not.toHaveAccessibleErrorMessage('This field is absolutely correct!')
469
-
470
- // Inputs without Valid Error Messages
471
- await expect.element(
472
- getByRole('textbox', { name: 'No Error Attributes' }),
473
- ).not.toHaveAccessibleErrorMessage()
474
-
475
- await expect.element(
476
- getByRole('textbox', { name: 'Not Invalid' }),
477
- ).not.toHaveAccessibleErrorMessage()
478
- ```
479
-
480
- ## toHaveAccessibleName
481
-
482
- ```ts
483
- function toHaveAccessibleName(name?: string | RegExp): Promise<void>
484
- ```
485
-
486
- This allows you to assert that an element has the expected
487
- [accessible name](https://w3c.github.io/accname/). It is useful, for instance,
488
- to assert that form elements and buttons are properly labelled.
489
-
490
- You can pass the exact string of the expected accessible name, or you can make a
491
- partial match passing a regular expression, or by using
492
- [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching).
493
-
494
- ```html
495
- <img data-testid="img-alt" src="" alt="Test alt" />
496
- <img data-testid="img-empty-alt" src="" alt="" />
497
- <svg data-testid="svg-title"><title>Test title</title></svg>
498
- <button data-testid="button-img-alt"><img src="" alt="Test" /></button>
499
- <p><img data-testid="img-paragraph" src="" alt="" /> Test content</p>
500
- <button data-testid="svg-button"><svg><title>Test</title></svg></p>
501
- <div><svg data-testid="svg-without-title"></svg></div>
502
- <input data-testid="input-title" title="test" />
503
- ```
504
-
505
- ```javascript
506
- await expect.element(getByTestId('img-alt')).toHaveAccessibleName('Test alt')
507
- await expect.element(getByTestId('img-empty-alt')).not.toHaveAccessibleName()
508
- await expect.element(getByTestId('svg-title')).toHaveAccessibleName('Test title')
509
- await expect.element(getByTestId('button-img-alt')).toHaveAccessibleName()
510
- await expect.element(getByTestId('img-paragraph')).not.toHaveAccessibleName()
511
- await expect.element(getByTestId('svg-button')).toHaveAccessibleName()
512
- await expect.element(getByTestId('svg-without-title')).not.toHaveAccessibleName()
513
- await expect.element(getByTestId('input-title')).toHaveAccessibleName()
514
- ```
515
-
516
- ## toHaveAttribute
517
-
518
- ```ts
519
- function toHaveAttribute(attribute: string, value?: unknown): Promise<void>
520
- ```
521
-
522
- This allows you to check whether the given element has an attribute or not. You
523
- can also optionally check that the attribute has a specific expected value or
524
- partial match using [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching).
525
-
526
- ```html
527
- <button data-testid="ok-button" type="submit" disabled>ok</button>
528
- ```
529
-
530
- ```ts
531
- const button = getByTestId('ok-button')
532
-
533
- await expect.element(button).toHaveAttribute('disabled')
534
- await expect.element(button).toHaveAttribute('type', 'submit')
535
- await expect.element(button).not.toHaveAttribute('type', 'button')
536
-
537
- await expect.element(button).toHaveAttribute(
538
- 'type',
539
- expect.stringContaining('sub')
540
- )
541
- await expect.element(button).toHaveAttribute(
542
- 'type',
543
- expect.not.stringContaining('but')
544
- )
545
- ```
546
-
547
- ## toHaveClass
548
-
549
- ```ts
550
- function toHaveClass(...classNames: string[], options?: { exact: boolean }): Promise<void>
551
- function toHaveClass(...classNames: (string | RegExp)[]): Promise<void>
552
- ```
553
-
554
- This allows you to check whether the given element has certain classes within
555
- its `class` attribute. You must provide at least one class, unless you are
556
- asserting that an element does not have any classes.
557
-
558
- The list of class names may include strings and regular expressions. Regular
559
- expressions are matched against each individual class in the target element, and
560
- it is NOT matched against its full `class` attribute value as whole.
561
-
562
- ::: warning
563
- Note that you cannot use `exact: true` option when only regular expressions are provided.
564
- :::
565
-
566
- ```html
567
- <button data-testid="delete-button" class="btn extra btn-danger">
568
- Delete item
569
- </button>
570
- <button data-testid="no-classes">No Classes</button>
571
- ```
572
-
573
- ```ts
574
- const deleteButton = getByTestId('delete-button')
575
- const noClasses = getByTestId('no-classes')
576
-
577
- await expect.element(deleteButton).toHaveClass('extra')
578
- await expect.element(deleteButton).toHaveClass('btn-danger btn')
579
- await expect.element(deleteButton).toHaveClass(/danger/, 'btn')
580
- await expect.element(deleteButton).toHaveClass('btn-danger', 'btn')
581
- await expect.element(deleteButton).not.toHaveClass('btn-link')
582
- await expect.element(deleteButton).not.toHaveClass(/link/)
583
-
584
- // ⚠️ regexp matches against individual classes, not the whole classList
585
- await expect.element(deleteButton).not.toHaveClass(/btn extra/)
586
-
587
- // the element has EXACTLY a set of classes (in any order)
588
- await expect.element(deleteButton).toHaveClass('btn-danger extra btn', {
589
- exact: true
590
- })
591
- // if it has more than expected it is going to fail
592
- await expect.element(deleteButton).not.toHaveClass('btn-danger extra', {
593
- exact: true
594
- })
595
-
596
- await expect.element(noClasses).not.toHaveClass()
597
- ```
598
-
599
- ## toHaveFocus
600
-
601
- ```ts
602
- function toHaveFocus(): Promise<void>
603
- ```
604
-
605
- This allows you to assert whether an element has focus or not.
606
-
607
- ```html
608
- <div><input type="text" data-testid="element-to-focus" /></div>
609
- ```
610
-
611
- ```ts
612
- const input = page.getByTestId('element-to-focus')
613
- input.element().focus()
614
- await expect.element(input).toHaveFocus()
615
- input.element().blur()
616
- await expect.element(input).not.toHaveFocus()
617
- ```
618
-
619
- ## toHaveFormValues
620
-
621
- ```ts
622
- function toHaveFormValues(expectedValues: Record<string, unknown>): Promise<void>
623
- ```
624
-
625
- This allows you to check if a form or fieldset contains form controls for each given name, and having the specified value.
626
-
627
- ::: tip
628
- It is important to stress that this matcher can only be invoked on a [form](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement) or a [fieldset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFieldSetElement) element.
629
-
630
- This allows it to take advantage of the [`.elements`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements) property in `form` and `fieldset` to reliably fetch all form controls within them.
631
-
632
- This also avoids the possibility that users provide a container that contains more than one `form`, thereby intermixing form controls that are not related, and could even conflict with one another.
633
- :::
634
-
635
- This matcher abstracts away the particularities with which a form control value
636
- is obtained depending on the type of form control. For instance, `<input>`
637
- elements have a `value` attribute, but `<select>` elements do not. Here's a list
638
- of all cases covered:
639
-
640
- - `<input type="number">` elements return the value as a **number**, instead of
641
- a string.
642
- - `<input type="checkbox">` elements:
643
- - if there's a single one with the given `name` attribute, it is treated as a
644
- **boolean**, returning `true` if the checkbox is checked, `false` if
645
- unchecked.
646
- - if there's more than one checkbox with the same `name` attribute, they are
647
- all treated collectively as a single form control, which returns the value
648
- as an **array** containing all the values of the selected checkboxes in the
649
- collection.
650
- - `<input type="radio">` elements are all grouped by the `name` attribute, and
651
- such a group treated as a single form control. This form control returns the
652
- value as a **string** corresponding to the `value` attribute of the selected
653
- radio button within the group.
654
- - `<input type="text">` elements return the value as a **string**. This also
655
- applies to `<input>` elements having any other possible `type` attribute
656
- that's not explicitly covered in different rules above (e.g. `search`,
657
- `email`, `date`, `password`, `hidden`, etc.)
658
- - `<select>` elements without the `multiple` attribute return the value as a
659
- **string** corresponding to the `value` attribute of the selected `option`, or
660
- `undefined` if there's no selected option.
661
- - `<select multiple>` elements return the value as an **array** containing all
662
- the values of the [selected options](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions).
663
- - `<textarea>` elements return their value as a **string**. The value
664
- corresponds to their node content.
665
-
666
- The above rules make it easy, for instance, to switch from using a single select
667
- control to using a group of radio buttons. Or to switch from a multi select
668
- control, to using a group of checkboxes. The resulting set of form values used
669
- by this matcher to compare against would be the same.
670
-
671
- ```html
672
- <form data-testid="login-form">
673
- <input type="text" name="username" value="jane.doe" />
674
- <input type="password" name="password" value="12345678" />
675
- <input type="checkbox" name="rememberMe" checked />
676
- <button type="submit">Sign in</button>
677
- </form>
678
- ```
679
-
680
- ```ts
681
- await expect.element(getByTestId('login-form')).toHaveFormValues({
682
- username: 'jane.doe',
683
- rememberMe: true,
684
- })
685
- ```
686
-
687
- ## toHaveStyle
688
-
689
- ```ts
690
- function toHaveStyle(css: string | Partial<CSSStyleDeclaration>): Promise<void>
691
- ```
692
-
693
- This allows you to check if a certain element has some specific css properties
694
- with specific values applied. It matches only if the element has _all_ the
695
- expected properties applied, not just some of them.
696
-
697
- ```html
698
- <button
699
- data-testid="delete-button"
700
- style="display: none; background-color: red"
701
- >
702
- Delete item
703
- </button>
704
- ```
705
-
706
- ```ts
707
- const button = getByTestId('delete-button')
708
-
709
- await expect.element(button).toHaveStyle('display: none')
710
- await expect.element(button).toHaveStyle({ display: 'none' })
711
- await expect.element(button).toHaveStyle(`
712
- background-color: red;
713
- display: none;
714
- `)
715
- await expect.element(button).toHaveStyle({
716
- backgroundColor: 'red',
717
- display: 'none',
718
- })
719
- await expect.element(button).not.toHaveStyle(`
720
- background-color: blue;
721
- display: none;
722
- `)
723
- await expect.element(button).not.toHaveStyle({
724
- backgroundColor: 'blue',
725
- display: 'none',
726
- })
727
- ```
728
-
729
- This also works with rules that are applied to the element via a class name for
730
- which some rules are defined in a stylesheet currently active in the document.
731
- The usual rules of css precedence apply.
732
-
733
- ## toHaveTextContent
734
-
735
- ```ts
736
- function toHaveTextContent(
737
- text: string | RegExp,
738
- options?: { normalizeWhitespace: boolean }
739
- ): Promise<void>
740
- ```
741
-
742
- This allows you to check whether the given node has a text content or not. This
743
- supports elements, but also text nodes and fragments.
744
-
745
- When a `string` argument is passed through, it will perform a partial
746
- case-sensitive match to the node content.
747
-
748
- To perform a case-insensitive match, you can use a `RegExp` with the `/i`
749
- modifier.
750
-
751
- If you want to match the whole content, you can use a `RegExp` to do it.
752
-
753
- ```html
754
- <span data-testid="text-content">Text Content</span>
755
- ```
756
-
757
- ```ts
758
- const element = getByTestId('text-content')
759
-
760
- await expect.element(element).toHaveTextContent('Content')
761
- // to match the whole content
762
- await expect.element(element).toHaveTextContent(/^Text Content$/)
763
- // to use case-insensitive match
764
- await expect.element(element).toHaveTextContent(/content$/i)
765
- await expect.element(element).not.toHaveTextContent('content')
766
- ```
767
-
768
- ## toHaveValue
769
-
770
- ```ts
771
- function toHaveValue(value: string | string[] | number | null): Promise<void>
772
- ```
773
-
774
- This allows you to check whether the given form element has the specified value.
775
- It accepts `<input>`, `<select>` and `<textarea>` elements with the exception of
776
- `<input type="checkbox">` and `<input type="radio">`, which can be meaningfully
777
- matched only using [`toBeChecked`](#tobechecked) or
778
- [`toHaveFormValues`](#tohaveformvalues).
779
-
780
- It also accepts elements with roles `meter`, `progressbar`, `slider` or
781
- `spinbutton` and checks their `aria-valuenow` attribute (as a number).
782
-
783
- For all other form elements, the value is matched using the same algorithm as in
784
- [`toHaveFormValues`](#tohaveformvalues) does.
785
-
786
- ```html
787
- <input type="text" value="text" data-testid="input-text" />
788
- <input type="number" value="5" data-testid="input-number" />
789
- <input type="text" data-testid="input-empty" />
790
- <select multiple data-testid="select-number">
791
- <option value="first">First Value</option>
792
- <option value="second" selected>Second Value</option>
793
- <option value="third" selected>Third Value</option>
794
- </select>
795
- ```
796
-
797
- ```ts
798
- const textInput = getByTestId('input-text')
799
- const numberInput = getByTestId('input-number')
800
- const emptyInput = getByTestId('input-empty')
801
- const selectInput = getByTestId('select-number')
802
-
803
- await expect.element(textInput).toHaveValue('text')
804
- await expect.element(numberInput).toHaveValue(5)
805
- await expect.element(emptyInput).not.toHaveValue()
806
- await expect.element(selectInput).toHaveValue(['second', 'third'])
807
- ```
808
-
809
- ## toHaveDisplayValue
810
-
811
- ```typescript
812
- function toHaveDisplayValue(
813
- value: string | RegExp | (string | RegExp)[]
814
- ): Promise<void>
815
- ```
816
-
817
- This allows you to check whether the given form element has the specified
818
- displayed value (the one the end user will see). It accepts `<input>`,
819
- `<select>` and `<textarea>` elements with the exception of
820
- `<input type="checkbox">` and `<input type="radio">`, which can be meaningfully
821
- matched only using [`toBeChecked`](#tobechecked) or
822
- [`toHaveFormValues`](#tohaveformvalues).
823
-
824
- ```html
825
- <label for="input-example">First name</label>
826
- <input type="text" id="input-example" value="Luca" />
827
-
828
- <label for="textarea-example">Description</label>
829
- <textarea id="textarea-example">An example description here.</textarea>
830
-
831
- <label for="single-select-example">Fruit</label>
832
- <select id="single-select-example">
833
- <option value="">Select a fruit...</option>
834
- <option value="banana">Banana</option>
835
- <option value="ananas">Ananas</option>
836
- <option value="avocado">Avocado</option>
837
- </select>
838
-
839
- <label for="multiple-select-example">Fruits</label>
840
- <select id="multiple-select-example" multiple>
841
- <option value="">Select a fruit...</option>
842
- <option value="banana" selected>Banana</option>
843
- <option value="ananas">Ananas</option>
844
- <option value="avocado" selected>Avocado</option>
845
- </select>
846
- ```
847
-
848
- ```ts
849
- const input = page.getByLabelText('First name')
850
- const textarea = page.getByLabelText('Description')
851
- const selectSingle = page.getByLabelText('Fruit')
852
- const selectMultiple = page.getByLabelText('Fruits')
853
-
854
- await expect.element(input).toHaveDisplayValue('Luca')
855
- await expect.element(input).toHaveDisplayValue(/Luc/)
856
- await expect.element(textarea).toHaveDisplayValue('An example description here.')
857
- await expect.element(textarea).toHaveDisplayValue(/example/)
858
- await expect.element(selectSingle).toHaveDisplayValue('Select a fruit...')
859
- await expect.element(selectSingle).toHaveDisplayValue(/Select/)
860
- await expect.element(selectMultiple).toHaveDisplayValue([/Avocado/, 'Banana'])
861
- ```
862
-
863
- ## toBeChecked
864
-
865
- ```ts
866
- function toBeChecked(): Promise<void>
867
- ```
868
-
869
- This allows you to check whether the given element is checked. It accepts an
870
- `input` of type `checkbox` or `radio` and elements with a `role` of `checkbox`,
871
- `radio` or `switch` with a valid `aria-checked` attribute of `"true"` or
872
- `"false"`.
873
-
874
- ```html
875
- <input type="checkbox" checked data-testid="input-checkbox-checked" />
876
- <input type="checkbox" data-testid="input-checkbox-unchecked" />
877
- <div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />
878
- <div
879
- role="checkbox"
880
- aria-checked="false"
881
- data-testid="aria-checkbox-unchecked"
882
- />
883
-
884
- <input type="radio" checked value="foo" data-testid="input-radio-checked" />
885
- <input type="radio" value="foo" data-testid="input-radio-unchecked" />
886
- <div role="radio" aria-checked="true" data-testid="aria-radio-checked" />
887
- <div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />
888
- <div role="switch" aria-checked="true" data-testid="aria-switch-checked" />
889
- <div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />
890
- ```
891
-
892
- ```ts
893
- const inputCheckboxChecked = getByTestId('input-checkbox-checked')
894
- const inputCheckboxUnchecked = getByTestId('input-checkbox-unchecked')
895
- const ariaCheckboxChecked = getByTestId('aria-checkbox-checked')
896
- const ariaCheckboxUnchecked = getByTestId('aria-checkbox-unchecked')
897
- await expect.element(inputCheckboxChecked).toBeChecked()
898
- await expect.element(inputCheckboxUnchecked).not.toBeChecked()
899
- await expect.element(ariaCheckboxChecked).toBeChecked()
900
- await expect.element(ariaCheckboxUnchecked).not.toBeChecked()
901
-
902
- const inputRadioChecked = getByTestId('input-radio-checked')
903
- const inputRadioUnchecked = getByTestId('input-radio-unchecked')
904
- const ariaRadioChecked = getByTestId('aria-radio-checked')
905
- const ariaRadioUnchecked = getByTestId('aria-radio-unchecked')
906
- await expect.element(inputRadioChecked).toBeChecked()
907
- await expect.element(inputRadioUnchecked).not.toBeChecked()
908
- await expect.element(ariaRadioChecked).toBeChecked()
909
- await expect.element(ariaRadioUnchecked).not.toBeChecked()
910
-
911
- const ariaSwitchChecked = getByTestId('aria-switch-checked')
912
- const ariaSwitchUnchecked = getByTestId('aria-switch-unchecked')
913
- await expect.element(ariaSwitchChecked).toBeChecked()
914
- await expect.element(ariaSwitchUnchecked).not.toBeChecked()
915
- ```
916
-
917
- ## toBePartiallyChecked
918
-
919
- ```typescript
920
- function toBePartiallyChecked(): Promise<void>
921
- ```
922
-
923
- This allows you to check whether the given element is partially checked. It
924
- accepts an `input` of type `checkbox` and elements with a `role` of `checkbox`
925
- with a `aria-checked="mixed"`, or `input` of type `checkbox` with
926
- `indeterminate` set to `true`
927
-
928
- ```html
929
- <input type="checkbox" aria-checked="mixed" data-testid="aria-checkbox-mixed" />
930
- <input type="checkbox" checked data-testid="input-checkbox-checked" />
931
- <input type="checkbox" data-testid="input-checkbox-unchecked" />
932
- <div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />
933
- <div
934
- role="checkbox"
935
- aria-checked="false"
936
- data-testid="aria-checkbox-unchecked"
937
- />
938
- <input type="checkbox" data-testid="input-checkbox-indeterminate" />
939
- ```
940
-
941
- ```ts
942
- const ariaCheckboxMixed = getByTestId('aria-checkbox-mixed')
943
- const inputCheckboxChecked = getByTestId('input-checkbox-checked')
944
- const inputCheckboxUnchecked = getByTestId('input-checkbox-unchecked')
945
- const ariaCheckboxChecked = getByTestId('aria-checkbox-checked')
946
- const ariaCheckboxUnchecked = getByTestId('aria-checkbox-unchecked')
947
- const inputCheckboxIndeterminate = getByTestId('input-checkbox-indeterminate')
948
-
949
- await expect.element(ariaCheckboxMixed).toBePartiallyChecked()
950
- await expect.element(inputCheckboxChecked).not.toBePartiallyChecked()
951
- await expect.element(inputCheckboxUnchecked).not.toBePartiallyChecked()
952
- await expect.element(ariaCheckboxChecked).not.toBePartiallyChecked()
953
- await expect.element(ariaCheckboxUnchecked).not.toBePartiallyChecked()
954
-
955
- inputCheckboxIndeterminate.element().indeterminate = true
956
- await expect.element(inputCheckboxIndeterminate).toBePartiallyChecked()
957
- ```
958
-
959
- ## toHaveRole
960
-
961
- ```ts
962
- function toHaveRole(role: ARIARole): Promise<void>
963
- ```
964
-
965
- This allows you to assert that an element has the expected [role](https://www.w3.org/TR/html-aria/#docconformance).
966
-
967
- This is useful in cases where you already have access to an element via some query other than the role itself, and want to make additional assertions regarding its accessibility.
968
-
969
- The role can match either an explicit role (via the `role` attribute), or an implicit one via the [implicit ARIA semantics](https://www.w3.org/TR/html-aria/#docconformance).
970
-
971
- ```html
972
- <button data-testid="button">Continue</button>
973
- <div role="button" data-testid="button-explicit">Continue</button>
974
- <button role="switch button" data-testid="button-explicit-multiple">Continue</button>
975
- <a href="/about" data-testid="link">About</a>
976
- <a data-testid="link-invalid">Invalid link<a/>
977
- ```
978
-
979
- ```ts
980
- await expect.element(getByTestId('button')).toHaveRole('button')
981
- await expect.element(getByTestId('button-explicit')).toHaveRole('button')
982
- await expect.element(getByTestId('button-explicit-multiple')).toHaveRole('button')
983
- await expect.element(getByTestId('button-explicit-multiple')).toHaveRole('switch')
984
- await expect.element(getByTestId('link')).toHaveRole('link')
985
- await expect.element(getByTestId('link-invalid')).not.toHaveRole('link')
986
- await expect.element(getByTestId('link-invalid')).toHaveRole('generic')
987
- ```
988
-
989
- ::: warning
990
- Roles are matched literally by string equality, without inheriting from the ARIA role hierarchy. As a result, querying a superclass role like `checkbox` will not include elements with a subclass role like `switch`.
991
-
992
- Also note that unlike `testing-library`, Vitest ignores all custom roles except the first valid one, following Playwright's behaviour:
993
-
994
- ```jsx
995
- <div data-testid="switch" role="switch alert"></div>
996
-
997
- await expect.element(getByTestId('switch')).toHaveRole('switch') // ✅
998
- await expect.element(getByTestId('switch')).toHaveRole('alert') // ❌
999
- ```
1000
- :::
1001
-
1002
- ## toHaveSelection
1003
-
1004
- ```ts
1005
- function toHaveSelection(selection?: string): Promise<void>
1006
- ```
1007
-
1008
- This allows to assert that an element has a
1009
- [text selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection).
1010
-
1011
- This is useful to check if text or part of the text is selected within an
1012
- element. The element can be either an input of type text, a textarea, or any
1013
- other element that contains text, such as a paragraph, span, div etc.
1014
-
1015
- ::: warning
1016
- The expected selection is a string, it does not allow to check for
1017
- selection range indices.
1018
- :::
1019
-
1020
- ```html
1021
- <div>
1022
- <input type="text" value="text selected text" data-testid="text" />
1023
- <textarea data-testid="textarea">text selected text</textarea>
1024
- <p data-testid="prev">prev</p>
1025
- <p data-testid="parent">
1026
- text <span data-testid="child">selected</span> text
1027
- </p>
1028
- <p data-testid="next">next</p>
1029
- </div>
1030
- ```
1031
-
1032
- ```ts
1033
- getByTestId('text').element().setSelectionRange(5, 13)
1034
- await expect.element(getByTestId('text')).toHaveSelection('selected')
1035
-
1036
- getByTestId('textarea').element().setSelectionRange(0, 5)
1037
- await expect.element('textarea').toHaveSelection('text ')
1038
-
1039
- const selection = document.getSelection()
1040
- const range = document.createRange()
1041
- selection.removeAllRanges()
1042
- selection.empty()
1043
- selection.addRange(range)
1044
-
1045
- // selection of child applies to the parent as well
1046
- range.selectNodeContents(getByTestId('child').element())
1047
- await expect.element(getByTestId('child')).toHaveSelection('selected')
1048
- await expect.element(getByTestId('parent')).toHaveSelection('selected')
1049
-
1050
- // selection that applies from prev all, parent text before child, and part child.
1051
- range.setStart(getByTestId('prev').element(), 0)
1052
- range.setEnd(getByTestId('child').element().childNodes[0], 3)
1053
- await expect.element(queryByTestId('prev')).toHaveSelection('prev')
1054
- await expect.element(queryByTestId('child')).toHaveSelection('sel')
1055
- await expect.element(queryByTestId('parent')).toHaveSelection('text sel')
1056
- await expect.element(queryByTestId('next')).not.toHaveSelection()
1057
-
1058
- // selection that applies from part child, parent text after child and part next.
1059
- range.setStart(getByTestId('child').element().childNodes[0], 3)
1060
- range.setEnd(getByTestId('next').element().childNodes[0], 2)
1061
- await expect.element(queryByTestId('child')).toHaveSelection('ected')
1062
- await expect.element(queryByTestId('parent')).toHaveSelection('ected text')
1063
- await expect.element(queryByTestId('prev')).not.toHaveSelection()
1064
- await expect.element(queryByTestId('next')).toHaveSelection('ne')
1065
- ```
1066
-
1067
- ## toMatchScreenshot <Badge type="warning">experimental</Badge> {#tomatchscreenshot}
1068
-
1069
- ```ts
1070
- function toMatchScreenshot(
1071
- options?: ScreenshotMatcherOptions,
1072
- ): Promise<void>
1073
- function toMatchScreenshot(
1074
- name?: string,
1075
- options?: ScreenshotMatcherOptions,
1076
- ): Promise<void>
1077
- ```
1078
-
1079
- ::: tip
1080
- The `toMatchScreenshot` assertion can be configured globally in your
1081
- [Vitest config](/config/browser/expect#tomatchscreenshot).
1082
- :::
1083
-
1084
- This assertion allows you to perform visual regression testing by comparing
1085
- screenshots of elements or pages against stored reference images.
1086
-
1087
- When differences are detected beyond the configured threshold, the test fails.
1088
- To help identify the changes, the assertion generates:
1089
-
1090
- - The actual screenshot captured during the test
1091
- - The expected reference screenshot
1092
- - A diff image highlighting the differences (when possible)
1093
-
1094
- ::: warning Screenshots Stability
1095
- The assertion automatically retries taking screenshots until two consecutive
1096
- captures yield the same result. This helps reduce flakiness caused by
1097
- animations, loading states, or other dynamic content. You can control this
1098
- behavior with the `timeout` option.
1099
-
1100
- However, browser rendering can vary across:
1101
-
1102
- - Different browsers and browser versions
1103
- - Operating systems (Windows, macOS, Linux)
1104
- - Screen resolutions and pixel densities
1105
- - GPU drivers and hardware acceleration
1106
- - Font rendering and system fonts
1107
-
1108
- It is recommended to read the
1109
- [Visual Regression Testing guide](/guide/browser/visual-regression-testing) to
1110
- implement this testing strategy efficiently.
1111
- :::
1112
-
1113
- ::: tip
1114
- When a screenshot comparison fails due to **intentional changes**, you can
1115
- update the reference screenshot by pressing the `u` key in watch mode, or by
1116
- running tests with the `-u` or `--update` flags.
1117
- :::
1118
-
1119
- ```html
1120
- <button data-testid="button">Fancy Button</button>
1121
- ```
1122
-
1123
- ```ts
1124
- // basic usage, auto-generates screenshot name
1125
- await expect.element(getByTestId('button')).toMatchScreenshot()
1126
-
1127
- // with custom name
1128
- await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button')
1129
-
1130
- // with options
1131
- await expect.element(getByTestId('button')).toMatchScreenshot({
1132
- comparatorName: 'pixelmatch',
1133
- comparatorOptions: {
1134
- allowedMismatchedPixelRatio: 0.01,
1135
- },
1136
- })
1137
-
1138
- // with both name and options
1139
- await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
1140
- comparatorName: 'pixelmatch',
1141
- comparatorOptions: {
1142
- allowedMismatchedPixelRatio: 0.01,
1143
- },
1144
- })
1145
- ```
1146
-
1147
- ### Options
1148
-
1149
- - `comparatorName: "pixelmatch" = "pixelmatch"`
1150
-
1151
- The algorithm/library used for comparing images.
1152
-
1153
- `"pixelmatch"` is the only built-in comparator, but you can use custom ones by [registering them in the config file](/config/browser/expect#browser-expect-tomatchscreenshot-comparators).
1154
-
1155
- - `comparatorOptions: object`
1156
-
1157
- These options allow changing the behavior of the comparator. What properties
1158
- can be set depends on the chosen comparator algorithm.
1159
-
1160
- Vitest has set default values out of the box, but they can be overridden.
1161
-
1162
- - [`"pixelmatch"` options](#pixelmatch-comparator-options)
1163
-
1164
- ::: warning
1165
- **Always explicitly set `comparatorName` to get proper type inference for
1166
- `comparatorOptions`**.
1167
-
1168
- Without it, TypeScript won't know which options are valid:
1169
-
1170
- ```ts
1171
- // ❌ TypeScript can't infer the correct options
1172
- await expect.element(button).toMatchScreenshot({
1173
- comparatorOptions: {
1174
- // might error when new comparators are added
1175
- allowedMismatchedPixelRatio: 0.01,
1176
- },
1177
- })
1178
-
1179
- // ✅ TypeScript knows these are pixelmatch options
1180
- await expect.element(button).toMatchScreenshot({
1181
- comparatorName: 'pixelmatch',
1182
- comparatorOptions: {
1183
- allowedMismatchedPixelRatio: 0.01,
1184
- },
1185
- })
1186
- ```
1187
- :::
1188
-
1189
- - `screenshotOptions: object`
1190
-
1191
- The same options allowed by
1192
- [`locator.screenshot()`](/api/browser/locators.html#screenshot), except for:
1193
-
1194
- - `'base64'`
1195
- - `'path'`
1196
- - `'save'`
1197
- - `'type'`
1198
-
1199
- - `timeout: number = 5_000`
1200
-
1201
- Time to wait until a stable screenshot is found.
1202
-
1203
- Setting this value to `0` disables the timeout, but if a stable screenshot
1204
- can't be determined the process will not end.
1205
-
1206
- #### `"pixelmatch"` comparator options
1207
-
1208
- The `"pixelmatch"` comparator uses [`@blazediff/core`](https://blazediff.dev/docs/core) under the hood. The following options are available when using it:
1209
-
1210
- - `allowedMismatchedPixelRatio: number | undefined = undefined`
1211
-
1212
- The maximum allowed ratio of differing pixels between the captured screenshot
1213
- and the reference image.
1214
-
1215
- Must be a value between `0` and `1`.
1216
-
1217
- For example, `allowedMismatchedPixelRatio: 0.02` means the test will pass
1218
- if up to 2% of pixels differ, but fail if more than 2% differ.
1219
-
1220
- - `allowedMismatchedPixels: number | undefined = undefined`
1221
-
1222
- The maximum number of pixels that are allowed to differ between the captured
1223
- screenshot and the stored reference image.
1224
-
1225
- If set to `undefined`, any non-zero difference will cause the test to fail.
1226
-
1227
- For example, `allowedMismatchedPixels: 10` means the test will pass if 10 or
1228
- fewer pixels differ, but fail if 11 or more differ.
1229
-
1230
- - `threshold: number = 0.1`
1231
-
1232
- Acceptable perceived color difference between the same pixel in two images.
1233
-
1234
- Value ranges from `0` (strict) to `1` (very lenient). Lower values mean small
1235
- differences will be detected.
1236
-
1237
- The comparison uses the [YIQ color space](https://en.wikipedia.org/wiki/YIQ).
1238
-
1239
- - `includeAA: boolean = false`
1240
-
1241
- If `true`, disables detection and ignoring of anti-aliased pixels.
1242
-
1243
- - `alpha: number = 0.1`
1244
-
1245
- Blending level of unchanged pixels in the diff image.
1246
-
1247
- Ranges from `0` (white) to `1` (original brightness).
1248
-
1249
- - `aaColor: [r: number, g: number, b: number] = [255, 255, 0]`
1250
-
1251
- Color used for anti-aliased pixels in the diff image.
1252
-
1253
- - `diffColor: [r: number, g: number, b: number] = [255, 0, 0]`
1254
-
1255
- Color used for differing pixels in the diff image.
1256
-
1257
- - `diffColorAlt: [r: number, g: number, b: number] | undefined = undefined`
1258
-
1259
- Optional alternative color for dark-on-light differences, to help show what's
1260
- added vs. removed.
1261
-
1262
- If not set, `diffColor` is used for all differences.
1263
-
1264
- - `diffMask: boolean = false`
1265
-
1266
- If `true`, shows only the diff as a mask on a transparent background, instead
1267
- of overlaying it on the original image.
1268
-
1269
- Anti-aliased pixels won't be shown (if detected).
1270
-
1271
- ::: warning
1272
- When both `allowedMismatchedPixels` and `allowedMismatchedPixelRatio` are set,
1273
- the more restrictive value is used.
1274
-
1275
- For example, if you allow 100 pixels or 2% ratio, and your image has 10,000
1276
- pixels, the effective limit would be 100 pixels instead of 200.
1277
- :::