The assertSnapshot function will create a snapshot of a value and compare it
to a reference snapshot, which is stored alongside the test file in the
__snapshots__ directory.
Calling assertSnapshot in a test will throw an AssertionError, causing the
test to fail, if the snapshot created during the test does not match the one in
the snapshot file.
Updating Snapshots:
When adding new snapshot assertions to your test suite, or when intentionally
making changes which cause your snapshots to fail, you can update your snapshots
by running the snapshot tests in update mode. Tests can be run in update mode by
passing the --update or -u flag as an argument when running the test. When
this flag is passed, then any snapshots which do not match will be updated.
denotest--allow-all----update
Additionally, new snapshots will only be created when this flag is present.
Permissions:
When running snapshot tests, the --allow-read permission must be enabled, or
else any calls to assertSnapshot will fail due to insufficient permissions.
Additionally, when updating snapshots, the --allow-write permission must also
be enabled, as this is required in order to update snapshot files.
The assertSnapshot function will only attempt to read from and write to
snapshot files. As such, the allow list for --allow-read and --allow-write
can be limited to only include existing snapshot files, if so desired.
Options:
The assertSnapshot function optionally accepts an options object.
When configuring default options like this, the resulting assertSnapshot
function will function the same as the default function exported from the
snapshot module. If passed an optional options object, this will take precedence
over the default options, where the value provided for an option differs.
It is possible to "extend" an assertSnapshot function which has been
configured with default options.
Deno.test("isSnapshotMatch", asyncfunction (t): Promise<void> { consta = "\x1b[32mThis green text has had its colors stripped\x1b[39m"; awaitassertMonochromeSnapshot(t, a); });
snapshot["isSnapshotMatch 1"] = "This green text has had its colors stripped";
Version Control:
Snapshot testing works best when changes to snapshot files are committed
alongside other code changes. This allows for changes to reference snapshots to
be reviewed along side the code changes that caused them, and ensures that when
others pull your changes, their tests will pass without needing to update
snapshots locally.
A snapshotting library.
The
assertSnapshotfunction will create a snapshot of a value and compare it to a reference snapshot, which is stored alongside the test file in the__snapshots__directory.Calling
assertSnapshotin a test will throw anAssertionError, causing the test to fail, if the snapshot created during the test does not match the one in the snapshot file.Updating Snapshots:
When adding new snapshot assertions to your test suite, or when intentionally making changes which cause your snapshots to fail, you can update your snapshots by running the snapshot tests in update mode. Tests can be run in update mode by passing the
--updateor-uflag as an argument when running the test. When this flag is passed, then any snapshots which do not match will be updated.Additionally, new snapshots will only be created when this flag is present.
Permissions:
When running snapshot tests, the
--allow-readpermission must be enabled, or else any calls toassertSnapshotwill fail due to insufficient permissions. Additionally, when updating snapshots, the--allow-writepermission must also be enabled, as this is required in order to update snapshot files.The
assertSnapshotfunction will only attempt to read from and write to snapshot files. As such, the allow list for--allow-readand--allow-writecan be limited to only include existing snapshot files, if so desired.Options:
The
assertSnapshotfunction optionally accepts an options object.You can also configure default options for
assertSnapshot.When configuring default options like this, the resulting
assertSnapshotfunction will function the same as the default function exported from the snapshot module. If passed an optional options object, this will take precedence over the default options, where the value provided for an option differs.It is possible to "extend" an
assertSnapshotfunction which has been configured with default options.Version Control:
Snapshot testing works best when changes to snapshot files are committed alongside other code changes. This allows for changes to reference snapshots to be reviewed along side the code changes that caused them, and ensures that when others pull your changes, their tests will pass without needing to update snapshots locally.