spatie/phpunit-snapshot-assertions

Add ability to have multiple snapshots per test

Closed this issue · 10 comments

Currently if your test has a data provider, or you try to call matchesSnapshot multiple times per test, it doesn't work. This is one of the things I miss from our library, it would be great if this was a supported scenario. Example:

/**
 * @dataProvider provideSomeData
 */
public function testSomething(string $someData)
{
    $this->assertMatchesSnapshot('foo'.$someData);
}

This would fail because the snapshots are named from the test method, so they overwrite each other.

As a tie in to this, the ability to label the snapshots like it is possible in Jest would be very useful for those cases, maybe with a separate method or something:

/**
 * @dataProvider provideSomeData
 */
public function testSomething(string $someData)
{
    $this->assertMatchesSnapshotNamed($someData, 'foo'.$someData);
}

In said library it would yield something like that

How does your library deal with generating a unique name based on the dataProvider's contents?

Or doest it not work out of the box, and you're using names snapshots for it?

I'm just using sequential keys, 0, 1, 2 like Jest does. This is the relevant method

What's different with this then?

protected $snapshotIncrementor;

Or are you on an older version that doesn't do that yet?

That solution works for multiple snapshots in a singel testcase, but not for dataProvider (I assume the latter spins up a new instance for each test)

Hm, I think I might be on an older version indeed. I don't recall having issues with data providers on my lib but it's starting to be a while back, will investigate more.

Gonna close this due to inactivity, if anyone's having smilar issues, feel free to reopen!

Hi there, thanks for very useful tool, I'm having difficulties though with multiple snapshots per test.
I can add many snapshots in 1 test, but to get all snapshots generated I need to run test as many times as many snapshots assertions I've put in the test - the test is marked as incomplete after running the first assertion and doesn't run next ones.
Is there something I'm doing wrong or it's just missing functionality? Jest is handling this properly

Resolved this in a new version, see #43 for details :)

Awesome, works like a charm! Thank you