From e45d3b1d87be50492f2b7b3992b3870fad24e3cf Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 2 Aug 2022 08:08:02 +0300 Subject: [PATCH 01/12] assert: add assert.Snapshot --- doc/api/assert.md | 28 ++++ doc/api/errors.md | 7 + lib/assert.js | 3 + lib/internal/assert/snapshot.js | 123 ++++++++++++++++ lib/internal/errors.js | 2 + test/fixtures/assert-snapshot/basic.mjs | 4 + test/fixtures/assert-snapshot/basic.snapshot | 5 + test/fixtures/assert-snapshot/named.mjs | 4 + test/fixtures/assert-snapshot/named.snapshot | 5 + .../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/same-name.mjs | 4 + test/fixtures/assert-snapshot/serialize.mjs | 9 ++ .../assert-snapshot/serialize.snapshot | 5 + test/fixtures/assert-snapshot/single.mjs | 3 + test/fixtures/assert-snapshot/single.snapshot | 2 + .../assert-snapshot/value-changed.mjs | 3 + .../assert-snapshot/value-changed.snapshot | 2 + test/parallel/test-assert-snapshot.mjs | 136 ++++++++++++++++++ 21 files changed, 370 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/basic.snapshot create mode 100644 test/fixtures/assert-snapshot/named.mjs create mode 100644 test/fixtures/assert-snapshot/named.snapshot 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/same-name.mjs 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/single.snapshot 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..8a2c4b63db54ba 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -2006,6 +2006,34 @@ 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` {string} the value to snapshot +* `name` {string} the name of snapshot. + in case order of snapshots is non-deterministic, + it is recommended to use named snapshots + **Default:** ordinal number of the snapshot. +* Returns: {Promise} + +reads a snapshot from a file, and compares `value` to the snapshot. +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, +set `process.env.NODE_UPDATE_SNAPSHOT=1`; + +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`