Skip to content

Commit ae028e8

Browse files
cjihrigRafaelGSS
authored andcommitted
doc: improve assert.snapshot() docs
PR-URL: #44429 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent 71c8696 commit ae028e8

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

doc/api/assert.md

+28-12
Original file line numberDiff line numberDiff line change
@@ -2097,23 +2097,39 @@ added: v18.8.0
20972097

20982098
> Stability: 1 - Experimental
20992099
2100-
* `value` {any} the value to snapshot
2101-
* `name` {string} the name of snapshot.
2100+
* `value` {any} the value to snapshot.
2101+
* `name` {string} the name of the snapshot.
21022102
* Returns: {Promise}
21032103

2104-
reads a snapshot from a file, and compares `value` to the snapshot.
2105-
`value` is serialized with [`util.inspect()`][]
2106-
If the value is not strictly equal to the snapshot,
2107-
`assert.snapshot()` will return a rejected `Promise`
2108-
with an [`AssertionError`][].
2104+
Reads the `name` snapshot from a file and compares `value` to the snapshot.
2105+
`value` is serialized with [`util.inspect()`][]. If the value is not strictly
2106+
equal to the snapshot, `assert.snapshot()` returns a rejected `Promise` with an
2107+
[`AssertionError`][].
2108+
2109+
The snapshot filename uses the same basename as the application's main
2110+
entrypoint with a `.snapshot` extension. If the snapshot file does not exist,
2111+
it is created. The [`--update-assert-snapshot`][] command line flag can be used
2112+
to force the update of an existing snapshot.
21092113

2110-
If the snapshot file does not exist, the snapshot is written.
2114+
```mjs
2115+
import assert from 'node:assert/strict';
21112116

2112-
In case it is needed to force a snapshot update,
2113-
use [`--update-assert-snapshot`][];
2117+
// Assuming that the application's main entrypoint is app.mjs, this reads the
2118+
// 'snapshotName' snapshot from app.snapshot and strictly compares its value
2119+
// to `util.inspect('value')`.
2120+
await assert.snapshot('value', 'snapshotName');
2121+
```
21142122

2115-
By default, a snapshot is read and written to a file,
2116-
using the same name as the main entrypoint with `.snapshot` as the extension.
2123+
```cjs
2124+
const assert = require('node:assert/strict');
2125+
2126+
(async () => {
2127+
// Assuming that the application's main entrypoint is app.js, this reads the
2128+
// 'snapshotName' snapshot from app.snapshot and strictly compares its value
2129+
// to `util.inspect('value')`.
2130+
await assert.snapshot('value', 'snapshotName');
2131+
})();
2132+
```
21172133

21182134
## `assert.strictEqual(actual, expected[, message])`
21192135

doc/api/cli.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ loading phase, it will always raise it as an uncaught exception.
14641464
added: v18.8.0
14651465
-->
14661466

1467-
Force updating snapshot files for [`assert.snapshot()`][]
1467+
Updates snapshot files used by [`assert.snapshot()`][].
14681468

14691469
### `--use-bundled-ca`, `--use-openssl-ca`
14701470

0 commit comments

Comments
 (0)