JestDotnet 1.2.0
See the version list below for details.
dotnet add package JestDotnet --version 1.2.0
NuGet\Install-Package JestDotnet -Version 1.2.0
<PackageReference Include="JestDotnet" Version="1.2.0" />
paket add JestDotnet --version 1.2.0
#r "nuget: JestDotnet, 1.2.0"
// Install JestDotnet as a Cake Addin #addin nuget:?package=JestDotnet&version=1.2.0 // Install JestDotnet as a Cake Tool #tool nuget:?package=JestDotnet&version=1.2.0
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
(useSnapshotSettings.SnapshotExtension
) - directory instead of
__snapshots__
(useSnapshotSettings.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
(useSnapshotSettings.CreateJsonSerializer
)JTokenWriter
(useSnapshotSettings.CreateJTokenWriter
)StringWriter
(useSnapshotSettings.CreateStringWriter
)JsonTextWriter
(useSnapshotSettings.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).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- JsonDiffPatch.Net (>= 2.1.0)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.