Skip to main content

Accepting snapshots

A missing or different snapshot writes the actual value to <TestFile>.<Method>.awexpect-snapshot.pending.txt next to the snapshot. Review it and rename it to <TestFile>.<Method>.awexpect-snapshot.txt to accept it. The next run that matches the snapshot deletes a leftover pending file.

Add the pending files to your .gitignore, so that they are not committed by accident; the pattern also covers snapshots with another extension, e.g. .awexpect-snapshot.pending.json:

.gitignore
*.awexpect-snapshot.pending.*

To accept many snapshots at once, set the environment variable AWEXPECT_ACCEPT_SNAPSHOTS to one of the levels of SnapshotAcceptance and run the tests:

LevelVariableA missing snapshotA different snapshot
Nonenone, 0 or falsewrites the pending filewrites the pending file
Newnewis acceptedwrites the pending file
Allall, 1 or trueis acceptedis accepted

An empty or unset variable accepts nothing. New adds the snapshots of new tests without touching the reviewed ones. An accepted snapshot is replaced by the actual value, and the expectation still fails, so that it never goes unnoticed:

Failure message
Expected that description
matches the snapshot "/src/Tests/AlbumTests.Describes.awexpect-snapshot.txt",
but the snapshot did not exist

Actual:
Abbey Road by The Beatles

Accepted snapshot:
/src/Tests/AlbumTests.Describes.awexpect-snapshot.txt

The next run compares with the accepted snapshot. Review the changed snapshots in version control before committing them.

Snapshots combined with other expectations

The pending snapshot is only written, and a change only accepted, when the whole expectation fails. When the snapshot is combined with other expectations and another branch is met, e.g. .Or.IsEqualTo(…) or Expect.ThatAny(…), the test passes and nothing is written, so that a passing test never changes a snapshot.

On a build server​

The environment variable is ignored on a build server (when CI, TF_BUILD, JENKINS_URL or TEAMCITY_VERSION is set), so that a variable that was forgotten in a pipeline cannot change the snapshots there. To decide it in code instead, which also applies on a build server:

await That(description).MatchesSnapshot().Accepting(SnapshotAcceptance.All); // missing or changed
await That(description).MatchesSnapshot().Accepting(SnapshotAcceptance.New); // only missing
await That(description).MatchesSnapshot().Accepting(SnapshotAcceptance.None); // never accepts

Customize.aweXpect.Snapshots().Acceptance.Set(SnapshotAcceptance.New); // for this async flow

Accepting(…) takes precedence over Customize.aweXpect.Snapshots().Acceptance, which takes precedence over the environment variable, so Accepting(SnapshotAcceptance.None) keeps a snapshot from being accepted while the variable is set. The setting defaults to null, which leaves it to the environment variable.

To review a mismatch after a run on a build server, attach its pending file to the result of the test, when the test framework supports attachments. MSTest 4 needs the opt-in AweXpectExperimentalMSTest for it:

Customize.aweXpect.Snapshots().AttachPendingFiles.Set(true); // for this async flow