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
@@ -0,0 +1,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>
@@ -0,0 +1 @@
the same on every attempt
@@ -0,0 +1 @@
the same on every attempt
+15 -1
View File
@@ -439,6 +439,7 @@
✓ interceptor can be filter the expected parameter as well
✓ interceptor works with negated expectation
✓ intercept can add new parameters to the expectation
✓ named arguments reach the pipes of an expectation
PASS Tests\Features\Expect\ray
✓ ray calls do not fail when ray is not installed
@@ -1122,6 +1123,17 @@
✓ multiple snapshot expectations with repeat @ repetition 8 of 10
✓ multiple snapshot expectations with repeat @ repetition 9 of 10
✓ multiple snapshot expectations with repeat @ repetition 10 of 10
✓ pass with named snapshot
✓ named snapshots do not depend on the order they are asserted in
✓ named snapshots do not consume the ordinal of the unnamed ones
✓ named snapshots require a name
✓ ordinal snapshots start over once forgotten
✓ ordinal snapshots start over on every repetition @ repetition 1 of 3
✓ ordinal snapshots start over on every repetition @ repetition 2 of 3
✓ ordinal snapshots start over on every repetition @ repetition 3 of 3
✓ snapshots of a repeated test are recorded per repetition @ repetition 1 of 3
✓ snapshots of a repeated test are recorded per repetition @ repetition 2 of 3
✓ snapshots of a repeated test are recorded per repetition @ repetition 3 of 3
PASS Tests\Features\Expect\toStartWith
✓ pass
@@ -1224,6 +1236,8 @@
✓ it does not retry fails()
✓ it retries unexpected exceptions even with throws set
✓ it does not leak mock objects between retries
✓ it resolves the same snapshot ordinals on every retry
✓ it matches the same snapshot on every retry
✓ it does not stop retrying when snapshot changes are absent
✓ it does not leak dynamic properties between retries
! it clears output buffer between retries when expectOutputString is used → Creation of dynamic property PHPUnit\Framework\TestCase\OutputBuffer::$expectedString is deprecated
@@ -2237,4 +2251,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1582 passed (3435 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1596 passed (3481 assertions)
+11
View File
@@ -239,3 +239,14 @@ test('intercept can add new parameters to the expectation', function (): void {
expect('Foo')->toBe('foo', $ignoreCase);
});
test('named arguments reach the pipes of an expectation', function () use ($state): void {
$char = new Char('A');
$state->reset();
expect($char)->toBe(expected: new Char('A'))
->and($state)
->runCount->toHaveKey('char', 1)
->appliedCount->toHaveKey('char', 1);
});
+76 -11
View File
@@ -16,7 +16,7 @@ beforeEach(function (): void {
});
test('pass', function (): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
expect($this->snapshotable)->toMatchSnapshot();
});
@@ -39,7 +39,7 @@ test('pass using pipes', function (): void {
});
test('pass with `__toString`', function (): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
$object = new class($this->snapshotable)
{
@@ -55,7 +55,7 @@ test('pass with `__toString`', function (): void {
});
test('pass with `toString`', function (): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
$object = new class($this->snapshotable)
{
@@ -71,8 +71,8 @@ test('pass with `toString`', function (): void {
});
test('pass with dataset', function ($data): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get();
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
$filename = TestSuite::getInstance()->snapshots->current()->path();
expect($filename)->toStartWith('tests/.pest/snapshots/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value___.snap')
@@ -81,8 +81,8 @@ test('pass with dataset', function ($data): void {
describe('within describe', function (): void {
test('pass with dataset', function ($data): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get();
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
$filename = TestSuite::getInstance()->snapshots->current()->path();
expect($filename)->toStartWith('tests/.pest/snapshots/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value___.snap')
@@ -91,7 +91,7 @@ describe('within describe', function (): void {
})->with(['my-datas-set-value']);
test('pass with `toArray`', function (): void {
TestSuite::getInstance()->snapshots->save(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
TestSuite::getInstance()->snapshots->current()->write(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
$object = new class($this->snapshotable)
{
@@ -109,7 +109,7 @@ test('pass with `toArray`', function (): void {
});
test('pass with array', function (): void {
TestSuite::getInstance()->snapshots->save(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
TestSuite::getInstance()->snapshots->current()->write(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
expect([
'key' => $this->snapshotable,
@@ -117,7 +117,7 @@ test('pass with array', function (): void {
});
test('pass with `toSnapshot`', function (): void {
TestSuite::getInstance()->snapshots->save(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
TestSuite::getInstance()->snapshots->current()->write(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));
$object = new class($this->snapshotable)
{
@@ -135,7 +135,7 @@ test('pass with `toSnapshot`', function (): void {
});
test('not failures', function (): void {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
TestSuite::getInstance()->snapshots->current()->write($this->snapshotable);
expect($this->snapshotable)->not->toMatchSnapshot();
})->throws(ExpectationFailedException::class);
@@ -161,3 +161,68 @@ test('multiple snapshot expectations with repeat', function (): void {
expect('foo bar 1')->toMatchSnapshot()
->and('foo bar 2')->toMatchSnapshot();
})->repeat(10);
test('pass with named snapshot', function (): void {
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->named('header')->write($this->snapshotable);
expect($snapshots->named('header')->path())
->toStartWith('tests/.pest/snapshots/')
->toEndWith('pass_with_named_snapshot__header.snap')
->and($this->snapshotable)->toMatchSnapshot(as: 'header');
});
test('named snapshots do not depend on the order they are asserted in', function (): void {
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->named('first')->write('foo bar 1');
$snapshots->named('second')->write('foo bar 2');
expect('foo bar 2')->toMatchSnapshot(as: 'second')
->and('foo bar 1')->toMatchSnapshot(as: 'first');
});
test('named snapshots do not consume the ordinal of the unnamed ones', function (): void {
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->current()->write('foo bar 1');
$snapshots->named('named')->write('foo bar 2');
expect('foo bar 1')->toMatchSnapshot()
->and('foo bar 2')->toMatchSnapshot(as: 'named')
->and($snapshots->current()->path())->toEndWith('named_snapshots_do_not_consume_the_ordinal_of_the_unnamed_ones.snap');
});
test('named snapshots require a name', function (): void {
TestSuite::getInstance()->snapshots->named('_');
})->throws(InvalidArgumentException::class, 'The snapshot name must contain at least one alphanumeric character.');
test('ordinal snapshots start over once forgotten', function (): void {
$snapshots = TestSuite::getInstance()->snapshots;
$first = $snapshots->next()->path();
expect($snapshots->next()->path())->toEndWith('ordinal_snapshots_start_over_once_forgotten__2.snap');
$snapshots->forget();
expect($snapshots->next()->path())->toBe($first);
});
test('ordinal snapshots start over on every repetition', function (): void {
$snapshots = TestSuite::getInstance()->snapshots;
$first = $snapshots->next()->path();
expect($first)->toContain('ordinal_snapshots_start_over_on_every_repetition')
->and($snapshots->next()->path())->toBe(str_replace('.snap', '__2.snap', $first));
})->repeat(3);
test('snapshots of a repeated test are recorded per repetition', function (int $iteration): void {
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->current()->write('foo bar');
$snapshots->named('named')->write('foo bar');
expect('foo bar')->toMatchSnapshot()
->and($snapshots->current()->path())->toEndWith("_with_data_set___{$iteration}__.snap")
->and('foo bar')->toMatchSnapshot(as: 'named')
->and($snapshots->named('named')->path())->toEndWith("_with_data_set___{$iteration}____named.snap");
})->repeat(3);
+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;
+2 -2
View File
@@ -26,13 +26,13 @@ test('parallel', function () use ($run): void {
$file = file_get_contents(__FILE__);
$file = preg_replace(
'/\$expected = \'.*?\';/',
"\$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1564 passed (3380 assertions)';",
"\$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1578 passed (3426 assertions)';",
$file,
);
file_put_contents(__FILE__, $file);
}
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1564 passed (3380 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1578 passed (3426 assertions)';
expect($output)
->toContain("Tests: {$expected}")