Settings
editInlineSnapshotSettings holds everything that controls an assertion: the serializer, the comparer, the scrubbers, how the snapshot is written in the source file, and the update strategy. InlineSnapshot.Validate uses InlineSnapshotSettings.Default unless you pass settings.
Configure every test
Replace InlineSnapshotSettings.Default before any test runs. A module initializer runs when the test assembly is loaded (ModuleInitializer is in the System.Runtime.CompilerServices namespace):
internal static class AssemblyInitializer
{
[ModuleInitializer]
public static void Initialize()
{
InlineSnapshotSettings.Default = InlineSnapshotSettings.Default with
{
SnapshotUpdateStrategy = SnapshotUpdateStrategy.MergeTool,
MergeTools = [MergeTool.VisualStudioCode],
};
}
}InlineSnapshotSettings is a record: with creates a copy, with its own list of scrubbers. Clone() does the same.
Change the settings of one assertion
Pass settings to Validate:
var data = new { FirstName = "Gérald", LastName = "Barré" };
// InlineSnapshotSettings is a record, so you can use the "with" keyword to create a new instance
var settings = InlineSnapshotSettings.Default with
{
SnapshotUpdateStrategy = SnapshotUpdateStrategy.Overwrite,
};
InlineSnapshot.Validate(data, settings, """
FirstName: Gérald
LastName: Barré
""");Or use the builder syntax, with an instance of the settings:
InlineSnapshot
.WithSettings(settings)
.Validate(data, """
FirstName: Gérald
LastName: Barré
""");Or with a function that changes a copy of the default settings:
InlineSnapshot
.WithSettings(settings => settings.SnapshotUpdateStrategy = SnapshotUpdateStrategy.Overwrite)
.Validate(data, """
FirstName: Gérald
LastName: Barré
""");InlineSnapshot.WithSettings and InlineSnapshot.WithSerializer return an InlineSnapshotBuilder; chain WithSettings and WithSerializer calls, then call Validate.
Properties
| Property | Default | Description |
|---|---|---|
| SnapshotUpdateStrategy | Disallow, or the INLINESNAPSHOTTESTING_STRATEGY environment variable | What happens when a snapshot does not match. See Updating snapshots |
| ForceUpdateSnapshots | false | Rewrites snapshots even when they match, for instance to reformat them |
| AutoDetectContinuousEnvironment | true, unless INLINESNAPSHOTTESTING_AUTODETECT_CONTINUOUS_ENVIRONMENT is false | Never updates snapshots on a build server, in a continuous testing runner or in an LLM agent. See Continuous integration |
| MergeTools | The configured and detected tools | The tools started by the merge tool strategies. See Merge tools |
| SnapshotSerializer | The human-readable serializer | Converts the value to text. See Serializers |
| Scrubbers | Empty | Rewrite the text after serialization. See Deterministic snapshots |
| SnapshotComparer | SnapshotComparer.Default | Decides whether the actual and expected values match. See Comparing snapshots |
| ErrorMessageFormatter | An inline diff | Formats the difference shown in the failure message |
| AllowedStringFormats | CSharpStringFormats.Default | The C# string literals the library may write. See String formats and source files |
| Indentation, EndOfLine, FileEncoding | null: detected from the source file | How the source file is written |
| ValidateSourceFilePathUsingPdbInfoWhenAvailable | true | Checks the file to edit against the PDB |
| ValidateLineNumberUsingPdbInfoWhenAvailable | false | Checks the line to edit against the PDB |
Comparing snapshots
SnapshotComparer.Default normalizes both values before comparing them: line endings become \n, tabs become four spaces, and lines that contain only whitespace become empty. A snapshot whose line endings or indentation were changed by an editor still matches.
Use SnapshotComparer.Ordinal when whitespace matters, for instance for generated source code, Markdown or a network protocol. It compares the values exactly, and the updated snapshots keep the exact line endings and whitespace: a raw string is used only when it gives back the exact value. See Values a format cannot write. The default serializer writes every line break as \n: to compare the line endings of a string too, use a serializer that returns it unchanged.
InlineSnapshot
.WithSettings(settings => settings.SnapshotComparer = SnapshotComparer.Ordinal)
.Validate("name:\tvalue", "name:\tvalue");To compare differently, derive from SnapshotComparer: NormalizeValue prepares each value (it returns the value unchanged by default), and AreEqual compares the normalized values. The normalized values are also the ones shown in the failure message.
Deterministic source paths
When your build uses deterministic paths, [CallerFilePath] returns a mapped path such as /_/tests/MyTests.cs. The package registers the source roots of the project from its MSBuild files, so the path is mapped back to the file on disk. You can also register a mapping yourself:
InlineSnapshot.RegisterSourceRootMapping("/_/", @"C:/agent/_work/1/s/");Environment variables
| Variable | Effect |
|---|---|
INLINESNAPSHOTTESTING_STRATEGY | The default update strategy: the case-insensitive name of a SnapshotUpdateStrategy property, such as Overwrite or mergetool |
INLINESNAPSHOTTESTING_AUTODETECT_CONTINUOUS_ENVIRONMENT | false, 0, no or off allows updates on a build server, in a continuous testing runner and in an LLM agent |
DiffEngine_Tool | The merge tool to try first, as the case-insensitive name of a MergeTool property |
DiffEngine_Disabled | true or 1 disables every merge tool |