Skip to content

Commit f94a1f7

Browse files
cjihrigFyko
authored andcommitted
doc: improve assert.snapshot() docs
PR-URL: nodejs#44429 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 01c81a2 commit f94a1f7

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

doc/api/assert.md

Lines changed: 28 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ loading phase, it will always raise it as an uncaught exception.
15121512
added: v18.8.0
15131513
-->
15141514

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

15171517
### `--use-bundled-ca`, `--use-openssl-ca`
15181518

0 commit comments

Comments
 (0)