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
+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);