fix: snapshot key on retry / repeat

This commit is contained in:
nuno maduro
2026-08-24 09:31:34 +01:00
parent 39cbdcd52b
commit b8c2265d66
30 changed files with 479 additions and 145 deletions
+36
View File
@@ -1,5 +1,6 @@
<?php
use Pest\TestSuite;
use Symfony\Component\Process\Process;
it('passes on first try', function (): void {
@@ -231,6 +232,41 @@ it('does not leak mock objects between retries', function (): void {
expect($mock->count())->toBe(1);
})->flaky(tries: 3);
it('resolves the same snapshot ordinals on every retry', function (): void {
$file = sys_get_temp_dir().'/pest_flaky_snapshot_ordinals';
$count = file_exists($file) ? (int) file_get_contents($file) : 0;
file_put_contents($file, (string) ++$count);
$snapshots = TestSuite::getInstance()->snapshots;
expect($snapshots->next()->path())->toEndWith('resolves_the_same_snapshot_ordinals_on_every_retry.snap')
->and($snapshots->next()->path())->toEndWith('resolves_the_same_snapshot_ordinals_on_every_retry__2.snap');
if ($count < 3) {
throw new Exception('Flaky snapshot ordinals');
}
@unlink($file);
})->flaky(tries: 3);
it('matches the same snapshot on every retry', function (): void {
$file = sys_get_temp_dir().'/pest_flaky_snapshot_match';
$count = file_exists($file) ? (int) file_get_contents($file) : 0;
file_put_contents($file, (string) ++$count);
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->current()->write('the same on every attempt');
expect('the same on every attempt')->toMatchSnapshot()
->and($snapshots->current()->path())->toEndWith('matches_the_same_snapshot_on_every_retry.snap');
if ($count < 2) {
throw new Exception('Flaky snapshot match');
}
@unlink($file);
})->flaky(tries: 3);
it('does not stop retrying when snapshot changes are absent', function (): void {
$file = sys_get_temp_dir().'/pest_flaky_no_snapshot';
$count = file_exists($file) ? (int) file_get_contents($file) : 0;