SnapshotAssertions 0.6.0
Prefix Reserveddotnet add package SnapshotAssertions --version 0.6.0
NuGet\Install-Package SnapshotAssertions -Version 0.6.0
<PackageReference Include="SnapshotAssertions" Version="0.6.0" />
<PackageVersion Include="SnapshotAssertions" Version="0.6.0" />
<PackageReference Include="SnapshotAssertions" />
paket add SnapshotAssertions --version 0.6.0
#r "nuget: SnapshotAssertions, 0.6.0"
#:package SnapshotAssertions@0.6.0
#addin nuget:?package=SnapshotAssertions&version=0.6.0
#tool nuget:?package=SnapshotAssertions&version=0.6.0
SnapshotAssertions
Scope: Test projects only. Not intended for production code.
Framework-agnostic core for text-snapshot assertions.
For TUnit test projects, install
SnapshotAssertions.TUnitinstead; this package comes with it transitively.SnapshotAssertionsis the framework-agnostic shared engine; framework-specific adapter packages add the assertion entry points each test framework expects.
What's in this package
SnapshotComparer: string-against-file comparison with line-ending, BOM, trailing-whitespace, and trailing-newline options.SnapshotFileResolver: TestContext-aware default file naming (Snapshots/{TestClass}.{TestMethod}.expected.txt) and explicit-path resolution.SnapshotAcceptMode: env-var-driven accept logic (SNAPSHOT_ACCEPT=1writes the actual content over the expected baseline) with aCI=trueguard so accidental pipeline acceptance is impossible.SnapshotOptions: line-ending, BOM, whitespace, and trailing-newline configuration with strict defaults, plus a caller-suppliedWithNormalizer(Func<string, string>)pre-comparison transform (v0.5.0+).LineDiffRenderer: terminal line-based diff display, truncated to the first 20 differing lines for very large diffs (LineDiffRenderer.MaxDifferingLines = 20).SnapshotScrubber+Scrubbersfactory (v0.2.0+): text-transform pipeline that replaces volatile substrings (GUIDs, ISO 8601 timestamps, Unix-epoch-millis numbers, custom regex matches) with stable indexed tokens before comparison. Built-ins (Scrubbers.Guid,Scrubbers.Iso8601Timestamp,Scrubbers.UnixEpochMillis, plusScrubbers.GuidNandScrubbers.ElapsedMs(v0.4.0+)), curated chains (Scrubbers.Defaultfor the v0.2.0 three-pattern set,Scrubbers.Common(v0.4.0+) for the extended five-pattern set), twoScrubbers.Patternoverloads for custom regex (flat literal token) plus twoScrubbers.IndexedPatternoverloads (v0.5.0+) for the indexed<kind:N>counterpart that keeps recurring matched values correlated, andSnapshotScrubberStatefor stable indexed-token assignment across recurring values.Scrubbers.Combine(params SnapshotScrubber[])(v0.3.0+) assembles an array of scrubbers into a single composite for reusable bundles.DiffSuggestion+DiffSuggestionAnalyzer(v0.4.0+): scans a snapshot-mismatch diff for known volatile patterns and surfaces scrubber recommendations. Integrated intoSnapshotResult.Describe()so failure messages include a top-3-capped "Suggestion(s)" section pointing at applicable built-ins.SnapshotAssertions.Render.SnapshotRenderer<T>+Renderer.For<T>(v0.4.0+): abstract base class and inline factory for projecting domain types to canonical strings before snapshot comparison. Adapter packages consume these via their ownMatchesSnapshot<T>overloads.
Test-framework adapters
| Package | Test framework | Status |
|---|---|---|
SnapshotAssertions.TUnit |
TUnit | Available now |
SnapshotAssertions.NUnit |
NUnit | Possible if there is demand |
SnapshotAssertions.xUnit |
xUnit | Possible if there is demand |
SnapshotAssertions.MSTest |
MSTest | Possible if there is demand |
If you'd find a non-TUnit adapter useful, open a feature request: adapters are not built proactively.
Installation
dotnet add package SnapshotAssertions.TUnit
SnapshotAssertions comes transitively. You don't need to install it directly unless you're building your own adapter package.
Stability
The public surfaces above are semver-bound. Breaking changes require a major version bump. The exact text format of line-based diff output is not stable and may gain extra detail or change formatting in any release.
License
MIT. Copyright (c) 2026 John Verheij.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SnapshotAssertions:
| Package | Downloads |
|---|---|
|
SnapshotAssertions.TUnit
TUnit-native text-snapshot assertions using [AssertionExtension]. Provides Assert.That(actual).MatchesSnapshot() / .MatchesSnapshotFile() with line-based diff display, accept-via-environment-variable workflow, and CI guard. AOT-compatible, trimmable, no reflection. Designed for API-surface verification and small text snapshots; coexists with Verify for object-graph cases. |
GitHub repositories
This package is not used by any popular GitHub repositories.
View the rendered release notes: https://github.com/JohnVerheij/SnapshotAssertions.TUnit/releases/tag/v0.6.0
Minor release. Adds a public source-tree resolution helper used by accept-mode, plus two behavior fixes to the baseline-generation path. Both fixes correct cases where a first-run or accepted baseline ended up in a form the next comparison would not read back as written.
### Added
- **`SnapshotFileResolver.TryResolveSourceSnapshotsDirectory(string startDirectory)`** walks up from a runtime directory (typically `AppContext.BaseDirectory`) to the nearest ancestor project directory and returns its `Snapshots/` folder, or `null` when no source tree is present. Accept-mode uses it to write an accepted baseline into the committable source tree rather than the runtime copy under `bin/`. Additive; validated against the `0.5.0` ApiCompat baseline.
### Fixed
- **`SnapshotOptions.WithNormalizer` baseline candidate** is now written in normalized form. Previously, on a first run with no baseline (and on accept-mode), the persisted candidate was the raw, un-normalized subject, so a consumer who accepted it committed un-canonicalized or unscrubbed volatile text that the very next comparison normalized away. The candidate (the `.actual.txt` a consumer renames to accept, and the `.expected.txt` accept-mode overwrites) now carries the same option-driven normalized form the comparison reads back, matching how Verify scrubs before writing its `.received` file.
- **`SNAPSHOT_ACCEPT=1` accept target** now resolves to the source-tree `Snapshots/` directory rather than the runtime copy under `bin/`. Previously, under `dotnet test --no-build`, accepting wrote the `.expected.txt` next to the runtime location, so it never reached the committable `tests/.../Snapshots/` source folder and CI later reported the baseline as missing. Accept-mode now walks up from `AppContext.BaseDirectory` to the nearest ancestor project directory (the folder the build's include glob is relative to) and writes there, so accepting lands the baseline where it is committed and read from. The new `SnapshotFileResolver.TryResolveSourceSnapshotsDirectory(string)` helper performs this resolution and falls back to the runtime directory when no source tree is present. The read path is unchanged; only the accept write target moves.