JestDotnet 1.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package JestDotnet --version 1.3.1                
NuGet\Install-Package JestDotnet -Version 1.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JestDotnet" Version="1.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JestDotnet --version 1.3.1                
#r "nuget: JestDotnet, 1.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JestDotnet as a Cake Addin
#addin nuget:?package=JestDotnet&version=1.3.1

// Install JestDotnet as a Cake Tool
#tool nuget:?package=JestDotnet&version=1.3.1                

Jest Snapshot Dotnet

Simple snapshot testing with inspiration from amazing Jest library.

Installation

You can install it using Nuget.

How it works

If you are unfamiliar with snapshot testing, I recommend you to check Jest documentation.

This library works very similarly. When you run ShouldMatchSnapshot for the first time, it will generate JSON snapshot of the serialization of the object. If you run it second time, it will check if the JSON serialization of the object is the same as the saved JSON snapshot.

It is important to commit snapshots with your code to the git.

Updating snapshots

You can mass update snapshot (useful when you add new property to an object, change default value or remove a property). Simply set environment variable UPDATE to true and run the tests that you want to update. It will update all snapshots that have failed.

If you are using JetBrains Rider, go to Settings - Build, Execution, Deployment - Unit Testing - Test Runner and set it there. Remember to unset the variable again.

Continuous Integration

Snapshots are not created if you are running tests inside Continuous Integration environment (Gitlab CI, Jenkins, Teamcity, Azure/AWS Pipelines etc.). Library detects CI environment by check CI environment variable. If it is set to true, test fails if snapshot is missing.

API

Extensions methods

ShouldMatchSnapshot

public static void ShouldMatchSnapshot(this object actual, string hint = "");
Example
var person = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

person.ShouldMatchSnapshot();

ShouldMatchInlineSnapshot

public static void ShouldMatchInlineSnapshot(this object actual, string inlineSnapshot);
Example
var person = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

person.ShouldMatchInlineSnapshot(@"
    {
        ""FirstName"": ""John"",
        ""LastName"": ""Bam"",
        ""DateOfBirth"": ""2008-07-07T00:00:00"",
        ""Age"": 13,    
    }"
);

ShouldMatchObject

public static void ShouldMatchObject(this object actual, object expected);
Example
var actual = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

var expected = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

actual.ShouldMatchObject(expected);

Methods

If you don't like extension methods, you can use static class JestAssert

ShouldMatchSnapshot

public static void ShouldMatchSnapshot(object actual, string hint = "");
Example
var person = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

JestAssert.ShouldMatchSnapshot(person);

ShouldMatchInlineSnapshot

public static void ShouldMatchInlineSnapshot(object actual, string inlineSnapshot);
Example
var person = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

JestAssert.ShouldMatchInlineSnapshot(person, @"
    {
        ""FirstName"": ""John"",
        ""LastName"": ""Bam"",
        ""DateOfBirth"": ""2008-07-07T00:00:00"",
        ""Age"": 13,    
    }"
);

ShouldMatchObject

public static void ShouldMatchObject(object actual, object expected);
Example
var actual = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

var expected = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

JestAssert.ShouldMatchObject(actual,expected);

Advanced

Configuring directory and file extensions

If you need to configure it, you can use SnapshotSettings class to specify your own

  • extension instead of .snap (use SnapshotSettings.SnapshotExtension)
  • directory instead of __snapshots__ (use SnapshotSettings.SnapshotDirectory)
  • function that generates directory, extension and filename (use SnapshotSettings.CreatePath)

Popular use is to change directory of the snapshot files. You can do it like this:

SnapshotSettings.SnapshotDirectory = "__custom__";

var testObject = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

JestAssert.ShouldMatchSnapshot(testObject);

// you can return it back using
SnapshotSettings.SnapshotDirectory = SnapshotSettings.DefaultSnapshotDirectory;

Configuring serialization

For serialization, I am using Json.NET. If you need to configure it, you can use SnapshotSettings class to specify your own

  • JsonSerializer (use SnapshotSettings.CreateJsonSerializer)
  • JTokenWriter (use SnapshotSettings.CreateJTokenWriter)
  • StringWriter (use SnapshotSettings.CreateStringWriter)
  • JsonTextWriter (use SnapshotSettings.CreateJsonTextWriter).

Popular use is to change line ending of the .snap files. For example if you want to set line ending to Linux LF, you can do it like this:

SnapshotSettings.CreateStringWriter = () => new StringWriter(CultureInfo.InvariantCulture)
{
    NewLine = "\n"
};

var testObject = new Person
{
    Age = 13,
    DateOfBirth = new DateTime(2008, 7, 7),
    FirstName = "John",
    LastName = "Bam"
};

JestAssert.ShouldMatchSnapshot(testObject);

SnapshotSettings expects you define your own function that returns new configured instance.

Caveats

Dynamic objects

You cannot call neither extension nor JestAssert with dynamic object. You need to cast it to object (or real type).

Credits

Package icon

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.1 13,538 2/19/2023
1.4.0 224 2/17/2023
1.3.1 26,208 12/9/2020
1.2.0 13,827 6/26/2020
1.1.0 6,290 3/26/2020
1.0.0 449 3/9/2020