Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot-naming collisions in async method that verifies multiple snapshots #201

Open
N-Olbert opened this issue Oct 25, 2024 · 1 comment

Comments

@N-Olbert
Copy link

N-Olbert commented Oct 25, 2024

Describe the bug
Within a single async method, using multiple snapshots with different names does not work.

To Reproduce

[TestMethod]
public async Task TestFoo()
{
    var something = new Foo();
    var fullName1 = Snapshot.FullName(new SnapshotNameExtension("GetFoo"));
    var fullName2 = Snapshot.FullName(new SnapshotNameExtension("GetBar"));

    something.GetFoo().MatchSnapshot(fullName1);
    something.GetBar().MatchSnapshot(fullName2);  // BOOM - fails since fullname 2 is identical to fullName1

}

Expected behavior
Two different snapshots should be created and verified.

Desktop (please complete the following information):

  • OS: Windows 10
  • Version 22H2

Additional context
Using MSTest

The problem is that the entire snapshot-naming info is cached in an AsyncLocal, therfore the second Snapshot.FullName has no effect since this cache is already initialized:
https://github.com/SwissLife-OSS/snapshooter/blob/94f9c77703a05e0d03b02aba9adb68d655fab820/src/Snapshooter.MSTest/Snapshot.cs#L15C5-L17C75

@N-Olbert
Copy link
Author

It works if you do it like that:

[TestMethod]
public async Task TestFoo()
{
    var something = new Foo();
    var fullName1 = Snapshot.FullName(new SnapshotNameExtension("GetFoo"));
    something.GetFoo().MatchSnapshot(fullName1);

    var fullName2 = Snapshot.FullName(new SnapshotNameExtension("GetBar"));
    something.GetBar().MatchSnapshot(fullName2); 

}

... still odd behaviour in my opinion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant