Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc,test: improvements and cleanup for assert.snapshot() #44429

Merged
merged 3 commits into from Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 28 additions & 12 deletions doc/api/assert.md
Expand Up @@ -2097,23 +2097,39 @@ added: v18.8.0

> Stability: 1 - Experimental

* `value` {any} the value to snapshot
* `name` {string} the name of snapshot.
* `value` {any} the value to snapshot.
* `name` {string} the name of the snapshot.
* Returns: {Promise}

reads a snapshot from a file, and compares `value` to the snapshot.
`value` is serialized with [`util.inspect()`][]
If the value is not strictly equal to the snapshot,
`assert.snapshot()` will return a rejected `Promise`
with an [`AssertionError`][].
Reads the `name` snapshot from a file and compares `value` to the snapshot.
`value` is serialized with [`util.inspect()`][]. If the value is not strictly
equal to the snapshot, `assert.snapshot()` returns a rejected `Promise` with an
[`AssertionError`][].

The snapshot filename uses the same basename as the application's main
entrypoint with a `.snapshot` extension. If the snapshot file does not exist,
it is created. The [`--update-assert-snapshot`][] command line flag can be used
to force the update of an existing snapshot.

If the snapshot file does not exist, the snapshot is written.
```mjs
import assert from 'node:assert/strict';

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

By default, a snapshot is read and written to a file,
using the same name as the main entrypoint with `.snapshot` as the extension.
```cjs
const assert = require('node:assert/strict');

(async () => {
// Assuming that the application's main entrypoint is app.js, this reads the
// 'snapshotName' snapshot from app.snapshot and strictly compares its value
// to `util.inspect('value')`.
await assert.snapshot('value', 'snapshotName');
})();
```

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

Expand Down
2 changes: 1 addition & 1 deletion doc/api/cli.md
Expand Up @@ -1512,7 +1512,7 @@ loading phase, it will always raise it as an uncaught exception.
added: v18.8.0
-->

Force updating snapshot files for [`assert.snapshot()`][]
Updates snapshot files used by [`assert.snapshot()`][].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be marked as experimental.


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

Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Expand Up @@ -486,6 +486,9 @@ Track heap object allocations for heap snapshots.
.It Fl -unhandled-rejections=mode
Define the behavior for unhandled rejections. Can be one of `strict` (raise an error), `warn` (enforce warnings) or `none` (silence warnings).
.
.It Fl -update-assert-snapshot
Updates snapshot files used by `assert.snapshot()`.
.
.It Fl -use-bundled-ca , Fl -use-openssl-ca
Use bundled Mozilla CA store as supplied by current Node.js version or use OpenSSL's default CA store.
The default store is selectable at build-time.
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/assert-snapshot/basic.mjs
@@ -1,3 +1,3 @@
import assert from 'node:assert';

await assert.snapshot("test", "name");
await assert.snapshot('test', 'name');
4 changes: 2 additions & 2 deletions test/fixtures/assert-snapshot/multiple.mjs
@@ -1,4 +1,4 @@
import assert from 'node:assert';

await assert.snapshot("test", "name");
await assert.snapshot("test", "another name");
await assert.snapshot('test', 'name');
await assert.snapshot('test', 'another name');
4 changes: 2 additions & 2 deletions test/fixtures/assert-snapshot/non-existing-name.mjs
@@ -1,4 +1,4 @@
import assert from 'node:assert';

await assert.snapshot("test", "another name");
await assert.snapshot("test", "non existing");
await assert.snapshot('test', 'another name');
await assert.snapshot('test', 'non existing');
2 changes: 1 addition & 1 deletion test/fixtures/assert-snapshot/single.mjs
@@ -1,3 +1,3 @@
import assert from 'node:assert';

await assert.snapshot("test", "snapshot");
await assert.snapshot('test', 'snapshot');
2 changes: 1 addition & 1 deletion test/fixtures/assert-snapshot/value-changed.mjs
@@ -1,3 +1,3 @@
import assert from 'node:assert';

await assert.snapshot("changed", "snapshot");
await assert.snapshot('changed', 'snapshot');