Settings

edit

InlineSnapshotSettings 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

PropertyDefaultDescription
SnapshotUpdateStrategyDisallow, or the INLINESNAPSHOTTESTING_STRATEGY environment variableWhat happens when a snapshot does not match. See Updating snapshots
ForceUpdateSnapshotsfalseRewrites snapshots even when they match, for instance to reformat them
AutoDetectContinuousEnvironmenttrue, unless INLINESNAPSHOTTESTING_AUTODETECT_CONTINUOUS_ENVIRONMENT is falseNever updates snapshots on a build server, in a continuous testing runner or in an LLM agent. See Continuous integration
MergeToolsThe configured and detected toolsThe tools started by the merge tool strategies. See Merge tools
SnapshotSerializerThe human-readable serializerConverts the value to text. See Serializers
ScrubbersEmptyRewrite the text after serialization. See Deterministic snapshots
SnapshotComparerSnapshotComparer.DefaultDecides whether the actual and expected values match. See Comparing snapshots
ErrorMessageFormatterAn inline diffFormats the difference shown in the failure message
AllowedStringFormatsCSharpStringFormats.DefaultThe C# string literals the library may write. See String formats and source files
Indentation, EndOfLine, FileEncodingnull: detected from the source fileHow the source file is written
ValidateSourceFilePathUsingPdbInfoWhenAvailabletrueChecks the file to edit against the PDB
ValidateLineNumberUsingPdbInfoWhenAvailablefalseChecks 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

VariableEffect
INLINESNAPSHOTTESTING_STRATEGYThe default update strategy: the case-insensitive name of a SnapshotUpdateStrategy property, such as Overwrite or mergetool
INLINESNAPSHOTTESTING_AUTODETECT_CONTINUOUS_ENVIRONMENTfalse, 0, no or off allows updates on a build server, in a continuous testing runner and in an LLM agent
DiffEngine_ToolThe merge tool to try first, as the case-insensitive name of a MergeTool property
DiffEngine_Disabledtrue or 1 disables every merge tool