Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

Vitest reexports the `assert` method from [`chai`](https://www.chaijs.com/api/assert/) for verifying invariants.

::: warning In-Source Testing {#in-source-testing}
When using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` from `import.meta.vitest` in [in-source tests](/guide/in-source), TypeScript reports error `TS2775` because they must be called via an explicitly annotated name. Annotate the variable with `Chai.Assert` or call it directly:

::: code-group
```ts [Annotated variable]
if (import.meta.vitest) {
const { test, assert } = import.meta.vitest // [!code --]
const { test } = import.meta.vitest // [!code ++]
const assert: Chai.Assert = import.meta.vitest.assert // [!code ++]

test('assert', () => {
assert('foo' !== 'bar', 'foo should not be equal to bar')
})
}
```
```ts [Direct call]
if (import.meta.vitest) {
const { test, assert } = import.meta.vitest // [!code --]
const { test } = import.meta.vitest // [!code ++]

test('assert', () => {
assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code --]
import.meta.vitest!.assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code ++]
})
}
```
:::

## assert

- **Type:** `(expression: any, message?: string) => asserts expression`
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/in-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ To get TypeScript support for `import.meta.vitest`, add `vitest/importMeta` to y

Reference to [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test) for the full example.

::: warning
There is a limitation when using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` in in-source tests. See [`assert`](/api/assert#in-source-testing) for details and workarounds.
:::

## Notes

This feature could be useful for:
Expand Down
Loading