Skip to content

Commit 77cbdcd

Browse files
committed
- Rename SnapshotHelper to SnapshotTest
- Add SnapshotTestBuilder - Update file header
1 parent c54cc3d commit 77cbdcd

89 files changed

Lines changed: 1623 additions & 495 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ var response = await httpClient.GetFromJsonAsync<Person>("target");
494494

495495
### Snapshot testing
496496

497-
Snapshot testing is a powerful technique for testing text and image outputs. The `SnapshotHelper` class provided in
497+
Snapshot testing is a powerful technique for testing text and image outputs. The `SnapshotTestBuilder` class provided in
498498
`SoloX.CodeQuality.Test.Helpers` makes it easy to create and maintain snapshot tests.
499499

500500
Snapshots are stored on disk and compared against test outputs. This is useful for testing scenarios such as:
@@ -504,32 +504,30 @@ Snapshots are stored on disk and compared against test outputs. This is useful f
504504

505505
#### Basic usage
506506

507-
To use the `SnapshotHelper` in your tests, simply create an instance and use its methods to verify outputs:
507+
To use the `SnapshotTest` in your tests, simply use the `SnapshotTestBuilder` to get an instance and use its methods to verify outputs:
508508

509509
```csharp
510510
using SoloX.CodeQuality.Test.Helpers.Snapshot;
511511

512-
var snapshotHelper = new SnapshotHelper();
513-
514512
// For text snapshots
513+
var snapshotTest = SnapshotTestBuilder
514+
.Create()
515+
.WithThisFilePathLocation()
516+
.WithTextStrategy()
517+
.Build();
518+
515519
string generatedOutput = GenerateContent();
516-
await snapshotHelper.CompareTextSnapshotAsync(nameof(MyTest), generatedOutput);
520+
await snapshotTest.CompareSnapshotAsync(nameof(MyTest), generatedOutput);
517521

518522
// For image snapshots
519-
Stream generatedPngImage = GeneratePngImage();
520-
await snapshotHelper.ComparePngSnapshotAsync(nameof(MyTest), generatedImage);
521-
```
522-
523-
#### Configuration
524-
525-
You can configure the `SnapshotHelper` with custom options:
523+
var snapshotTest = SnapshotTestBuilder
524+
.Create()
525+
.WithThisFilePathLocation()
526+
.WithPngStrategy()
527+
.Build();
526528

527-
```csharp
528-
var snapshotHelper = new SnapshotHelper(options =>
529-
{
530-
options.IntermediateFolder = "CustomSnapshotsFolder";
531-
options.RootPath = "/path/to/test/project";
532-
});
529+
Stream generatedPngImage = GeneratePngImage();
530+
await snapshotTest.CompareSnapshotAsync(nameof(MyTest), generatedImage);
533531
```
534532

535533
The snapshots are stored in a `Snapshots` folder by default and can be reviewed and updated when the expected output changes.

documents/BreakingChanges.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@ The `SnapshotHelper` API has been significantly reworked with breaking changes t
99
#### Key Changes
1010
- **Moved** from `SoloX.CodeQuality.Test.Helpers.XUnit` to `SoloX.CodeQuality.Test.Helpers` project and namespace.
1111

12-
- **Dependency change**: The `SnapshotHelper` class no longer depends on xUnit-specific types, making it more flexible and reusable
12+
- **Renamed** from `SnapshotHelper` to `SnapshotTest`.
13+
14+
- **Dependency change**: The snapshot test classes no longer depends on xUnit-specific types, making it more flexible and reusable
1315
across different testing frameworks.
1416

15-
- **Static to instance**: The `SnapshotHelper` class is no longer static. You must create an instance of `SnapshotHelper` to use its methods.
17+
- **Static to builder**: The `SnapshotHelper` class is no longer static. You must use the builder to get an instance of `SnapshotTest` to use its methods.
1618

1719
- **New asynchronous API**: All snapshot methods are now async (`Task`-based) and must be awaited.
1820
- Old: `AssertSnapshot(string generated, string snapshotName, string location)`
19-
- New: `await CompareTextSnapshotAsync(string name, string content)`
21+
- New: `await CompareSnapshotAsync(string name, string content)`
2022

