Merge pull request #1373 from clementbirkle/3.x

fix: normalize snapshot paths for tests outside the main tests directory
This commit is contained in:
nuno maduro
2025-07-26 03:58:40 +01:00
committed by GitHub
7 changed files with 69 additions and 3 deletions

View File

@ -16,6 +16,7 @@
<testsuites> <testsuites>
<testsuite name="default"> <testsuite name="default">
<directory suffix=".php">./tests</directory> <directory suffix=".php">./tests</directory>
<directory suffix=".php">./tests-external</directory>
<exclude>./tests/.snapshots</exclude> <exclude>./tests/.snapshots</exclude>
<exclude>./tests/.tests</exclude> <exclude>./tests/.tests</exclude>
<exclude>./tests/Fixtures/Inheritance</exclude> <exclude>./tests/Fixtures/Inheritance</exclude>

View File

@ -19,6 +19,7 @@ final class SnapshotRepository
* Creates a snapshot repository instance. * Creates a snapshot repository instance.
*/ */
public function __construct( public function __construct(
private readonly string $rootPath,
private readonly string $testsPath, private readonly string $testsPath,
private readonly string $snapshotsPath, private readonly string $snapshotsPath,
) {} ) {}
@ -103,7 +104,19 @@ final class SnapshotRepository
*/ */
private function getSnapshotFilename(): string private function getSnapshotFilename(): string
{ {
$relativePath = str_replace($this->testsPath, '', TestSuite::getInstance()->getFilename()); $testFile = TestSuite::getInstance()->getFilename();
if (str_starts_with($testFile, $this->testsPath)) {
// if the test file is in the tests directory
$startPath = $this->testsPath;
} else {
// if the test file is in the app, src, etc. directory
$startPath = $this->rootPath;
}
// relative path: we use substr() and not str_replace() to remove the start path
// for instance, if the $startPath is /app/ and the $testFile is /app/app/tests/Unit/ExampleTest.php, we should only remove the first /app/ from the path
$relativePath = substr($testFile, strlen($startPath));
// remove extension from filename // remove extension from filename
$relativePath = substr($relativePath, 0, (int) strrpos($relativePath, '.')); $relativePath = substr($relativePath, 0, (int) strrpos($relativePath, '.'));

View File

@ -78,6 +78,7 @@ final class TestSuite
$this->afterAll = new AfterAllRepository; $this->afterAll = new AfterAllRepository;
$this->rootPath = (string) realpath($rootPath); $this->rootPath = (string) realpath($rootPath);
$this->snapshots = new SnapshotRepository( $this->snapshots = new SnapshotRepository(
$this->rootPath,
implode(DIRECTORY_SEPARATOR, [$this->rootPath, $this->testPath]), implode(DIRECTORY_SEPARATOR, [$this->rootPath, $this->testPath]),
implode(DIRECTORY_SEPARATOR, ['.pest', 'snapshots']), implode(DIRECTORY_SEPARATOR, ['.pest', 'snapshots']),
); );

View File

@ -0,0 +1,35 @@
<?php
use Pest\TestSuite;
beforeEach(function () {
$this->snapshotable = <<<'HTML'
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>
HTML;
});
test('pass with dataset', function ($data) {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get();
expect($filename)->toStartWith('tests/.pest/snapshots-external/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap')
->and($this->snapshotable)->toMatchSnapshot();
})->with(['my-datas-set-value']);
describe('within describe', function () {
test('pass with dataset', function ($data) {
TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get();
expect($filename)->toStartWith('tests/.pest/snapshots-external/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap')
->and($this->snapshotable)->toMatchSnapshot();
});
})->with(['my-datas-set-value']);

View File

@ -0,0 +1,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>

View File

@ -0,0 +1,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>

View File

@ -74,7 +74,8 @@ test('pass with dataset', function ($data) {
TestSuite::getInstance()->snapshots->save($this->snapshotable); TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get(); [$filename] = TestSuite::getInstance()->snapshots->get();
expect($filename)->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') expect($filename)->toStartWith('tests/.pest/snapshots/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap')
->and($this->snapshotable)->toMatchSnapshot(); ->and($this->snapshotable)->toMatchSnapshot();
})->with(['my-datas-set-value']); })->with(['my-datas-set-value']);
@ -83,7 +84,8 @@ describe('within describe', function () {
TestSuite::getInstance()->snapshots->save($this->snapshotable); TestSuite::getInstance()->snapshots->save($this->snapshotable);
[$filename] = TestSuite::getInstance()->snapshots->get(); [$filename] = TestSuite::getInstance()->snapshots->get();
expect($filename)->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') expect($filename)->toStartWith('tests/.pest/snapshots/')
->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap')
->and($this->snapshotable)->toMatchSnapshot(); ->and($this->snapshotable)->toMatchSnapshot();
}); });
})->with(['my-datas-set-value']); })->with(['my-datas-set-value']);