--- title: Assertion API | Browser Mode --- # Assertion API 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. ::: tip TypeScript Support 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`: ```ts /// ``` ::: 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: ```ts import { expect, test } from 'vitest' import { page } from 'vitest/browser' test('error banner is rendered', async () => { triggerError() // This creates a locator that will try to find the element // when any of its methods are called. // This call by itself doesn't check the existence of the element. const banner = page.getByRole('alert', { name: /error/i, }) // Vitest provides `expect.element` with built-in retry-ability // It will repeatedly check that the element exists in the DOM and that // the content of `element.textContent` is equal to "Error!" // until all the conditions are met await expect.element(banner).toHaveTextContent('Error!') }) ``` 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: ```ts interface ExpectPollOptions { // The interval to retry the assertion for in milliseconds // Defaults to "expect.poll.interval" config option interval?: number // Time to retry the assertion for in milliseconds // Defaults to "expect.poll.timeout" config option timeout?: number // The message printed when the assertion fails message?: string } ``` ::: tip `expect.element` is a shorthand for `expect.poll(() => element)` and works in exactly the same way. `toHaveTextContent` and all other assertions are still available on a regular `expect` without a built-in retry-ability mechanism: ```ts // will fail immediately if .textContent is not `'Error!'` expect(banner).toHaveTextContent('Error!') ``` ::: ## toBeDisabled ```ts function toBeDisabled(): Promise ``` Allows you to check whether an element is disabled from the user's perspective. Matches if the element is a form control and the `disabled` attribute is specified on this element or the element is a descendant of a form element with a `disabled` attribute. Note that only native control elements such as HTML `button`, `input`, `select`, `textarea`, `option`, `optgroup` can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored, unless it's a custom element. ```html ``` ```ts await expect.element(getByTestId('button')).toBeDisabled() // ✅ await expect.element(getByTestId('button')).not.toBeDisabled() // ❌ ``` ## toBeEnabled ```ts function toBeEnabled(): Promise ``` Allows you to check whether an element is not disabled from the user's perspective. Works like [`not.toBeDisabled()`](#tobedisabled). Use this matcher to avoid double negation in your tests. ```html ``` ```ts await expect.element(getByTestId('button')).toBeEnabled() // ✅ await expect.element(getByTestId('button')).not.toBeEnabled() // ❌ ``` ## toBeEmptyDOMElement ```ts function toBeEmptyDOMElement(): Promise ``` 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. ```html ``` ```ts await expect.element(getByTestId('empty')).toBeEmptyDOMElement() await expect.element(getByTestId('not-empty')).not.toBeEmptyDOMElement() await expect.element( getByTestId('with-whitespace') ).not.toBeEmptyDOMElement() ``` ## toBeInTheDocument ```ts function toBeInTheDocument(): Promise ``` Assert whether an element is present in the document or not. ```html ``` ```ts await expect.element(getByTestId('svg-element')).toBeInTheDocument() await expect.element(getByTestId('does-not-exist')).not.toBeInTheDocument() ``` ::: warning 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). ::: ## toBeInvalid ```ts function toBeInvalid(): Promise ``` This allows you to check if an element, is currently invalid. 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`. ```html
``` ```ts await expect.element(getByTestId('no-aria-invalid')).not.toBeInvalid() await expect.element(getByTestId('aria-invalid')).toBeInvalid() await expect.element(getByTestId('aria-invalid-value')).toBeInvalid() await expect.element(getByTestId('aria-invalid-false')).not.toBeInvalid() await expect.element(getByTestId('valid-form')).not.toBeInvalid() await expect.element(getByTestId('invalid-form')).toBeInvalid() ``` ## toBeRequired ```ts function toBeRequired(): Promise ``` This allows you to check if a form element is currently required. An element is required if it is having a `required` or `aria-required="true"` attribute. ```html
``` ```ts await expect.element(getByTestId('required-input')).toBeRequired() await expect.element(getByTestId('aria-required-input')).toBeRequired() await expect.element(getByTestId('conflicted-input')).toBeRequired() await expect.element(getByTestId('aria-not-required-input')).not.toBeRequired() await expect.element(getByTestId('optional-input')).not.toBeRequired() await expect.element(getByTestId('unsupported-type')).not.toBeRequired() await expect.element(getByTestId('select')).toBeRequired() await expect.element(getByTestId('textarea')).toBeRequired() await expect.element(getByTestId('supported-role')).not.toBeRequired() await expect.element(getByTestId('supported-role-aria')).toBeRequired() ``` ## toBeValid ```ts function toBeValid(): Promise ``` This allows you to check if the value of an element, is currently valid. 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. ```html
``` ```ts await expect.element(getByTestId('no-aria-invalid')).toBeValid() await expect.element(getByTestId('aria-invalid')).not.toBeValid() await expect.element(getByTestId('aria-invalid-value')).not.toBeValid() await expect.element(getByTestId('aria-invalid-false')).toBeValid() await expect.element(getByTestId('valid-form')).toBeValid() await expect.element(getByTestId('invalid-form')).not.toBeValid() ``` ## toBeVisible ```ts function toBeVisible(): Promise ``` This allows you to check if an element is currently visible to the user. Element is considered visible when it has non-empty bounding box and does not have `visibility:hidden` computed style. Note that according to this definition: - Elements of zero size **are not** considered visible. - Elements with `display:none` **are not** considered visible. - Elements with `opacity:0` **are** considered visible. To check that at least one element from the list is visible, use `locator.first()`. ```ts // A specific element is visible. await expect.element(page.getByText('Welcome')).toBeVisible() // At least one item in the list is visible. await expect.element(page.getByTestId('todo-item').first()).toBeVisible() // At least one of the two elements is visible, possibly both. await expect.element( page.getByRole('button', { name: 'Sign in' }) .or(page.getByRole('button', { name: 'Sign up' })) .first() ).toBeVisible() ``` ## toBeInViewport ```ts function toBeInViewport(options: { ratio?: number }): Promise ``` 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). 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. ```ts // A specific element is in viewport. await expect.element(page.getByText('Welcome')).toBeInViewport() // 50% of a specific element should be in viewport await expect.element(page.getByText('To')).toBeInViewport({ ratio: 0.5 }) // Full of a specific element should be in viewport await expect.element(page.getByText('Vitest')).toBeInViewport({ ratio: 1 }) ``` ## toContainElement ```ts function toContainElement(element: HTMLElement | SVGElement | null): Promise ``` This allows you to assert whether an element contains another element as a descendant or not. ```html ``` ```ts const ancestor = getByTestId('ancestor') const descendant = getByTestId('descendant') const nonExistantElement = getByTestId('does-not-exist') await expect.element(ancestor).toContainElement(descendant) await expect.element(descendant).not.toContainElement(ancestor) await expect.element(ancestor).not.toContainElement(nonExistantElement) ``` ## toContainHTML ```ts function toContainHTML(htmlText: string): Promise ``` Assert whether a string representing a HTML element is contained in another element. The string should contain valid html, and not any incomplete html. ```html ``` ```ts // These are valid usages await expect.element(getByTestId('parent')).toContainHTML('') await expect.element(getByTestId('parent')).toContainHTML('') await expect.element(getByTestId('parent')).not.toContainHTML('
') // These won't work await expect.element(getByTestId('parent')).toContainHTML('data-testid="child"') await expect.element(getByTestId('parent')).toContainHTML('data-testid') await expect.element(getByTestId('parent')).toContainHTML('
') ``` ::: warning 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. 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 that html code was used as intended. It should not be used to check DOM structure that you control. Please, use [`toContainElement`](#tocontainelement) instead. ::: ## toHaveAccessibleDescription ```ts function toHaveAccessibleDescription(description?: string | RegExp): Promise ``` This allows you to assert that an element has the expected [accessible description](https://w3c.github.io/accname/). You can pass the exact string of the expected accessible description, or you can make a partial match passing a regular expression, or by using [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching). ```html Start About User profile pic Company logo The logo of Our Company Company logo ``` ```ts await expect.element(getByTestId('link')).toHaveAccessibleDescription() await expect.element(getByTestId('link')).toHaveAccessibleDescription('A link to start over') await expect.element(getByTestId('link')).not.toHaveAccessibleDescription('Home page') await expect.element(getByTestId('extra-link')).not.toHaveAccessibleDescription() await expect.element(getByTestId('avatar')).not.toHaveAccessibleDescription() await expect.element(getByTestId('logo')).not.toHaveAccessibleDescription('Company logo') await expect.element(getByTestId('logo')).toHaveAccessibleDescription( 'The logo of Our Company', ) await expect.element(getByTestId('logo2')).toHaveAccessibleDescription( 'The logo of Our Company', ) ``` ## toHaveAccessibleErrorMessage ```ts function toHaveAccessibleErrorMessage(message?: string | RegExp): Promise ``` This allows you to assert that an element has the expected [accessible error message](https://w3c.github.io/aria/#aria-errormessage). You can pass the exact string of the expected accessible error message. Alternatively, you can perform a partial match by passing a regular expression or by using [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching). ```html ``` ```ts // Inputs with Valid Error Messages await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage() await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage( 'This field is invalid', ) await expect.element(getByRole('textbox', { name: 'Has Error' })).toHaveAccessibleErrorMessage( /invalid/i, ) await expect.element( getByRole('textbox', { name: 'Has Error' }), ).not.toHaveAccessibleErrorMessage('This field is absolutely correct!') // Inputs without Valid Error Messages await expect.element( getByRole('textbox', { name: 'No Error Attributes' }), ).not.toHaveAccessibleErrorMessage() await expect.element( getByRole('textbox', { name: 'Not Invalid' }), ).not.toHaveAccessibleErrorMessage() ``` ## toHaveAccessibleName ```ts function toHaveAccessibleName(name?: string | RegExp): Promise ``` This allows you to assert that an element has the expected [accessible name](https://w3c.github.io/accname/). It is useful, for instance, to assert that form elements and buttons are properly labelled. You can pass the exact string of the expected accessible name, or you can make a partial match passing a regular expression, or by using [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching). ```html Test alt Test title

Test content

``` ```ts const button = getByTestId('ok-button') await expect.element(button).toHaveAttribute('disabled') await expect.element(button).toHaveAttribute('type', 'submit') await expect.element(button).not.toHaveAttribute('type', 'button') await expect.element(button).toHaveAttribute( 'type', expect.stringContaining('sub') ) await expect.element(button).toHaveAttribute( 'type', expect.not.stringContaining('but') ) ``` ## toHaveClass ```ts function toHaveClass(...classNames: string[], options?: { exact: boolean }): Promise function toHaveClass(...classNames: (string | RegExp)[]): Promise ``` This allows you to check whether the given element has certain classes within its `class` attribute. You must provide at least one class, unless you are asserting that an element does not have any classes. The list of class names may include strings and regular expressions. Regular expressions are matched against each individual class in the target element, and it is NOT matched against its full `class` attribute value as whole. ::: warning Note that you cannot use `exact: true` option when only regular expressions are provided. ::: ```html ``` ```ts const deleteButton = getByTestId('delete-button') const noClasses = getByTestId('no-classes') await expect.element(deleteButton).toHaveClass('extra') await expect.element(deleteButton).toHaveClass('btn-danger btn') await expect.element(deleteButton).toHaveClass(/danger/, 'btn') await expect.element(deleteButton).toHaveClass('btn-danger', 'btn') await expect.element(deleteButton).not.toHaveClass('btn-link') await expect.element(deleteButton).not.toHaveClass(/link/) // ⚠️ regexp matches against individual classes, not the whole classList await expect.element(deleteButton).not.toHaveClass(/btn extra/) // the element has EXACTLY a set of classes (in any order) await expect.element(deleteButton).toHaveClass('btn-danger extra btn', { exact: true }) // if it has more than expected it is going to fail await expect.element(deleteButton).not.toHaveClass('btn-danger extra', { exact: true }) await expect.element(noClasses).not.toHaveClass() ``` ## toHaveFocus ```ts function toHaveFocus(): Promise ``` This allows you to assert whether an element has focus or not. ```html
``` ```ts const input = page.getByTestId('element-to-focus') input.element().focus() await expect.element(input).toHaveFocus() input.element().blur() await expect.element(input).not.toHaveFocus() ``` ## toHaveFormValues ```ts function toHaveFormValues(expectedValues: Record): Promise ``` This allows you to check if a form or fieldset contains form controls for each given name, and having the specified value. ::: tip 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. 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. 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. ::: This matcher abstracts away the particularities with which a form control value is obtained depending on the type of form control. For instance, `` elements have a `value` attribute, but `` elements return the value as a **number**, instead of a string. - `` elements: - if there's a single one with the given `name` attribute, it is treated as a **boolean**, returning `true` if the checkbox is checked, `false` if unchecked. - if there's more than one checkbox with the same `name` attribute, they are all treated collectively as a single form control, which returns the value as an **array** containing all the values of the selected checkboxes in the collection. - `` elements are all grouped by the `name` attribute, and such a group treated as a single form control. This form control returns the value as a **string** corresponding to the `value` attribute of the selected radio button within the group. - `` elements return the value as a **string**. This also applies to `` elements having any other possible `type` attribute that's not explicitly covered in different rules above (e.g. `search`, `email`, `date`, `password`, `hidden`, etc.) - `` elements return the value as an **array** containing all the values of the [selected options](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions). - ` ``` ```ts const input = page.getByLabelText('First name') const textarea = page.getByLabelText('Description') const selectSingle = page.getByLabelText('Fruit') const selectMultiple = page.getByLabelText('Fruits') await expect.element(input).toHaveDisplayValue('Luca') await expect.element(input).toHaveDisplayValue(/Luc/) await expect.element(textarea).toHaveDisplayValue('An example description here.') await expect.element(textarea).toHaveDisplayValue(/example/) await expect.element(selectSingle).toHaveDisplayValue('Select a fruit...') await expect.element(selectSingle).toHaveDisplayValue(/Select/) await expect.element(selectMultiple).toHaveDisplayValue([/Avocado/, 'Banana']) ``` ## toBeChecked ```ts function toBeChecked(): Promise ``` This allows you to check whether the given element is checked. It accepts an `input` of type `checkbox` or `radio` and elements with a `role` of `checkbox`, `radio` or `switch` with a valid `aria-checked` attribute of `"true"` or `"false"`. ```html
``` ```ts const inputCheckboxChecked = getByTestId('input-checkbox-checked') const inputCheckboxUnchecked = getByTestId('input-checkbox-unchecked') const ariaCheckboxChecked = getByTestId('aria-checkbox-checked') const ariaCheckboxUnchecked = getByTestId('aria-checkbox-unchecked') await expect.element(inputCheckboxChecked).toBeChecked() await expect.element(inputCheckboxUnchecked).not.toBeChecked() await expect.element(ariaCheckboxChecked).toBeChecked() await expect.element(ariaCheckboxUnchecked).not.toBeChecked() const inputRadioChecked = getByTestId('input-radio-checked') const inputRadioUnchecked = getByTestId('input-radio-unchecked') const ariaRadioChecked = getByTestId('aria-radio-checked') const ariaRadioUnchecked = getByTestId('aria-radio-unchecked') await expect.element(inputRadioChecked).toBeChecked() await expect.element(inputRadioUnchecked).not.toBeChecked() await expect.element(ariaRadioChecked).toBeChecked() await expect.element(ariaRadioUnchecked).not.toBeChecked() const ariaSwitchChecked = getByTestId('aria-switch-checked') const ariaSwitchUnchecked = getByTestId('aria-switch-unchecked') await expect.element(ariaSwitchChecked).toBeChecked() await expect.element(ariaSwitchUnchecked).not.toBeChecked() ``` ## toBePartiallyChecked ```typescript function toBePartiallyChecked(): Promise ``` This allows you to check whether the given element is partially checked. It accepts an `input` of type `checkbox` and elements with a `role` of `checkbox` with a `aria-checked="mixed"`, or `input` of type `checkbox` with `indeterminate` set to `true` ```html
``` ```ts const ariaCheckboxMixed = getByTestId('aria-checkbox-mixed') const inputCheckboxChecked = getByTestId('input-checkbox-checked') const inputCheckboxUnchecked = getByTestId('input-checkbox-unchecked') const ariaCheckboxChecked = getByTestId('aria-checkbox-checked') const ariaCheckboxUnchecked = getByTestId('aria-checkbox-unchecked') const inputCheckboxIndeterminate = getByTestId('input-checkbox-indeterminate') await expect.element(ariaCheckboxMixed).toBePartiallyChecked() await expect.element(inputCheckboxChecked).not.toBePartiallyChecked() await expect.element(inputCheckboxUnchecked).not.toBePartiallyChecked() await expect.element(ariaCheckboxChecked).not.toBePartiallyChecked() await expect.element(ariaCheckboxUnchecked).not.toBePartiallyChecked() inputCheckboxIndeterminate.element().indeterminate = true await expect.element(inputCheckboxIndeterminate).toBePartiallyChecked() ``` ## toHaveRole ```ts function toHaveRole(role: ARIARole): Promise ``` This allows you to assert that an element has the expected [role](https://www.w3.org/TR/html-aria/#docconformance). 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. 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). ```html
Continue About Invalid link ``` ```ts await expect.element(getByTestId('button')).toHaveRole('button') await expect.element(getByTestId('button-explicit')).toHaveRole('button') await expect.element(getByTestId('button-explicit-multiple')).toHaveRole('button') await expect.element(getByTestId('button-explicit-multiple')).toHaveRole('switch') await expect.element(getByTestId('link')).toHaveRole('link') await expect.element(getByTestId('link-invalid')).not.toHaveRole('link') await expect.element(getByTestId('link-invalid')).toHaveRole('generic') ``` ::: warning 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`. Also note that unlike `testing-library`, Vitest ignores all custom roles except the first valid one, following Playwright's behaviour: ```jsx
await expect.element(getByTestId('switch')).toHaveRole('switch') // ✅ await expect.element(getByTestId('switch')).toHaveRole('alert') // ❌ ``` ::: ## toHaveSelection ```ts function toHaveSelection(selection?: string): Promise ``` This allows to assert that an element has a [text selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection). This is useful to check if text or part of the text is selected within an element. The element can be either an input of type text, a textarea, or any other element that contains text, such as a paragraph, span, div etc. ::: warning The expected selection is a string, it does not allow to check for selection range indices. ::: ```html

prev

text selected text

next

``` ```ts getByTestId('text').element().setSelectionRange(5, 13) await expect.element(getByTestId('text')).toHaveSelection('selected') getByTestId('textarea').element().setSelectionRange(0, 5) await expect.element('textarea').toHaveSelection('text ') const selection = document.getSelection() const range = document.createRange() selection.removeAllRanges() selection.empty() selection.addRange(range) // selection of child applies to the parent as well range.selectNodeContents(getByTestId('child').element()) await expect.element(getByTestId('child')).toHaveSelection('selected') await expect.element(getByTestId('parent')).toHaveSelection('selected') // selection that applies from prev all, parent text before child, and part child. range.setStart(getByTestId('prev').element(), 0) range.setEnd(getByTestId('child').element().childNodes[0], 3) await expect.element(queryByTestId('prev')).toHaveSelection('prev') await expect.element(queryByTestId('child')).toHaveSelection('sel') await expect.element(queryByTestId('parent')).toHaveSelection('text sel') await expect.element(queryByTestId('next')).not.toHaveSelection() // selection that applies from part child, parent text after child and part next. range.setStart(getByTestId('child').element().childNodes[0], 3) range.setEnd(getByTestId('next').element().childNodes[0], 2) await expect.element(queryByTestId('child')).toHaveSelection('ected') await expect.element(queryByTestId('parent')).toHaveSelection('ected text') await expect.element(queryByTestId('prev')).not.toHaveSelection() await expect.element(queryByTestId('next')).toHaveSelection('ne') ``` ## toMatchScreenshot experimental ```ts function toMatchScreenshot( options?: ScreenshotMatcherOptions, ): Promise function toMatchScreenshot( name?: string, options?: ScreenshotMatcherOptions, ): Promise ``` ::: tip The `toMatchScreenshot` assertion can be configured globally in your [Vitest config](/guide/browser/config#browser-expect-tomatchscreenshot). ::: This assertion allows you to perform visual regression testing by comparing screenshots of elements or pages against stored reference images. When differences are detected beyond the configured threshold, the test fails. To help identify the changes, the assertion generates: - The actual screenshot captured during the test - The expected reference screenshot - A diff image highlighting the differences (when possible) ::: warning Screenshots Stability The assertion automatically retries taking screenshots until two consecutive captures yield the same result. This helps reduce flakiness caused by animations, loading states, or other dynamic content. You can control this behavior with the `timeout` option. However, browser rendering can vary across: - Different browsers and browser versions - Operating systems (Windows, macOS, Linux) - Screen resolutions and pixel densities - GPU drivers and hardware acceleration - Font rendering and system fonts It is recommended to read the [Visual Regression Testing guide](/guide/browser/visual-regression-testing) to implement this testing strategy efficiently. ::: ::: tip When a screenshot comparison fails due to **intentional changes**, you can update the reference screenshot by pressing the `u` key in watch mode, or by running tests with the `-u` or `--update` flags. ::: ```html ``` ```ts // basic usage, auto-generates screenshot name await expect.element(getByTestId('button')).toMatchScreenshot() // with custom name await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button') // with options await expect.element(getByTestId('button')).toMatchScreenshot({ comparatorName: 'pixelmatch', comparatorOptions: { allowedMismatchedPixelRatio: 0.01, }, }) // with both name and options await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', { comparatorName: 'pixelmatch', comparatorOptions: { allowedMismatchedPixelRatio: 0.01, }, }) ``` ### Options - `comparatorName: "pixelmatch" = "pixelmatch"` The name of the algorithm/library used for comparing images. Currently, [`"pixelmatch"`](https://github.com/mapbox/pixelmatch) is the only supported comparator. - `comparatorOptions: object` These options allow changing the behavior of the comparator. What properties can be set depends on the chosen comparator algorithm. Vitest has set default values out of the box, but they can be overridden. - [`"pixelmatch"` options](#pixelmatch-comparator-options) ::: warning **Always explicitly set `comparatorName` to get proper type inference for `comparatorOptions`**. Without it, TypeScript won't know which options are valid: ```ts // ❌ TypeScript can't infer the correct options await expect.element(button).toMatchScreenshot({ comparatorOptions: { // might error when new comparators are added allowedMismatchedPixelRatio: 0.01, }, }) // ✅ TypeScript knows these are pixelmatch options await expect.element(button).toMatchScreenshot({ comparatorName: 'pixelmatch', comparatorOptions: { allowedMismatchedPixelRatio: 0.01, }, }) ``` ::: - `screenshotOptions: object` The same options allowed by [`locator.screenshot()`](/guide/browser/locators.html#screenshot), except for: - `'base64'` - `'path'` - `'save'` - `'type'` - `timeout: number = 5_000` Time to wait until a stable screenshot is found. Setting this value to `0` disables the timeout, but if a stable screenshot can't be determined the process will not end. #### `"pixelmatch"` comparator options The following options are available when using the `"pixelmatch"` comparator: - `allowedMismatchedPixelRatio: number | undefined = undefined` The maximum allowed ratio of differing pixels between the captured screenshot and the reference image. Must be a value between `0` and `1`. For example, `allowedMismatchedPixelRatio: 0.02` means the test will pass if up to 2% of pixels differ, but fail if more than 2% differ. - `allowedMismatchedPixels: number | undefined = undefined` The maximum number of pixels that are allowed to differ between the captured screenshot and the stored reference image. If set to `undefined`, any non-zero difference will cause the test to fail. For example, `allowedMismatchedPixels: 10` means the test will pass if 10 or fewer pixels differ, but fail if 11 or more differ. - `threshold: number = 0.1` Acceptable perceived color difference between the same pixel in two images. Value ranges from `0` (strict) to `1` (very lenient). Lower values mean small differences will be detected. The comparison uses the [YIQ color space](https://en.wikipedia.org/wiki/YIQ). - `includeAA: boolean = false` If `true`, disables detection and ignoring of anti-aliased pixels. - `alpha: number = 0.1` Blending level of unchanged pixels in the diff image. Ranges from `0` (white) to `1` (original brightness). - `aaColor: [r: number, g: number, b: number] = [255, 255, 0]` Color used for anti-aliased pixels in the diff image. - `diffColor: [r: number, g: number, b: number] = [255, 0, 0]` Color used for differing pixels in the diff image. - `diffColorAlt: [r: number, g: number, b: number] | undefined = undefined` Optional alternative color for dark-on-light differences, to help show what's added vs. removed. If not set, `diffColor` is used for all differences. - `diffMask: boolean = false` If `true`, shows only the diff as a mask on a transparent background, instead of overlaying it on the original image. Anti-aliased pixels won't be shown (if detected). ::: warning When both `allowedMismatchedPixels` and `allowedMismatchedPixelRatio` are set, the more restrictive value is used. For example, if you allow 100 pixels or 2% ratio, and your image has 10,000 pixels, the effective limit would be 100 pixels instead of 200. :::