-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
69 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
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
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 was deleted.
Oops, something went wrong.
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,110 @@ | ||
# Snapshot | ||
|
||
Snapshot tests are a very useful tool whenever you want to make sure the output of your functions not change unexpectedly. | ||
|
||
When using snapshot, Vitest will take a snapshot of the given value, then compares it to a reference snapshot file stored alongside the test. The test will fail if the two snapshots do not match: either the change is unexpected, or the reference snapshot needs to be updated to the new version of the result. | ||
|
||
## Use Snapshots | ||
|
||
To snapshoting a value, you can use the [`toMatchSnapshot()`](/api/#tomatchsnapshot) from `expect()` API: | ||
|
||
```ts | ||
import { expect, it } from 'vitest' | ||
|
||
it('toUpperCase', () => { | ||
const result = toUpperCase('foobar') | ||
expect(result).toMatchSnapshot() | ||
}) | ||
``` | ||
|
||
The first time this test is run, Vitest creates a snapshot file that looks like this: | ||
|
||
```js | ||
// Vitest Snapshot v1 | ||
|
||
exports['toUpperCase 1'] = '"FOOBAR"' | ||
``` | ||
|
||
The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process. On subsequent test runs, Vitest will compare the rendered output with the previous snapshot. If they match, the test will pass. If they don't match, either the test runner found a bug in your code that should be fixed, or the implementation has changed and the snapshot needs to be updated. | ||
|
||
## Inline Snapshots | ||
|
||
Similarly, you can use the [`toMatchInlineSnapshot()`](/api/#tomatchinlinesnapshot) to store the snapshot inline within the test file. | ||
|
||
```ts | ||
import { expect, it } from 'vitest' | ||
|
||
it('toUpperCase', () => { | ||
const result = toUpperCase('foobar') | ||
expect(result).toMatchInlineSnapshot() | ||
}) | ||
``` | ||
|
||
Instead of creating a snapshot file, Vitest will modify the test file directory to update the snapshot as a string: | ||
|
||
```ts | ||
import { expect, it } from 'vitest' | ||
|
||
it('toUpperCase', () => { | ||
const result = toUpperCase('foobar') | ||
expect(result).toMatchInlineSnapshot('"FOOBAR"') | ||
}) | ||
``` | ||
|
||
This allows you to see the expect output directly without jumpping across different files. | ||
|
||
## Updating Snapshots | ||
|
||
When the received value doesn't match with the snapshot, the test would fail and show you the difference between them. When the snapshot change is expected, you maybe want to update the snapshot from teh current state. | ||
|
||
In watch mode, you can press `u` key in the terminal to update the failed snapshot directly. | ||
|
||
Or you can use the `--updateSnapshot` or `-u` flag in the CLI to make Vitest into snapshot updating mode. | ||
|
||
```bash | ||
vitest -u | ||
``` | ||
|
||
## Custom 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). |