Files
pest/tests/Features/Tia/CoverageScopedUnknownTestFiles.php
T
Jhon Farias 2b2a1dab31 fix(tia): record test files the graph does not know during a replay (#2093)
* fix(tia): record test files the graph does not know during a replay

* fix(tia): keep coverage-scoped edges off test files the graph does not know

---------

Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-12-04 12:42:52 +05:30

100 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Support\Coverage;
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
function addTestFileThatReadsOutsideTheCoverageSource(Project $project): void
{
$project->write('support/salutation.php', <<<'PHP'
<?php
return 'visitor';
PHP);
$project->write('tests/Integration/WelcomeTest.php', <<<'PHP'
<?php
declare(strict_types=1);
use Fixture\App\Greeter;
test('welcomes a visitor', function (): void {
$name = require dirname(__DIR__, 2).'/support/salutation.php';
expect((new Greeter)->greet($name))->toBe('Hello, visitor!');
});
PHP);
$project->git()->commit('add a test file the graph does not know');
$project->seed('master');
}
function forgetWelcomeTest(Project $project): void
{
$project->mutateGraph(function (array $graph): array {
unset($graph['edges']['tests/Integration/WelcomeTest.php']);
return $graph;
});
}
test('a coverage replay does not narrow the edges of a test file the graph does not know', function (array $arguments): void {
$project = Project::make('master');
addTestFileThatReadsOutsideTheCoverageSource($project);
$baseline = $project->pest('--tia', '--coverage', ...$arguments);
expect($baseline->exitCode)->toBe(0, $baseline->describe());
forgetWelcomeTest($project);
$coverage = $project->pest('--tia', '--coverage', ...$arguments);
expect($coverage->exitCode)->toBe(0, $coverage->describe())
->and($coverage->uncached())->toBe(1, $coverage->describe())
->and($project->graph()['edges'] ?? [])->not->toHaveKey('tests/Integration/WelcomeTest.php');
$project->write('support/salutation.php', <<<'PHP'
<?php
return 'stranger';
PHP);
$project->git()->commit('change the salutation');
$result = $project->pest('--tia', ...$arguments);
expect($result->exitCode)->toBe(1, $result->describe());
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
test('a coverage replay with affected test files does not mark a test file the graph does not know as known', function (array $arguments): void {
$project = Project::make('master');
addTestFileThatReadsOutsideTheCoverageSource($project);
$baseline = $project->pest('--tia', '--coverage', ...$arguments);
expect($baseline->exitCode)->toBe(0, $baseline->describe());
forgetWelcomeTest($project);
$project->write('app/Calculator.php', str_replace(
'return $a + $b;',
'return ($a + $b);',
(string) file_get_contents($project->path('app/Calculator.php')),
));
$project->git()->commit('touch the calculator');
$coverage = $project->pest('--tia', '--coverage', ...$arguments);
expect($coverage->exitCode)->toBe(0, $coverage->describe())
->and($coverage->affected())->toBeGreaterThan(0, $coverage->describe())
->and($coverage->uncached())->toBe(1, $coverage->describe())
->and($project->graph()['edges'] ?? [])->not->toHaveKey('tests/Integration/WelcomeTest.php');
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');