From 126fbbab744010d8d080abb3275a110df590da93 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 11 Aug 2022 16:07:52 +0300 Subject: [PATCH] assert: add assert.Snapshot PR-URL: https://github.com/nodejs/node/pull/44095 Reviewed-By: Benjamin Gruenbaum --- doc/api/assert.md | 28 ++++ doc/api/cli.md | 10 ++ doc/api/errors.md | 7 + lib/assert.js | 3 + lib/internal/assert/snapshot.js | 129 +++++++++++++++++ lib/internal/errors.js | 2 + src/node_options.cc | 5 + src/node_options.h | 1 + test/fixtures/assert-snapshot/basic.mjs | 3 + test/fixtures/assert-snapshot/multiple.mjs | 4 + .../assert-snapshot/non-existing-name.mjs | 4 + .../non-existing-name.snapshot | 5 + test/fixtures/assert-snapshot/random.mjs | 11 ++ test/fixtures/assert-snapshot/random.snapshot | 5 + test/fixtures/assert-snapshot/serialize.mjs | 11 ++ .../assert-snapshot/serialize.snapshot | 11 ++ test/fixtures/assert-snapshot/single.mjs | 3 + .../assert-snapshot/value-changed.mjs | 3 + .../assert-snapshot/value-changed.snapshot | 2 + test/parallel/test-assert-snapshot.mjs | 133 ++++++++++++++++++ 20 files changed, 380 insertions(+) create mode 100644 lib/internal/assert/snapshot.js create mode 100644 test/fixtures/assert-snapshot/basic.mjs create mode 100644 test/fixtures/assert-snapshot/multiple.mjs create mode 100644 test/fixtures/assert-snapshot/non-existing-name.mjs create mode 100644 test/fixtures/assert-snapshot/non-existing-name.snapshot create mode 100644 test/fixtures/assert-snapshot/random.mjs create mode 100644 test/fixtures/assert-snapshot/random.snapshot create mode 100644 test/fixtures/assert-snapshot/serialize.mjs create mode 100644 test/fixtures/assert-snapshot/serialize.snapshot create mode 100644 test/fixtures/assert-snapshot/single.mjs create mode 100644 test/fixtures/assert-snapshot/value-changed.mjs create mode 100644 test/fixtures/assert-snapshot/value-changed.snapshot create mode 100644 test/parallel/test-assert-snapshot.mjs diff --git a/doc/api/assert.md b/doc/api/assert.md index 4958e6dd7df474..34c0e47ecf0b52 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -2006,6 +2006,32 @@ argument, then `error` is assumed to be omitted and the string will be used for example in [`assert.throws()`][] carefully if using a string as the second argument gets considered. +## `assert.snapshot(value, name)` + + + +> Stability: 1 - Experimental + +* `value` {any} the value to snapshot +* `name` {string} the name of 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`][]. + +If the snapshot file does not exist, the snapshot is written. + +In case it is needed to force a snapshot update, +use [`--update-assert-snapshot`][]; + +By default, a snapshot is read and written to a file, +using the same name as the main entrypoint with `.snapshot` as the extension. + ## `assert.strictEqual(actual, expected[, message])` + +Force updating snapshot files for [`assert.snapshot()`][] + ### `--use-bundled-ca`, `--use-openssl-ca`