---
url: /guide/advanced.md
---
# Getting Started advanced {#getting-started}
::: warning
This guide lists advanced APIs to run tests via a Node.js script. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
:::
You can import any method from the `vitest/node` entry-point.
## startVitest
```ts
function startVitest(
mode: VitestRunMode,
cliFilters: string[] = [],
options: CliOptions = {},
viteOverrides?: ViteUserConfig,
vitestOptions?: VitestOptions,
): Promise
```
You can start running Vitest tests using its Node API:
```js
import { startVitest } from 'vitest/node'
const vitest = await startVitest('test')
await vitest.close()
```
`startVitest` function returns [`Vitest`](/api/advanced/vitest) instance if tests can be started.
If watch mode is not enabled, Vitest will call `close` method automatically.
If watch mode is enabled and the terminal supports TTY, Vitest will register console shortcuts.
You can pass down a list of filters as a second argument. Vitest will run only tests that contain at least one of the passed-down strings in their file path.
Additionally, you can use the third argument to pass in CLI arguments, which will override any test config options. Alternatively, you can pass in the complete Vite config as the fourth argument, which will take precedence over any other user-defined options.
After running the tests, you can get the results from the [`state.getTestModules`](/api/advanced/test-module) API:
```ts
import type { TestModule } from 'vitest/node'
const vitest = await startVitest('test')
console.log(vitest.state.getTestModules()) // [TestModule]
```
::: tip
The ["Running Tests"](/guide/advanced/tests#startvitest) guide has a usage example.
:::
## createVitest
```ts
function createVitest(
mode: VitestRunMode,
options: CliOptions,
viteOverrides: ViteUserConfig = {},
vitestOptions: VitestOptions = {},
): Promise
```
You can create Vitest instance by using `createVitest` function. It returns the same [`Vitest`](/api/advanced/vitest) instance as `startVitest`, but it doesn't start tests and doesn't validate installed packages.
```js
import { createVitest } from 'vitest/node'
const vitest = await createVitest('test', {
watch: false,
})
```
::: tip
The ["Running Tests"](/guide/advanced/tests#createvitest) guide has a usage example.
:::
## resolveConfig
```ts
function resolveConfig(
options: UserConfig = {},
viteOverrides: ViteUserConfig = {},
): Promise<{
vitestConfig: ResolvedConfig
viteConfig: ResolvedViteConfig
}>
```
This method resolves the config with custom parameters. If no parameters are given, the `root` will be `process.cwd()`.
```ts
import { resolveConfig } from 'vitest/node'
// vitestConfig only has resolved "test" properties
const { vitestConfig, viteConfig } = await resolveConfig({
mode: 'custom',
configFile: false,
resolve: {
conditions: ['custom']
},
test: {
setupFiles: ['/my-setup-file.js'],
pool: 'threads',
},
})
```
::: info
Due to how Vite's `createServer` works, Vitest has to resolve the config during the plugin's `configResolve` hook. Therefore, this method is not actually used internally and is exposed exclusively as a public API.
If you pass down the config to the `startVitest` or `createVitest` APIs, Vitest will still resolve the config again.
:::
::: warning
The `resolveConfig` doesn't resolve `projects`. To resolve projects configs, Vitest needs an established Vite server.
Also note that `viteConfig.test` will not be fully resolved. If you need Vitest config, use `vitestConfig` instead.
:::
## parseCLI
```ts
function parseCLI(argv: string | string[], config: CliParseOptions = {}): {
filter: string[]
options: CliOptions
}
```
You can use this method to parse CLI arguments. It accepts a string (where arguments are split by a single space) or a strings array of CLI arguments in the same format that Vitest CLI uses. It returns a filter and `options` that you can later pass down to `createVitest` or `startVitest` methods.
```ts
import { parseCLI } from 'vitest/node'
const result = parseCLI('vitest ./files.ts --coverage --browser=chrome')
result.options
// {
// coverage: { enabled: true },
// browser: { name: 'chrome', enabled: true }
// }
result.filter
// ['./files.ts']
```
---
---
url: /config/alias.md
---
# alias
* **Type:** `Record | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`
Define custom aliases when running inside tests. They will be merged with aliases from `resolve.alias`.
::: warning
Vitest uses Vite SSR primitives to run tests which has [certain pitfalls](https://vitejs.dev/guide/ssr.html#ssr-externals).
1. Aliases affect only modules imported directly with an `import` keyword by an [inlined](/config/server#server-deps-inline) module (all source code is inlined by default).
2. Vitest does not support aliasing `require` calls.
3. If you are aliasing an external dependency (e.g., `react` -> `preact`), you may want to alias the actual `node_modules` packages instead to make it work for externalized dependencies. Both [Yarn](https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias) and [pnpm](https://pnpm.io/aliases/) support aliasing via the `npm:` prefix.
:::
---
---
url: /config/allowonly.md
---
# allowOnly
* **Type**: `boolean`
* **Default**: `!process.env.CI`
* **CLI:** `--allowOnly`, `--allowOnly=false`
By default, Vitest does not permit tests marked with the [`only`](/api/test#test-only) flag in Continuous Integration (CI) environments. Conversely, in local development environments, Vitest allows these tests to run.
::: info
Vitest uses [`std-env`](https://npmx.dev/package/std-env) package to detect the environment.
:::
You can customize this behavior by explicitly setting the `allowOnly` option to either `true` or `false`.
::: code-group
```js [vitest.config.js]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
allowOnly: true,
},
})
```
```bash [CLI]
vitest --allowOnly
```
:::
When enabled, Vitest will not fail the test suite if tests marked with [`only`](/api/test#test-only) are detected, including in CI environments.
When disabled, Vitest will fail the test suite if tests marked with [`only`](/api/test#test-only) are detected, including in local development environments.
---
---
url: /config/api.md
---
# api
* **Type:** `boolean | number | object`
* **Default:** `false`
* **CLI:** `--api`, `--api.port`, `--api.host`, `--api.strictPort`
Listen to port and serve API for [the UI](/guide/ui) or [browser server](/guide/browser/). When set to `true`, the default port is `51204`.
## api.allowWrite 4.1.0 {#api-allowwrite}
* **Type:** `boolean`
* **Default:** `true` if not exposed to the network, `false` otherwise
Vitest server can save test files or snapshot files via the API. This allows anyone who can connect to the API the ability to run any arbitrary code on your machine.
::: danger SECURITY ADVICE
Vitest does not expose the API to the internet by default and only listens on `localhost`. However if `host` is manually exposed to the network, anyone who connects to it can run arbitrary code on your machine, unless `api.allowWrite` and `api.allowExec` are set to `false`.
If the host is set to anything other than `localhost` or `127.0.0.1`, Vitest will set `api.allowWrite` and `api.allowExec` to `false` by default. This means that any write operations (like changing the code in the UI) will not work. However, if you understand the security implications, you can override them.
:::
## api.allowExec 4.1.0 {#api-allowexec}
* **Type:** `boolean`
* **Default:** `true` if not exposed to the network, `false` otherwise
Allows running any test file via the API. See the security advice in [`api.allowWrite`](#api-allowwrite).
---
---
url: /guide/browser/aria-snapshots.md
---
# ARIA Snapshots experimental 4.1.4
ARIA snapshots let you test the accessibility structure of your pages. Instead of asserting against raw HTML or visual output, you assert against the accessibility tree — the same structure that screen readers and other assistive technologies use.
Given this HTML:
```html
Home
About
```
You can assert its accessibility tree:
```ts
await expect.element(page.getByRole('navigation')).toMatchAriaInlineSnapshot(`
- navigation "Main":
- link "Home":
- /url: /
- link "About":
- /url: /about
`)
```
This catches accessibility regressions: missing labels, broken roles, incorrect heading levels, and more — things that DOM snapshots would miss. Even if the underlying HTML structure changes, the assertion would not fail as long as content matches semantically.
## Snapshot Workflow
ARIA snapshots use the same Vitest snapshot workflow as other snapshot assertions. File snapshots, inline snapshots, `--update` / `-u`, watch mode updates, and CI snapshot behavior all work the same way.
See the main [Snapshot guide](/guide/snapshot) for the general snapshot workflow, update behavior, and review guidelines.
## Basic Usage
Given a page with this HTML:
```html
```
### File Snapshots
Use `toMatchAriaSnapshot()` to store the snapshot in a `.snap` file alongside your test:
```ts [basic.test.ts]
import { expect, test } from 'vitest'
test('login form', async () => {
await expect.element(page.getByRole('form')).toMatchAriaSnapshot()
})
```
On first run, Vitest generates a snapshot file entry:
```js [__snapshots__/basic.test.ts.snap]
// Vitest Snapshot ...
exports[`login form 1`] = `
- form "Log In":
- textbox "Email"
- textbox "Password"
- button "Submit"
`
```
### Inline Snapshots
Use `toMatchAriaInlineSnapshot()` to store the snapshot directly in the test file:
```ts
import { expect, test } from 'vitest'
test('login form', async () => {
await expect.element(page.getByRole('form')).toMatchAriaInlineSnapshot(`
- form "Log In":
- textbox "Email"
- textbox "Password"
- button "Submit"
`)
})
```
## Browser Mode Retry Behavior
In [Browser Mode](/guide/browser/), `expect.element()` polls the DOM and waits for the accessibility tree to **stabilize** before evaluating the result. On each poll, the matcher re-queries the element and re-captures the accessibility tree. The snapshot is considered stable when two consecutive polls produce the same output.
```ts
await expect.element(page.getByRole('form')).toMatchAriaInlineSnapshot(`
- form "Log In":
- textbox "Email"
- textbox "Password"
- button "Submit"
`)
```
On first run or with `--update`, the stable result is written as the new snapshot.
When an existing snapshot is present, the matcher also checks whether the stable result matches. If it does not, polling resets and continues — giving the DOM time to reach the expected state. This handles cases like animations, async rendering, or delayed state updates where the tree may briefly stabilize in an intermediate state before settling into its final form.
## Preserving Hand-Edited Patterns
When you hand-edit a snapshot to use regex patterns, those patterns survive `--update`. Only the literal parts that changed are overwritten. This lets you write flexible assertions that don't break when content changes.
### Example
**Step 1.** Your shopping cart page renders this HTML:
```html
Your Cart
Wireless Headphones — $79.99
Checkout
```
You run your test for the first time with `--update`. Vitest generates the snapshot:
```yaml
- heading "Your Cart" [level=1]
- list "Cart Items":
- listitem: Wireless Headphones — $79.99
- button "Checkout"
```
**Step 2.** The item names and prices are seeded test data that may change. You hand-edit those lines to regex patterns, but keep the stable structure as literals:
```yaml
- heading "Your Cart" [level=1]
- list "Cart Items":
- listitem: /.+ — \$\d+\.\d+/
- button "Checkout"
```
**Step 3.** Later, a developer renames the button from "Checkout" to "Place Order". Running `--update` updates that literal but preserves your regex patterns:
```yaml
- heading "Your Cart" [level=1]
- list "Cart Items":
- listitem: /.+ — \$\d+\.\d+/
- button "Place Order" 👈 New snapshot updated with new string
```
The regex patterns you wrote in step 2 are preserved because they still match the actual content. Only the mismatched literal "Checkout" was updated to "Place Order".
## Snapshot Format
ARIA snapshots use a YAML-like syntax. Each line represents a node in the accessibility tree.
::: info
ARIA snapshot templates use a **subset of YAML** syntax. Only the features needed for accessibility trees are supported: scalar values, nested mappings via indentation, and sequences (`- item`). Advanced YAML features like anchors, tags, flow collections, and multi-line scalars are not supported.
Captured text is also whitespace-normalized before it is rendered into the snapshot. Newlines, ` ` line breaks, tabs, and repeated whitespace collapse to single spaces, so multi-line DOM text is emitted as a single-line snapshot value.
:::
Each accessible element in the tree is represented as a YAML node:
```yaml
- role "name" [attribute=value]
```
* `role`: The ARIA role of the element, such as `heading`, `list`, `listitem`, or `button`
* `"name"`: The [accessible name](https://w3c.github.io/accname/), when present. Quoted strings match exact values, and `/patterns/` match regular expressions
* `[attribute=value]`: Accessibility states and properties such as `checked`, `disabled`, `expanded`, `level`, `pressed`, or `selected`
These values come from ARIA attributes and the browser's accessibility tree, including semantics inferred from native HTML elements.
Because ARIA snapshots reflect the browser's accessibility tree, content excluded from that tree, such as `aria-hidden="true"` or `display: none`, does not appear in the snapshot.
### Roles and Accessible Names
For example:
```html
Submit
Welcome
Home
```
```yaml
- button "Submit"
- heading "Welcome" [level=1]
- link "Home"
- textbox "Email"
```
The role usually comes from the element's native semantics, though it can also be defined with ARIA. The accessible name is computed from text content, associated labels, `aria-label`, `aria-labelledby`, and related naming rules.
For a closer look at how names are computed, see [Accessible Name and Description Computation](https://w3c.github.io/accname/).
Some content appears in the snapshot as a text node instead of a role-based element:
```html
Hello world
```
```yaml
- text: Hello world
```
Text values are always serialized on a single line after whitespace normalization. For example:
```html
Line 1
Line 2 Line 3
Line 4
```
```yaml
- paragraph: Line 1 Line 2 Line 3 Line 4
```
### Children
Child elements appear nested under their parent:
```html
```
```yaml
- list:
- listitem: First
- listitem: Second
- listitem: Third
```
If the parent has an accessible name, the snapshot includes it before the nested children:
```html
Home
About
```
```yaml
- navigation "Main":
- link "Home"
- link "About"
```
If an element only contains a single text child and has no other properties, the text is rendered inline:
```html
Hello world
```
```yaml
- paragraph: Hello world
```
### Attributes
ARIA states and properties appear in brackets:
| HTML | Snapshot |
| ---------------------------------------------------------------------- | ----------------------------------------- |
| ` ` | `- checkbox "Agree" [checked]` |
| ` ` | `- checkbox "Select all" [checked=mixed]` |
| `Submit ` | `- button "Submit" [disabled]` |
| `Menu ` | `- button "Menu" [expanded]` |
| `Title ` | `- heading "Title" [level=2]` |
| `Bold ` | `- button "Bold" [pressed]` |
| `Bold ` | `- button "Bold" [pressed=mixed]` |
| `English ` | `- option "English" [selected]` |
Attributes only appear when they are active. A button that is not disabled simply has no `[disabled]` attribute — there is no `[disabled=false]`.
### Pseudo-Attributes
Some DOM properties that aren't part of ARIA but are useful for testing are exposed with a `/` prefix:
#### `/url:`
Links include their URL:
```html
Home
```
```yaml
- link "Home":
- /url: /
```
#### `/placeholder:`
Textboxes can include their placeholder text:
```html
```
```yaml
- textbox "Email":
- /placeholder: user@example.com
```
::: tip When does `/placeholder:` appear?
The `/placeholder:` pseudo-attribute only appears when the placeholder text is **different from the accessible name**. When an input has a placeholder but no `aria-label` or associated ``, the browser uses the placeholder as the accessible name. In that case, the placeholder information is already in the name and is not duplicated.
* When placeholder is the accessible name:
```html
```
```yaml
- textbox "Search"
```
* When placeholder differs from the accessible name:
```html
```
```yaml
- textbox "Search products":
- /placeholder: Search
```
:::
## Matching
### Regular Expressions
Use regex patterns to match names flexibly:
```html
Welcome, Alice
Profile
```
```yaml
- heading /Welcome, .*/
- link "Profile":
- /url: /https:\/\/example\.com\/.*/
```
Regex also works in pseudo-attribute values:
```html
```
```yaml
- textbox "Search":
- /placeholder: /Type .*/
```
::: warning Escaping backslashes in regex patterns
Snapshots are stored as JavaScript strings — in backtick-delimited template literals for inline snapshots and in `.snap` files. Because of this, backslashes need to be **doubled** when you hand-edit a snapshot to add a regex pattern.
For example, to match one or more digits with `\d+`:
```ts
// ✅ Correct — double backslash
await expect.element(button).toMatchAriaInlineSnapshot(`
- button: /item \\d+/
`)
// ❌ Wrong — single backslash is consumed by JS, regex sees "d+" instead of "\d+"
await expect.element(button).toMatchAriaInlineSnapshot(`
- button: /item \d+/
`)
```
This applies to both inline snapshots and `.snap` files. When Vitest **auto-generates** or **updates** a snapshot, escaping is handled automatically — you only need to worry about this when hand-editing regex patterns.
:::
### Child Matching
The `/children` directive controls how a node's children are compared against the template. There are three modes:
#### Partial Matching (default)
By default (no `/children` directive), templates use **contain** semantics — extra children in the actual tree are allowed as long as all template children appear as an ordered subsequence. This is the same as `/children: contain`.
```html
Welcome
Some intro text
Get Started
```
```ts
// This passes — the template children are a subset of the actual children
await expect.element(page.getByRole('main')).toMatchAriaInlineSnapshot(`
- main:
- heading "Welcome" [level=1]
`)
```
This is useful for focused, resilient tests that don't break when unrelated content is added.
#### Exact Matching (`/children: equal`)
Requires that the node's immediate children match the template exactly — same count, same order. No extra children are allowed at this level.
```html
Feature A
Feature B
Feature C
```
```ts
// This FAILS — the list has 3 items but the template only lists 2
await expect.element(page.getByRole('list')).toMatchAriaInlineSnapshot(`
- list "Features":
- /children: equal
- listitem: Feature A
- listitem: Feature B
`)
```
```ts
// This PASSES — all 3 items are listed
await expect.element(page.getByRole('list')).toMatchAriaInlineSnapshot(`
- list "Features":
- /children: equal
- listitem: Feature A
- listitem: Feature B
- listitem: Feature C
`)
```
The strict matching only applies at the level where `/children` is placed. Descendants of each `listitem` still use the default contain semantics.
#### Deep Exact Matching (`/children: deep-equal`)
Like `equal`, but the strict matching **propagates to all descendants**. Every level of nesting must match exactly — same count, same order, no extra nodes at any depth.
```ts
await expect.element(page.getByRole('navigation')).toMatchAriaInlineSnapshot(`
- navigation "Main":
- /children: deep-equal
- link "Home":
- /url: /
- link "About":
- /url: /about
`)
```
With `deep-equal`, every child of each `link` must also match exactly. If a link had an extra child node not listed in the template, the assertion would fail.
#### Comparison
| Mode | Directive | Behavior |
| --- | --- | --- |
| Partial | *(default)* or `/children: contain` | Template children are an ordered subsequence — extra actual children are ignored |
| Exact | `/children: equal` | Immediate children must match exactly; descendants still use partial matching |
| Deep exact | `/children: deep-equal` | All children at every depth must match exactly |
---
---
url: /api/assert.md
---
# assert
Vitest reexports the `assert` method from [`chai`](https://www.chaijs.com/api/assert/) for verifying invariants.
## assert
* **Type:** `(expression: any, message?: string) => asserts expression`
Assert that the given `expression` is truthy, otherwise the assertion fails.
```ts
import { assert, test } from 'vitest'
test('assert', () => {
assert('foo' !== 'bar', 'foo should not be equal to bar')
})
```
## fail
* **Type:**
* `(message?: string) => never`
* `(actual: T, expected: T, message?: string, operator?: string) => never`
Force an assertion failure.
```ts
import { assert, test } from 'vitest'
test('assert.fail', () => {
assert.fail('error message on failure')
assert.fail('foo', 'bar', 'foo is not bar', '===')
})
```
## isOk
* **Type:** `(value: T, message?: string) => asserts value`
* **Alias** `ok`
Assert that the given `value` is truthy.
```ts
import { assert, test } from 'vitest'
test('assert.isOk', () => {
assert.isOk('foo', 'every truthy is ok')
assert.isOk(false, 'this will fail since false is not truthy')
})
```
## isNotOk
* **Type:** `(value: T, message?: string) => void`
* **Alias** `notOk`
Assert that the given `value` is falsy.
```ts
import { assert, test } from 'vitest'
test('assert.isNotOk', () => {
assert.isNotOk('foo', 'this will fail, every truthy is not ok')
assert.isNotOk(false, 'this will pass since false is falsy')
})
```
## equal
* **Type:** `(actual: T, expected: T, message?: string) => void`
Asserts non-strict equality (==) of `actual` and `expected`.
```ts
import { assert, test } from 'vitest'
test('assert.equal', () => {
assert.equal(Math.sqrt(4), '2')
})
```
## notEqual
* **Type:** `(actual: T, expected: T, message?: string) => void`
Asserts non-strict inequality (!=) of `actual` and `expected`.
```ts
import { assert, test } from 'vitest'
test('assert.equal', () => {
assert.notEqual(Math.sqrt(4), 3)
})
```
## strictEqual
* **Type:** `(actual: T, expected: T, message?: string) => void`
Asserts strict equality (===) of `actual` and `expected`.
```ts
import { assert, test } from 'vitest'
test('assert.strictEqual', () => {
assert.strictEqual(Math.sqrt(4), 2)
})
```
## deepEqual
* **Type:** `(actual: T, expected: T, message?: string) => void`
Asserts that `actual` is deeply equal to `expected`.
```ts
import { assert, test } from 'vitest'
test('assert.deepEqual', () => {
assert.deepEqual({ color: 'green' }, { color: 'green' })
})
```
## notDeepEqual
* **Type:** `(actual: T, expected: T, message?: string) => void`
Assert that `actual` is not deeply equal to `expected`.
```ts
import { assert, test } from 'vitest'
test('assert.notDeepEqual', () => {
assert.notDeepEqual({ color: 'green' }, { color: 'red' })
})
```
## isAbove
* **Type:** `(valueToCheck: number, valueToBeAbove: number, message?: string) => void`
Assert that `valueToCheck` is strictly greater than (>) `valueToBeAbove`.
```ts
import { assert, test } from 'vitest'
test('assert.isAbove', () => {
assert.isAbove(5, 2, '5 is strictly greater than 2')
})
```
## isAtLeast
* **Type:** `(valueToCheck: number, valueToBeAtLeast: number, message?: string) => void`
Assert that `valueToCheck` is greater than or equal to (>=) `valueToBeAtLeast`.
```ts
import { assert, test } from 'vitest'
test('assert.isAtLeast', () => {
assert.isAtLeast(5, 2, '5 is greater or equal to 2')
assert.isAtLeast(3, 3, '3 is greater or equal to 3')
})
```
## isBelow
* **Type:** `(valueToCheck: number, valueToBeBelow: number, message?: string) => void`
Asserts `valueToCheck` is strictly less than (<) `valueToBeBelow`.
```ts
import { assert, test } from 'vitest'
test('assert.isBelow', () => {
assert.isBelow(3, 6, '3 is strictly less than 6')
})
```
## isAtMost
* **Type:** `(valueToCheck: number, valueToBeAtMost: number, message?: string) => void`
Asserts `valueToCheck` is less than or equal to (<=) `valueToBeAtMost`.
```ts
import { assert, test } from 'vitest'
test('assert.isAtMost', () => {
assert.isAtMost(3, 6, '3 is less than or equal to 6')
assert.isAtMost(4, 4, '4 is less than or equal to 4')
})
```
## isTrue
* **Type:** `(value: T, message?: string) => asserts value is true`
Asserts that `value` is true.
```ts
import { assert, test } from 'vitest'
const testPassed = true
test('assert.isTrue', () => {
assert.isTrue(testPassed)
})
```
## isNotTrue
* **Type:** `(value: T, message?: string) => asserts value is Exclude`
Asserts that `value` is not true.
```ts
import { assert, test } from 'vitest'
const testPassed = 'ok'
test('assert.isNotTrue', () => {
assert.isNotTrue(testPassed)
})
```
## isFalse
* **Type:** `(value: T, message?: string) => asserts value is false`
Asserts that `value` is false.
```ts
import { assert, test } from 'vitest'
const testPassed = false
test('assert.isFalse', () => {
assert.isFalse(testPassed)
})
```
## isNotFalse
* **Type:** `(value: T, message?: string) => asserts value is Exclude`
Asserts that `value` is not false.
```ts
import { assert, test } from 'vitest'
const testPassed = 'no'
test('assert.isNotFalse', () => {
assert.isNotFalse(testPassed)
})
```
## isNull
* **Type:** `(value: T, message?: string) => asserts value is null`
Asserts that `value` is null.
```ts
import { assert, test } from 'vitest'
const error = null
test('assert.isNull', () => {
assert.isNull(error, 'error is null')
})
```
## isNotNull
* **Type:** `(value: T, message?: string) => asserts value is Exclude`
Asserts that `value` is not null.
```ts
import { assert, test } from 'vitest'
const error = { message: 'error was occurred' }
test('assert.isNotNull', () => {
assert.isNotNull(error, 'error is not null but object')
})
```
## isNaN
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is NaN.
```ts
import { assert, test } from 'vitest'
const calculation = 1 * 'vitest'
test('assert.isNaN', () => {
assert.isNaN(calculation, '1 * "vitest" is NaN')
})
```
## isNotNaN
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not NaN.
```ts
import { assert, test } from 'vitest'
const calculation = 1 * 2
test('assert.isNotNaN', () => {
assert.isNotNaN(calculation, '1 * 2 is Not NaN but 2')
})
```
## exists
* **Type:** `(value: T, message?: string) => asserts value is NonNullable`
Asserts that `value` is neither null nor undefined.
```ts
import { assert, test } from 'vitest'
const name = 'foo'
test('assert.exists', () => {
assert.exists(name, 'foo is neither null nor undefined')
})
```
## notExists
* **Type:** `(value: T, message?: string) => asserts value is null | undefined`
Asserts that `value` is either null nor undefined.
```ts
import { assert, test } from 'vitest'
const foo = null
const bar = undefined
test('assert.notExists', () => {
assert.notExists(foo, 'foo is null so not exist')
assert.notExists(bar, 'bar is undefined so not exist')
})
```
## isUndefined
* **Type:** `(value: T, message?: string) => asserts value is undefined`
Asserts that `value` is undefined.
```ts
import { assert, test } from 'vitest'
const name = undefined
test('assert.isUndefined', () => {
assert.isUndefined(name, 'name is undefined')
})
```
## isDefined
* **Type:** `(value: T, message?: string) => asserts value is Exclude`
Asserts that `value` is not undefined.
```ts
import { assert, test } from 'vitest'
const name = 'foo'
test('assert.isDefined', () => {
assert.isDefined(name, 'name is not undefined')
})
```
## isFunction
* **Type:** `(value: T, message?: string) => void`
* **Alias:** `isCallable`
Asserts that `value` is a function.
```ts
import { assert, test } from 'vitest'
function name() { return 'foo' };
test('assert.isFunction', () => {
assert.isFunction(name, 'name is function')
})
```
## isNotFunction
* **Type:** `(value: T, message?: string) => void`
* **Alias:** `isNotCallable`
Asserts that `value` is not a function.
```ts
import { assert, test } from 'vitest'
const name = 'foo'
test('assert.isNotFunction', () => {
assert.isNotFunction(name, 'name is not function but string')
})
```
## isObject
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is an object of type Object (as revealed by Object.prototype.toString). The assertion does not match subclassed objects.
```ts
import { assert, test } from 'vitest'
const someThing = { color: 'red', shape: 'circle' }
test('assert.isObject', () => {
assert.isObject(someThing, 'someThing is object')
})
```
## isNotObject
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not an object of type Object (as revealed by Object.prototype.toString). The assertion does not match subclassed objects.
```ts
import { assert, test } from 'vitest'
const someThing = 'redCircle'
test('assert.isNotObject', () => {
assert.isNotObject(someThing, 'someThing is not object but string')
})
```
## isArray
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is an array.
```ts
import { assert, test } from 'vitest'
const color = ['red', 'green', 'yellow']
test('assert.isArray', () => {
assert.isArray(color, 'color is array')
})
```
## isNotArray
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not an array.
```ts
import { assert, test } from 'vitest'
const color = 'red'
test('assert.isNotArray', () => {
assert.isNotArray(color, 'color is not array but string')
})
```
## isString
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is a string.
```ts
import { assert, test } from 'vitest'
const color = 'red'
test('assert.isString', () => {
assert.isString(color, 'color is string')
})
```
## isNotString
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not a string.
```ts
import { assert, test } from 'vitest'
const color = ['red', 'green', 'yellow']
test('assert.isNotString', () => {
assert.isNotString(color, 'color is not string but array')
})
```
## isNumber
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is a number.
```ts
import { assert, test } from 'vitest'
const colors = 3
test('assert.isNumber', () => {
assert.isNumber(colors, 'colors is number')
})
```
## isNotNumber
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not a number.
```ts
import { assert, test } from 'vitest'
const colors = '3 colors'
test('assert.isNotNumber', () => {
assert.isNotNumber(colors, 'colors is not number but strings')
})
```
## isFinite
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is a finite number (not NaN, Infinity).
```ts
import { assert, test } from 'vitest'
const colors = 3
test('assert.isFinite', () => {
assert.isFinite(colors, 'colors is number not NaN or Infinity')
})
```
## isBoolean
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is a boolean.
```ts
import { assert, test } from 'vitest'
const isReady = true
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady is a boolean')
})
```
## isNotBoolean
* **Type:** `(value: T, message?: string) => void`
Asserts that `value` is not a boolean.
```ts
import { assert, test } from 'vitest'
const isReady = 'sure'
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady is not a boolean but string')
})
```
## typeOf
* **Type:** `(value: T, name: string, message?: string) => void`
Asserts that `value`’s type is `name`, as determined by Object.prototype.toString.
```ts
import { assert, test } from 'vitest'
test('assert.typeOf', () => {
assert.typeOf({ color: 'red' }, 'object', 'we have an object')
assert.typeOf(['red', 'green'], 'array', 'we have an array')
assert.typeOf('red', 'string', 'we have a string')
assert.typeOf(/red/, 'regexp', 'we have a regular expression')
assert.typeOf(null, 'null', 'we have a null')
assert.typeOf(undefined, 'undefined', 'we have an undefined')
})
```
## notTypeOf
* **Type:** `(value: T, name: string, message?: string) => void`
Asserts that `value`’s type is not `name`, as determined by Object.prototype.toString.
```ts
import { assert, test } from 'vitest'
test('assert.notTypeOf', () => {
assert.notTypeOf('red', 'number', '"red" is not a number')
})
```
## instanceOf
* **Type:** `(value: T, constructor: Function, message?: string) => asserts value is T`
Asserts that `value` is an instance of `constructor`.
```ts
import { assert, test } from 'vitest'
function Person(name) { this.name = name }
const foo = new Person('foo')
class Tea {
constructor(name) {
this.name = name
}
}
const coffee = new Tea('coffee')
test('assert.instanceOf', () => {
assert.instanceOf(foo, Person, 'foo is an instance of Person')
assert.instanceOf(coffee, Tea, 'coffee is an instance of Tea')
})
```
## notInstanceOf
* **Type:** `(value: T, constructor: Function, message?: string) => asserts value is Exclude`
Asserts that `value` is not an instance of `constructor`.
```ts
import { assert, test } from 'vitest'
function Person(name) { this.name = name }
const foo = new Person('foo')
class Tea {
constructor(name) {
this.name = name
}
}
const coffee = new Tea('coffee')
test('assert.instanceOf', () => {
assert.instanceOf(foo, Tea, 'foo is not an instance of Tea')
})
```
## include
* **Type:**
* `(haystack: string, needle: string, message?: string) => void`
* `(haystack: readonly T[] | ReadonlySet | ReadonlyMap, needle: T, message?: string) => void`
* `(haystack: WeakSet, needle: T, message?: string) => void`
* `(haystack: T, needle: Partial, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a value in an array, a substring in a string, or a subset of properties in an object.
```ts
import { assert, test } from 'vitest'
test('assert.include', () => {
assert.include([1, 2, 3], 2, 'array contains value')
assert.include('foobar', 'foo', 'string contains substring')
assert.include({ foo: 'bar', hello: 'universe' }, { foo: 'bar' }, 'object contains property')
})
```
## notInclude
* **Type:**
* `(haystack: string, needle: string, message?: string) => void`
* `(haystack: readonly T[] | ReadonlySet | ReadonlyMap, needle: T, message?: string) => void`
* `(haystack: WeakSet, needle: T, message?: string) => void`
* `(haystack: T, needle: Partial, message?: string) => void`
Asserts that `haystack` does not include `needle`. It can be used to assert the absence of a value in an array, a substring in a string, or a subset of properties in an object.
```ts
import { assert, test } from 'vitest'
test('assert.notInclude', () => {
assert.notInclude([1, 2, 3], 4, 'array doesn\'t contain 4')
assert.notInclude('foobar', 'baz', 'foobar doesn\'t contain baz')
assert.notInclude({ foo: 'bar', hello: 'universe' }, { foo: 'baz' }, 'object doesn\'t contain property')
})
```
## deepInclude
* **Type:**
* `(haystack: string, needle: string, message?: string) => void`
* `(haystack: readonly T[] | ReadonlySet | ReadonlyMap, needle: T, message?: string) => void`
* `(haystack: T, needle: T extends WeakSet ? never : Partial, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a value in an array or a subset of properties in an object. Deep equality is used.
```ts
import { assert, test } from 'vitest'
const obj1 = { a: 1 }
const obj2 = { b: 2 }
test('assert.deepInclude', () => {
assert.deepInclude([obj1, obj2], { a: 1 })
assert.deepInclude({ foo: obj1, bar: obj2 }, { foo: { a: 1 } })
})
```
## notDeepInclude
* **Type:**
* `(haystack: string, needle: string, message?: string) => void`
* `(haystack: readonly T[] | ReadonlySet | ReadonlyMap, needle: T, message?: string) => void`
* `(haystack: T, needle: T extends WeakSet ? never : Partial, message?: string) => void`
Asserts that `haystack` does not include `needle`. It can be used to assert the absence of a value in an array or a subset of properties in an object. Deep equality is used.
```ts
import { assert, test } from 'vitest'
const obj1 = { a: 1 }
const obj2 = { b: 2 }
test('assert.notDeepInclude', () => {
assert.notDeepInclude([obj1, obj2], { a: 10 })
assert.notDeepInclude({ foo: obj1, bar: obj2 }, { foo: { a: 10 } })
})
```
## nestedInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a subset of properties in an object. Enables the use of dot- and bracket-notation for referencing nested properties. ‘\[]’ and ‘.’ in property names can be escaped using double backslashes.
```ts
import { assert, test } from 'vitest'
test('assert.nestedInclude', () => {
assert.nestedInclude({ '.a': { b: 'x' } }, { '\\.a.[b]': 'x' })
assert.nestedInclude({ a: { '[b]': 'x' } }, { 'a.\\[b\\]': 'x' })
})
```
## notNestedInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` does not include `needle`. Can be used to assert the inclusion of a subset of properties in an object. Enables the use of dot- and bracket-notation for referencing nested properties. ‘\[]’ and ‘.’ in property names can be escaped using double backslashes.
```ts
import { assert, test } from 'vitest'
test('assert.nestedInclude', () => {
assert.notNestedInclude({ '.a': { b: 'x' } }, { '\\.a.b': 'y' })
assert.notNestedInclude({ a: { '[b]': 'x' } }, { 'a.\\[b\\]': 'y' })
})
```
## deepNestedInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a subset of properties in an object while checking for deep equality. Enables the use of dot- and bracket-notation for referencing nested properties. ‘\[]’ and ‘.’ in property names can be escaped using double backslashes.
```ts
import { assert, test } from 'vitest'
test('assert.deepNestedInclude', () => {
assert.deepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { x: 1 } })
assert.deepNestedInclude({ '.a': { '[b]': { x: 1 } } }, { '\\.a.\\[b\\]': { x: 1 } })
})
```
## notDeepNestedInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` not includes `needle`. Can be used to assert the absence of a subset of properties in an object while checking for deep equality. Enables the use of dot- and bracket-notation for referencing nested properties. ‘\[]’ and ‘.’ in property names can be escaped using double backslashes.
```ts
import { assert, test } from 'vitest'
test('assert.notDeepNestedInclude', () => {
assert.notDeepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { y: 1 } })
assert.notDeepNestedInclude({ '.a': { '[b]': { x: 1 } } }, { '\\.a.\\[b\\]': { y: 2 } })
})
```
## ownInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a subset of properties in an object while ignoring inherited properties.
```ts
import { assert, test } from 'vitest'
test('assert.ownInclude', () => {
assert.ownInclude({ a: 1 }, { a: 1 })
})
```
## notOwnInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the absence of a subset of properties in an object while ignoring inherited properties.
```ts
import { assert, test } from 'vitest'
const obj1 = {
b: 2
}
const obj2 = object.create(obj1)
obj2.a = 1
test('assert.notOwnInclude', () => {
assert.notOwnInclude(obj2, { b: 2 })
})
```
## deepOwnInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` includes `needle`. Can be used to assert the inclusion of a subset of properties in an object while ignoring inherited properties and checking for deep equality.
```ts
import { assert, test } from 'vitest'
test('assert.deepOwnInclude', () => {
assert.deepOwnInclude({ a: { b: 2 } }, { a: { b: 2 } })
})
```
## notDeepOwnInclude
* **Type:** `(haystack: any, needle: any, message?: string) => void`
Asserts that `haystack` not includes `needle`. Can be used to assert the absence of a subset of properties in an object while ignoring inherited properties and checking for deep equality.
```ts
import { assert, test } from 'vitest'
test('assert.notDeepOwnInclude', () => {
assert.notDeepOwnInclude({ a: { b: 2 } }, { a: { c: 3 } })
})
```
## match
* **Type:** `(value: string, regexp: RegExp, message?: string) => void`
Asserts that `value` matches the regular expression `regexp`.
```ts
import { assert, test } from 'vitest'
test('assert.match', () => {
assert.match('foobar', /^foo/, 'regexp matches')
})
```
## notMatch
* **Type:** `(value: string, regexp: RegExp, message?: string) => void`
Asserts that `value` does not matches the regular expression `regexp`.
```ts
import { assert, test } from 'vitest'
test('assert.notMatch', () => {
assert.notMatch('foobar', /^foo/, 'regexp does not match')
})
```
## property
* **Type:** `(object: T, property: string, message?: string) => void`
Asserts that `object` has a direct or inherited property named by `property`
```ts
import { assert, test } from 'vitest'
test('assert.property', () => {
assert.property({ tea: { green: 'matcha' } }, 'tea')
assert.property({ tea: { green: 'matcha' } }, 'toString')
})
```
## notProperty
* **Type:** `(object: T, property: string, message?: string) => void`
Asserts that `object` does not have a direct or inherited property named by `property`
```ts
import { assert, test } from 'vitest'
test('assert.notProperty', () => {
assert.notProperty({ tea: { green: 'matcha' } }, 'coffee')
})
```
## propertyVal
* **Type:** `(object: T, property: string, value: V, message?: string) => void`
Asserts that `object` has a direct or inherited property named by `property` with a value given by `value`. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notPropertyVal', () => {
assert.propertyVal({ tea: 'is good' }, 'tea', 'is good')
})
```
## notPropertyVal
* **Type:** `(object: T, property: string, value: V, message?: string) => void`
Asserts that `object` does not have a direct or inherited property named by `property` with a value given by `value`. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notPropertyVal', () => {
assert.notPropertyVal({ tea: 'is good' }, 'tea', 'is bad')
assert.notPropertyVal({ tea: 'is good' }, 'coffee', 'is good')
})
```
## deepPropertyVal
* **Type:** `(object: T, property: string, value: V, message?: string) => void`
Asserts that `object` has a direct or inherited property named by `property` with a value given by `value`. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.deepPropertyVal', () => {
assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' })
})
```
## notDeepPropertyVal
* **Type:** `(object: T, property: string, value: V, message?: string) => void`
Asserts that `object` does not have a direct or inherited property named by `property` with a value given by `value`. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.deepPropertyVal', () => {
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { black: 'matcha' })
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'oolong' })
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'coffee', { green: 'matcha' })
})
```
## nestedProperty
* **Type:** `(object: T, property: string, message?: string) => void`
Asserts that `object` has a direct or inherited property named by `property`, which can be a string using dot- and bracket-notation for nested reference.
```ts
import { assert, test } from 'vitest'
test('assert.deepPropertyVal', () => {
assert.nestedProperty({ tea: { green: 'matcha' } }, 'tea.green')
})
```
## notNestedProperty
* **Type:** `(object: T, property: string, message?: string) => void`
Asserts that `object` does not have a direct or inherited property named by `property`, which can be a string using dot- and bracket-notation for nested reference.
```ts
import { assert, test } from 'vitest'
test('assert.deepPropertyVal', () => {
assert.notNestedProperty({ tea: { green: 'matcha' } }, 'tea.oolong')
})
```
## nestedPropertyVal
* **Type:** `(object: T, property: string, value: any, message?: string) => void`
Asserts that `object` has a property named by `property` with value given by `value`. `property` can use dot- and bracket-notation for nested reference. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.nestedPropertyVal', () => {
assert.nestedPropertyVal({ tea: { green: 'matcha' } }, 'tea.green', 'matcha')
})
```
## notNestedPropertyVal
* **Type:** `(object: T, property: string, value: any, message?: string) => void`
Asserts that `object` does not have a property named by `property` with value given by `value`. `property` can use dot- and bracket-notation for nested reference. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notNestedPropertyVal', () => {
assert.notNestedPropertyVal({ tea: { green: 'matcha' } }, 'tea.green', 'konacha')
assert.notNestedPropertyVal({ tea: { green: 'matcha' } }, 'coffee.green', 'matcha')
})
```
## deepNestedPropertyVal
* **Type:** `(object: T, property: string, value: any, message?: string) => void`
Asserts that `object` has a property named by `property` with a value given by `value`. `property` can use dot- and bracket-notation for nested reference. Uses a deep equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notNestedPropertyVal', () => {
assert.notNestedPropertyVal({ tea: { green: 'matcha' } }, 'tea.green', 'konacha')
assert.notNestedPropertyVal({ tea: { green: 'matcha' } }, 'coffee.green', 'matcha')
})
```
## notDeepNestedPropertyVal
* **Type:** `(object: T, property: string, value: any, message?: string) => void`
Asserts that `object` does not have a property named by `property` with value given by `value`. `property` can use dot- and bracket-notation for nested reference. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.notDeepNestedPropertyVal', () => {
assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { oolong: 'yum' })
assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { matcha: 'yuck' })
assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.black', { matcha: 'yum' })
})
```
## lengthOf
* **Type:** `(object: T, length: number, message?: string) => void`
Asserts that `object` has a `length` or `size` with the expected value.
```ts
import { assert, test } from 'vitest'
test('assert.lengthOf', () => {
assert.lengthOf([1, 2, 3], 3, 'array has length of 3')
assert.lengthOf('foobar', 6, 'string has length of 6')
assert.lengthOf(new Set([1, 2, 3]), 3, 'set has size of 3')
assert.lengthOf(new Map([['a', 1], ['b', 2], ['c', 3]]), 3, 'map has size of 3')
})
```
## hasAnyKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has at least one of the `keys` provided. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.hasAnyKeys', () => {
assert.hasAnyKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'iDontExist', 'baz'])
assert.hasAnyKeys({ foo: 1, bar: 2, baz: 3 }, { foo: 30, iDontExist: 99, baz: 1337 })
assert.hasAnyKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ foo: 1 }, 'key'])
assert.hasAnyKeys(new Set([{ foo: 'bar' }, 'anotherKey']), [{ foo: 'bar' }, 'anotherKey'])
})
```
## hasAllKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has all and only all of the `keys` provided. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.hasAllKeys', () => {
assert.hasAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar', 'baz'])
assert.hasAllKeys({ foo: 1, bar: 2, baz: 3 }, { foo: 30, bar: 99, baz: 1337 })
assert.hasAllKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ foo: 1 }, 'key'])
assert.hasAllKeys(new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }, 'anotherKey']))
})
```
## containsAllKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has all of the `keys` provided but may have more keys not listed. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.containsAllKeys', () => {
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz'])
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar', 'baz'])
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, { foo: 30, baz: 1337 })
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, { foo: 30, bar: 99, baz: 1337 })
assert.containsAllKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ foo: 1 }])
assert.containsAllKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ foo: 1 }, 'key'])
assert.containsAllKeys(new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }]))
assert.containsAllKeys(new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }, 'anotherKey']))
})
```
## doesNotHaveAnyKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has none of the `keys` provided. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotHaveAnyKeys', () => {
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2, baz: 3 }, ['one', 'two', 'example'])
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2, baz: 3 }, { one: 1, two: 2, example: 'foo' })
assert.doesNotHaveAnyKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ one: 'two' }, 'example'])
assert.doesNotHaveAnyKeys(new Set([{ foo: 'bar' }, 'anotherKey'], [{ one: 'two' }, 'example']))
})
```
## doesNotHaveAllKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` does not have at least one of the `keys` provided. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.hasAnyKeys', () => {
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2, baz: 3 }, ['one', 'two', 'example'])
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2, baz: 3 }, { one: 1, two: 2, example: 'foo' })
assert.doesNotHaveAnyKeys(new Map([[{ foo: 1 }, 'bar'], ['key', 'value']]), [{ one: 'two' }, 'example'])
assert.doesNotHaveAnyKeys(new Set([{ foo: 'bar' }, 'anotherKey']), [{ one: 'two' }, 'example'])
})
```
## hasAnyDeepKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has at least one of the `keys` provided. Since Sets and Maps can have objects as keys you can use this assertion to perform a deep comparison. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.hasAnyDeepKeys', () => {
assert.hasAnyDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [1, 2]]), { one: 'one' })
assert.hasAnyDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [1, 2]]), [{ one: 'one' }, { two: 'two' }])
assert.hasAnyDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [{ two: 'two' }, 'valueTwo']]), [{ one: 'one' }, { two: 'two' }])
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), { one: 'one' })
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ one: 'one' }, { three: 'three' }])
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ one: 'one' }, { two: 'two' }])
})
```
## hasAllDeepKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has all and only all of the `keys` provided. Since Sets and Maps can have objects as keys you can use this assertion to perform a deep comparison. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.hasAnyDeepKeys', () => {
assert.hasAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne']]), { one: 'one' })
assert.hasAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [{ two: 'two' }, 'valueTwo']]), [{ one: 'one' }, { two: 'two' }])
assert.hasAllDeepKeys(new Set([{ one: 'one' }]), { one: 'one' })
assert.hasAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ one: 'one' }, { two: 'two' }])
})
```
## containsAllDeepKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` contains all of the `keys` provided. Since Sets and Maps can have objects as keys you can use this assertion to perform a deep comparison. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.containsAllDeepKeys', () => {
assert.containsAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [1, 2]]), { one: 'one' })
assert.containsAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [{ two: 'two' }, 'valueTwo']]), [{ one: 'one' }, { two: 'two' }])
assert.containsAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), { one: 'one' })
assert.containsAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ one: 'one' }, { two: 'two' }])
})
```
## doesNotHaveAnyDeepKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` has none of the `keys` provided. Since Sets and Maps can have objects as keys you can use this assertion to perform a deep comparison. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotHaveAnyDeepKeys', () => {
assert.doesNotHaveAnyDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [1, 2]]), { thisDoesNot: 'exist' })
assert.doesNotHaveAnyDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [{ two: 'two' }, 'valueTwo']]), [{ twenty: 'twenty' }, { fifty: 'fifty' }])
assert.doesNotHaveAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), { twenty: 'twenty' })
assert.doesNotHaveAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ twenty: 'twenty' }, { fifty: 'fifty' }])
})
```
## doesNotHaveAllDeepKeys
* **Type:** `(object: T, keys: Array | { [key: string]: any }, message?: string) => void`
Asserts that `object` does not have at least one of the `keys` provided. Since Sets and Maps can have objects as keys you can use this assertion to perform a deep comparison. You can also provide a single object instead of a keys array and its keys will be used as the expected set of keys.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotHaveAllDeepKeys', () => {
assert.doesNotHaveAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [1, 2]]), { thisDoesNot: 'exist' })
assert.doesNotHaveAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne'], [{ two: 'two' }, 'valueTwo']]), [{ twenty: 'twenty' }, { one: 'one' }])
assert.doesNotHaveAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), { twenty: 'twenty' })
assert.doesNotHaveAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [{ one: 'one' }, { fifty: 'fifty' }])
})
```
## throws
* **Type:**
* `(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void`
* `(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void`
* **Alias:**
* `throw`
* `Throw`
If `errorLike` is an Error constructor, asserts that `fn` will throw an error that is an instance of `errorLike`. If errorLike is an Error instance, asserts that the error thrown is the same instance as `errorLike`. If `errMsgMatcher` is provided, it also asserts that the error thrown will have a message matching `errMsgMatcher`.
```ts
import { assert, test } from 'vitest'
test('assert.throws', () => {
assert.throws(fn, 'Error thrown must have this msg')
assert.throws(fn, /Error thrown must have a msg that matches this/)
assert.throws(fn, ReferenceError)
assert.throws(fn, errorInstance)
assert.throws(fn, ReferenceError, 'Error thrown must be a ReferenceError and have this msg')
assert.throws(fn, errorInstance, 'Error thrown must be the same errorInstance and have this msg')
assert.throws(fn, ReferenceError, /Error thrown must be a ReferenceError and match this/)
assert.throws(fn, errorInstance, /Error thrown must be the same errorInstance and match this/)
})
```
## doesNotThrow
* **Type:** `(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void`
* **Type:** `(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void`
If `errorLike` is an Error constructor, asserts that `fn` will not throw an error that is an instance of `errorLike`. If errorLike is an Error instance, asserts that the error thrown is not the same instance as `errorLike`. If `errMsgMatcher` is provided, it also asserts that the error thrown will not have a message matching `errMsgMatcher`.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotThrow', () => {
assert.doesNotThrow(fn, 'Any Error thrown must not have this message')
assert.doesNotThrow(fn, /Any Error thrown must not match this/)
assert.doesNotThrow(fn, Error)
assert.doesNotThrow(fn, errorInstance)
assert.doesNotThrow(fn, Error, 'Error must not have this message')
assert.doesNotThrow(fn, errorInstance, 'Error must not have this message')
assert.doesNotThrow(fn, Error, /Error must not match this/)
assert.doesNotThrow(fn, errorInstance, /Error must not match this/)
})
```
## operator
* **Type:** `(val1: OperatorComparable, operator: Operator, val2: OperatorComparable, message?: string) => void`
Compare `val1` and `val2` using `operator`.
```ts
import { assert, test } from 'vitest'
test('assert.operator', () => {
assert.operator(1, '<', 2, 'everything is ok')
})
```
## closeTo
* **Type:** `(actual: number, expected: number, delta: number, message?: string) => void`
* **Alias:** `approximately`
Asserts that the `actual` is equal `expected`, to within a +/- `delta` range.
```ts
import { assert, test } from 'vitest'
test('assert.closeTo', () => {
assert.closeTo(1.5, 1, 0.5, 'numbers are close')
})
```
## sameMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` have the same members in any order. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.sameMembers', () => {
assert.sameMembers([1, 2, 3], [2, 1, 3], 'same members')
})
```
## notSameMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` don't have the same members in any order. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.sameMembers', () => {
assert.notSameMembers([1, 2, 3], [5, 1, 3], 'not same members')
})
```
## sameDeepMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` have the same members in any order. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.sameDeepMembers', () => {
assert.sameDeepMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { a: 1 }, { c: 3 }], 'same deep members')
})
```
## notSameDeepMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` don’t have the same members in any order. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.sameDeepMembers', () => {
assert.sameDeepMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { a: 1 }, { c: 3 }], 'same deep members')
})
```
## sameOrderedMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` have the same members in the same order. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.sameOrderedMembers', () => {
assert.sameOrderedMembers([1, 2, 3], [1, 2, 3], 'same ordered members')
})
```
## notSameOrderedMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` have the same members in the same order. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notSameOrderedMembers', () => {
assert.notSameOrderedMembers([1, 2, 3], [2, 1, 3], 'not same ordered members')
})
```
## sameDeepOrderedMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` have the same members in the same order. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.sameDeepOrderedMembers', () => {
assert.sameDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ a: 1 }, { b: 2 }, { c: 3 }], 'same deep ordered members')
})
```
## notSameDeepOrderedMembers
* **Type:** `(set1: T[], set2: T[], message?: string) => void`
Asserts that `set1` and `set2` don’t have the same members in the same order. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.notSameDeepOrderedMembers', () => {
assert.notSameDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ a: 1 }, { b: 2 }, { z: 5 }], 'not same deep ordered members')
assert.notSameDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { a: 1 }, { c: 3 }], 'not same deep ordered members')
})
```
## includeMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` is included in `superset` in any order. Uses a strict equality check (===). Duplicates are ignored.
```ts
import { assert, test } from 'vitest'
test('assert.includeMembers', () => {
assert.includeMembers([1, 2, 3], [2, 1, 2], 'include members')
})
```
## notIncludeMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` isn't included in `superset` in any order. Uses a strict equality check (===). Duplicates are ignored.
```ts
import { assert, test } from 'vitest'
test('assert.notIncludeMembers', () => {
assert.notIncludeMembers([1, 2, 3], [5, 1], 'not include members')
})
```
## includeDeepMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` is included in `superset` in any order. Uses a deep equality check. Duplicates are ignored.
```ts
import { assert, test } from 'vitest'
test('assert.includeDeepMembers', () => {
assert.includeDeepMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { a: 1 }, { b: 2 }], 'include deep members')
})
```
## notIncludeDeepMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` isn’t included in `superset` in any order. Uses a deep equality check. Duplicates are ignored.
```ts
import { assert, test } from 'vitest'
test('assert.notIncludeDeepMembers', () => {
assert.notIncludeDeepMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { f: 5 }], 'not include deep members')
})
```
## includeOrderedMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` is included in `superset` in the same order beginning with the first element in `superset`. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.includeOrderedMembers', () => {
assert.includeOrderedMembers([1, 2, 3], [1, 2], 'include ordered members')
})
```
## notIncludeOrderedMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` isn't included in `superset` in the same order beginning with the first element in `superset`. Uses a strict equality check (===).
```ts
import { assert, test } from 'vitest'
test('assert.notIncludeOrderedMembers', () => {
assert.notIncludeOrderedMembers([1, 2, 3], [2, 1], 'not include ordered members')
assert.notIncludeOrderedMembers([1, 2, 3], [2, 3], 'not include ordered members')
})
```
## includeDeepOrderedMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` is included in `superset` in the same order beginning with the first element in `superset`. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.includeDeepOrderedMembers', () => {
assert.includeDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ a: 1 }, { b: 2 }], 'include deep ordered members')
})
```
## notIncludeDeepOrderedMembers
* **Type:** `(superset: T[], subset: T[], message?: string) => void`
Asserts that `subset` isn’t included in `superset` in the same order beginning with the first element in superset. Uses a deep equality check.
```ts
import { assert, test } from 'vitest'
test('assert.includeDeepOrderedMembers', () => {
assert.notIncludeDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ a: 1 }, { f: 5 }], 'not include deep ordered members')
assert.notIncludeDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { a: 1 }], 'not include deep ordered members')
assert.notIncludeDeepOrderedMembers([{ a: 1 }, { b: 2 }, { c: 3 }], [{ b: 2 }, { c: 3 }], 'not include deep ordered members')
})
```
## oneOf
* **Type:** `(inList: T, list: T[], message?: string) => void`
Asserts that non-object, non-array value `inList` appears in the flat array `list`.
```ts
import { assert, test } from 'vitest'
test('assert.oneOf', () => {
assert.oneOf(1, [2, 1], 'Not found in list')
})
```
## changes
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` changes the `object` of a `property`.
```ts
import { assert, test } from 'vitest'
test('assert.changes', () => {
const obj = { val: 10 }
function fn() { obj.val = 22 };
assert.changes(fn, obj, 'val')
})
```
## changesBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` changes the `object` of a `property` by a `change`.
```ts
import { assert, test } from 'vitest'
test('assert.changesBy', () => {
const obj = { val: 10 }
function fn() { obj.val += 2 };
assert.changesBy(fn, obj, 'val', 2)
})
```
## doesNotChange
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` does not changes the `object` of a `property`.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotChange', () => {
const obj = { val: 10 }
function fn() { obj.val += 2 };
assert.doesNotChange(fn, obj, 'val', 2)
})
```
## changesButNotBy
* **Type:** `(modifier: Function, object: T, property: string, change:number, message?: string) => void`
Asserts that a `modifier` does not change the `object` of a `property` or of a `modifier` return value by a `change`.
```ts
import { assert, test } from 'vitest'
test('assert.changesButNotBy', () => {
const obj = { val: 10 }
function fn() { obj.val += 10 };
assert.changesButNotBy(fn, obj, 'val', 5)
})
```
## increases
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` increases a numeric `object`'s `property`.
```ts
import { assert, test } from 'vitest'
test('assert.increases', () => {
const obj = { val: 10 }
function fn() { obj.val = 13 };
assert.increases(fn, obj, 'val')
})
```
## increasesBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` increases a numeric `object`'s `property` or a `modifier` return value by an `change`.
```ts
import { assert, test } from 'vitest'
test('assert.increasesBy', () => {
const obj = { val: 10 }
function fn() { obj.val += 10 };
assert.increasesBy(fn, obj, 'val', 10)
})
```
## doesNotIncrease
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` does not increases a numeric `object`'s `property`.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotIncrease', () => {
const obj = { val: 10 }
function fn() { obj.val = 8 }
assert.doesNotIncrease(fn, obj, 'val')
})
```
## increasesButNotBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` does not increases a numeric `object`'s `property` or a `modifier` return value by an `change`.
```ts
import { assert, test } from 'vitest'
test('assert.increasesButNotBy', () => {
const obj = { val: 10 }
function fn() { obj.val += 15 };
assert.increasesButNotBy(fn, obj, 'val', 10)
})
```
## decreases
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` decreases a numeric `object`'s `property`.
```ts
import { assert, test } from 'vitest'
test('assert.decreases', () => {
const obj = { val: 10 }
function fn() { obj.val = 5 };
assert.decreases(fn, obj, 'val')
})
```
## decreasesBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` decreases a numeric `object`'s `property` or a `modifier` return value by a `change`.
```ts
import { assert, test } from 'vitest'
test('assert.decreasesBy', () => {
const obj = { val: 10 }
function fn() { obj.val -= 5 };
assert.decreasesBy(fn, obj, 'val', 5)
})
```
## doesNotDecrease
* **Type:** `(modifier: Function, object: T, property: string, message?: string) => void`
Asserts that a `modifier` dose not decrease a numeric `object`'s `property`.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotDecrease', () => {
const obj = { val: 10 }
function fn() { obj.val = 15 }
assert.doesNotDecrease(fn, obj, 'val')
})
```
## doesNotDecreaseBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` does not decrease a numeric `object`'s `property` or a `modifier` return value by a `change`.
```ts
import { assert, test } from 'vitest'
test('assert.doesNotDecreaseBy', () => {
const obj = { val: 10 }
function fn() { obj.val = 5 };
assert.doesNotDecreaseBy(fn, obj, 'val', 1)
})
```
## decreasesButNotBy
* **Type:** `(modifier: Function, object: T, property: string, change: number, message?: string) => void`
Asserts that a `modifier` does not decrease a numeric `object`'s `property` or a `modifier` return value by a `change`.
```ts
import { assert, test } from 'vitest'
test('assert.decreasesButNotBy', () => {
const obj = { val: 10 }
function fn() { obj.val = 5 };
assert.decreasesButNotBy(fn, obj, 'val', 1)
})
```
## ifError
* **Type:** `(object: T, message?: string) => void`
Asserts if `object` is not a false value, and throws if it is a true value. This is added to allow for chai to be a drop-in replacement for Node’s assert class.
```ts
import { assert, test } from 'vitest'
test('assert.ifError', () => {
const err = new Error('I am a custom error')
assert.ifError(err) // Rethrows err!
})
```
## isExtensible
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `extensible`
Asserts that `object` is extensible (can have new properties added to it).
```ts
import { assert, test } from 'vitest'
test('assert.isExtensible', () => {
assert.isExtensible({})
})
```
## isNotExtensible
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `notExtensible`
Asserts that `object` is not extensible (can not have new properties added to it).
```ts
import { assert, test } from 'vitest'
test('assert.isNotExtensible', () => {
const nonExtensibleObject = Object.preventExtensions({})
const sealedObject = Object.seal({})
const frozenObject = Object.freeze({})
assert.isNotExtensible(nonExtensibleObject)
assert.isNotExtensible(sealedObject)
assert.isNotExtensible(frozenObject)
})
```
## isSealed
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `sealed`
Asserts that `object` is sealed (cannot have new properties added to it and its existing properties cannot be removed).
```ts
import { assert, test } from 'vitest'
test('assert.isSealed', () => {
const sealedObject = Object.seal({})
const frozenObject = Object.seal({})
assert.isSealed(sealedObject)
assert.isSealed(frozenObject)
})
```
## isNotSealed
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `notSealed`
Asserts that `object` is not sealed (can have new properties added to it and its existing properties can be removed).
```ts
import { assert, test } from 'vitest'
test('assert.isNotSealed', () => {
assert.isNotSealed({})
})
```
## isFrozen
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `frozen`
Asserts that object is frozen (cannot have new properties added to it and its existing properties cannot be modified).
```ts
import { assert, test } from 'vitest'
test('assert.isFrozen', () => {
const frozenObject = Object.freeze({})
assert.frozen(frozenObject)
})
```
## isNotFrozen
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `notFrozen`
Asserts that `object` is not frozen (can have new properties added to it and its existing properties can be modified).
```ts
import { assert, test } from 'vitest'
test('assert.isNotFrozen', () => {
assert.isNotFrozen({})
})
```
## isEmpty
* **Type:** `(target: T, message?: string) => void`
* **Alias:** `empty`
Asserts that the `target` does not contain any values. For arrays and strings, it checks the length property. For Map and Set instances, it checks the size property. For non-function objects, it gets the count of its own enumerable string keys.
```ts
import { assert, test } from 'vitest'
test('assert.isEmpty', () => {
assert.isEmpty([])
assert.isEmpty('')
assert.isEmpty(new Map())
assert.isEmpty({})
})
```
## isNotEmpty
* **Type:** `(object: T, message?: string) => void`
* **Alias:** `notEmpty`
Asserts that the `target` contains values. For arrays and strings, it checks the length property. For Map and Set instances, it checks the size property. For non-function objects, it gets the count of its own enumerable string keys.
```ts
import { assert, test } from 'vitest'
test('assert.isNotEmpty', () => {
assert.isNotEmpty([1, 2])
assert.isNotEmpty('34')
assert.isNotEmpty(new Set([5, 6]))
assert.isNotEmpty({ key: 7 })
})
```
---
---
url: /api/browser/assertions.md
---
# 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
submit
```
```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
submit
```
```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 4.0.0 {#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 | Locator | 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 nonExistingElement = getByTestId('does-not-exist')
await expect.element(ancestor).toContainElement(descendant)
await expect.element(descendant).not.toContainElement(ancestor)
await expect.element(ancestor).not.toContainElement(nonExistingElement)
```
## 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 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
The logo of Our Company
```
```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
This field is invalid
```
```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 title
Test content
Test
```
```javascript
await expect.element(getByTestId('img-alt')).toHaveAccessibleName('Test alt')
await expect.element(getByTestId('img-empty-alt')).not.toHaveAccessibleName()
await expect.element(getByTestId('svg-title')).toHaveAccessibleName('Test title')
await expect.element(getByTestId('button-img-alt')).toHaveAccessibleName()
await expect.element(getByTestId('img-paragraph')).not.toHaveAccessibleName()
await expect.element(getByTestId('svg-button')).toHaveAccessibleName()
await expect.element(getByTestId('svg-without-title')).not.toHaveAccessibleName()
await expect.element(getByTestId('input-title')).toHaveAccessibleName()
```
## toHaveAttribute
```ts
function toHaveAttribute(attribute: string, value?: unknown): Promise
```
This allows you to check whether the given element has an attribute or not. You
can also optionally check that the attribute has a specific expected value or
partial match using [`expect.stringContaining`](/api/expect#expect-stringcontaining) or [`expect.stringMatching`](/api/expect#expect-stringmatching).
```html
ok
```
```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
No Classes
```
```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 do not. Here's a list
of all cases covered:
* ` ` 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 without the `multiple` attribute return the value as a
**string** corresponding to the `value` attribute of the selected `option`, or
`undefined` if there's no selected option.
* `` 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).
* `