21-
- **PNG file snapshot support**: New support for comparing PNG image snapshots has been added via `ComparePngSnapshotAsync()`.
23+
- **PNG file snapshot support**: New support for comparing PNG image snapshots has been added via a Png Snapshot strategy.
2224
- Includes configurable `differencesThreshold` parameter for image comparison tolerance.
2325

24-
- **Configuration-based initialization**: The constructor now uses a fluent configuration pattern.
25-
- Old: location was specified as parameter of the static AssertSnapshot method.
26-
- New: `new SnapshotHelper(options => options.IntermediateFolder = intermediateFolder)`
27-
28-
- **Force replace capability**: All comparison methods now support a `forceReplaceSnapshot` parameter to update existing snapshots.
26+
- **Force replace capability**: The comparison methods now support a `forceReplaceSnapshot` parameter to update existing snapshots.
2927

3028
#### Migration Example
3129

@@ -35,17 +33,28 @@ var location = "Path where to read/write snapshot files";
3533
SnapshotHelper.AssertSnapshot("Some text to match against the snapshot", snapshotName, location);
3634

3735
// New code
38-
var helper = new SnapshotHelper(options => options.RootPath = "Root path where to read/write snapshot files");
39-
await helper.CompareTextSnapshotAsync(snapshotName, "Some text to match against the snapshot");
36+
var snapshotTest= SnapshotTestBuilder
37+
.Create()
38+
.WithLocation("Root path where to read/write snapshot files")
39+
.WithTextStrategy()
40+
.Build();
41+
42+
await snapshotTest.CompareSnapshotAsync(snapshotName, "Some text to match against the snapshot");
4043

4144
// For PNG snapshots (new capability)
42-
await helper.ComparePngSnapshotAsync(snapshotName, pngStream, differencesThreshold: 0.01);
45+
var snapshotTest= SnapshotTestBuilder
46+
.Create()
47+
.WithLocation("Root path where to read/write snapshot files")
48+
.WithPngStrategy(differencesThreshold: 0.01)
49+
.Build();
50+
51+
await snapshotTest.CompareSnapshotAsync(snapshotName, pngStream);
4352
```
4453

4554
#### Migration Steps
4655

47-
1. Setup with `SnapshotHelper` instantiation and use the configuration action.
48-
2. Change all `SnapshotHelper.AssertSnapshot()` calls to `CompareTextSnapshotAsync()` and add `await`.
56+
1. Setup with `SnapshotTestBuilder` instantiation and use the configuration action.
57+
2. Change all `SnapshotHelper.AssertSnapshot()` calls to `CompareSnapshotAsync()` and add `await`.
4958
3. Review snapshot test methods and mark them as `async Task`.
5059
4. Existing snapshot text files (`.snapshot`) are compatible; you just need to rename with (`.snapshot.ref.txt`).
5160

src/SharedProperties.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Version>3.0.0-preview.1</Version>
55
<Authors>Xavier Solau</Authors>
6-
<Copyright>Copyright © 2021 Xavier Solau</Copyright>
6+
<Copyright>Copyright © 2021-2026 Xavier Solau</Copyright>
77
<PackageLicenseExpression>MIT</PackageLicenseExpression>
88
<PackageProjectUrl>https://github.com/xaviersolau/CodeQuality</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/xaviersolau/CodeQuality.git</RepositoryUrl>

src/examples/SoloX.CodeQuality.Prod.Example/AClass2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="AClass2.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Prod.Example/Class1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="Class1.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Prod.Example/Class3.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="Class3.g.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Prod.Example/IInterface1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="IInterface1.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Prod.Example/Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="Test.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Test.Example/Class1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="Class1.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

src/examples/SoloX.CodeQuality.Test.Example/Class3.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ----------------------------------------------------------------------
22
// <copyright file="Class3.g.cs" company="Xavier Solau">
3-
// Copyright © 2021 Xavier Solau.
3+
// Copyright © 2021-2026 Xavier Solau.
44
// Licensed under the MIT license.
55
// See LICENSE file in the project root for full license information.
66
// </copyright>

0 commit comments

Comments
 (0)