Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add more expects and guides (#1236)
- Loading branch information
1 parent
3d8e846
commit 93422cb
Showing
4 changed files
with
221 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Extending Matchers | ||
|
||
Since Vitest is compatible with both Chai and Jest, you can use either `chai.use` API or `expect.extend`, whichever you prefer. | ||
|
||
This guide will explore extending matchers with `expect.extend`. If you are interested in Chai API, check [their guide](https://www.chaijs.com/guide/plugins/). | ||
|
||
To extend default matchers, call `expect.extend` with an object containing your matchers. | ||
|
||
```ts | ||
expect.extend({ | ||
toBeFoo(received, expected) { | ||
const { isNot } = this | ||
return { | ||
// do not alter your "pass" based on isNot. Vitest does it for you | ||
pass: received === 'foo', | ||
message: () => `${received} is${isNot ? ' not' : ''} foo` | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
The return value of a matcher should be compatible with the following interface: | ||
|
||
```ts | ||
interface MatcherResult { | ||
pass: boolean | ||
message: () => string | ||
// If you pass these, they will automatically appear inside a diff, | ||
// if the matcher will not pass, so you don't need to print diff yourself | ||
actual?: unknown | ||
expected?: unknown | ||
} | ||
``` | ||
|
||
::: warning | ||
If you create an asynchronous matcher, don't forget to `await` the result (`await expect('foo').toBeFoo()`) in the test itself. | ||
::: | ||
|
||
The first argument inside a matchers function is received value (the one inside `expect(received)`). The rest are arguments passed directly to the matcher. | ||
|
||
Matcher function have access to `this` context with the following properties: | ||
|
||
- `isNot` | ||
|
||
Returns true, if matcher was called on `not` (`expect(received).not.toBeFoo()`). | ||
|
||
- `promise` | ||
|
||
If matcher was called on `resolved/rejected`, this value will contain the name of modifier. Otherwise, it will be an empty string. | ||
|
||
- `equals` | ||
|
||
This is utility function that allows you to compare two values. It will return `true` if values are equal, `false` otherwise. This function is used internally for almost every matcher. | ||
It supports objects with asymmetric matchers by default. | ||
|
||
- `utils` | ||
|
||
This contains a set of utility functions that you can use to display messages. | ||
|
||
`this` context also contains information about the current test. You can also get it by calling `expect.getState()`. The most useful properties are: | ||
|
||
- `currentTestName` | ||
|
||
Full name of the current test (including describe block). | ||
|
||
- `testPath` | ||
|
||
Path to the current test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Snapshot Serializer | ||
|
||
You can add your own logic to alter how your snapshots are serialized. Like Jest, Vitest has default serializers for built-in JavaScript types, HTML elements, ImmutableJS and for React elements. | ||
|
||
Example serializer module: | ||
|
||
```ts | ||
expect.addSnapshotSerializer({ | ||
serialize(val, config, indentation, depth, refs, printer) { | ||
// `printer` is a function that serializes a value using existing plugins. | ||
return `Pretty foo: ${printer(val.foo)}` | ||
}, | ||
test(val) { | ||
return val && Object.prototype.hasOwnProperty.call(val, 'foo') | ||
}, | ||
}) | ||
``` | ||
|
||
After adding a test like this: | ||
|
||
```ts | ||
test('foo snapshot test', () => { | ||
const bar = { | ||
foo: { | ||
x: 1, | ||
y: 2, | ||
}, | ||
} | ||
|
||
expect(bar).toMatchSnapshot() | ||
}) | ||
``` | ||
|
||
You will get the following snapshot: | ||
|
||
``` | ||
Pretty foo: Object { | ||
"x": 1, | ||
"y": 2, | ||
} | ||
``` | ||
|
||
We are using Jest's `pretty-format` for serializing snapshots. You can read more about it here: [pretty-format](https://github.com/facebook/jest/blob/main/packages/pretty-format/README.md#serialize). |