chore: uses snapshot testing in some visual testing

This commit is contained in:
Nuno Maduro
2023-06-17 13:36:39 +01:00
parent 36b585835d
commit b1c6f247e0
8 changed files with 14 additions and 31 deletions

View File

@ -76,7 +76,7 @@
"test:inline": "php bin/pest --colors=always --configuration=phpunit.inline.xml", "test:inline": "php bin/pest --colors=always --configuration=phpunit.inline.xml",
"test:parallel": "php bin/pest --colors=always --exclude-group=integration --parallel --processes=10", "test:parallel": "php bin/pest --colors=always --exclude-group=integration --parallel --processes=10",
"test:integration": "php bin/pest --colors=always --group=integration", "test:integration": "php bin/pest --colors=always --group=integration",
"update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always", "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always --update-snapshots",
"test": [ "test": [
"@test:refacto", "@test:refacto",
"@test:lint", "@test:lint",

View File

@ -107,6 +107,10 @@ final class Help implements HandlesArguments
'arg' => '--parallel', 'arg' => '--parallel',
'desc' => 'Run tests in parallel', 'desc' => 'Run tests in parallel',
], ],
[
'arg' => '--update-snapshots',
'desc' => 'Update snapshots for tests using the "toMatchSnapshot" expectation',
],
], ...$content['Execution']]; ], ...$content['Execution']];
$content['Selection'] = [[ $content['Selection'] = [[

View File

@ -34,6 +34,7 @@
EXECUTION OPTIONS: EXECUTION OPTIONS:
--parallel ........................................... Run tests in parallel --parallel ........................................... Run tests in parallel
--update-snapshots Update snapshots for tests using the "toMatchSnapshot" expectation
--process-isolation ................ Run each test in a separate PHP process --process-isolation ................ Run each test in a separate PHP process
--globals-backup ................. Backup and restore $GLOBALS for each test --globals-backup ................. Backup and restore $GLOBALS for each test
--static-backup ......... Backup and restore static properties for each test --static-backup ......... Backup and restore static properties for each test

View File

@ -1,12 +1,6 @@
<?php <?php
test('collision', function (array $arguments) { test('collision', function (array $arguments) {
$snapshot = __DIR__.'/../.snapshots/collision.txt';
if (in_array('--parallel', $arguments)) {
$snapshot = __DIR__.'/../.snapshots/collision-parallel.txt';
}
$output = function () use ($arguments) { $output = function () use ($arguments) {
$process = (new Symfony\Component\Process\Process( $process = (new Symfony\Component\Process\Process(
array_merge(['php', 'bin/pest', 'tests/Fixtures/CollisionTest.php'], $arguments), array_merge(['php', 'bin/pest', 'tests/Fixtures/CollisionTest.php'], $arguments),
@ -19,7 +13,6 @@ test('collision', function (array $arguments) {
return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
}; };
if (getenv('REBUILD_SNAPSHOTS')) {
$outputContent = explode("\n", $output()); $outputContent = explode("\n", $output());
array_pop($outputContent); array_pop($outputContent);
array_pop($outputContent); array_pop($outputContent);
@ -30,12 +23,7 @@ test('collision', function (array $arguments) {
array_pop($outputContent); array_pop($outputContent);
} }
file_put_contents($snapshot, implode("\n", $outputContent)); expect(implode("\n", $outputContent))->toMatchSnapshot();
$this->markTestSkipped('Snapshot rebuilt.');
}
expect($output())->toContain(file_get_contents($snapshot));
})->with([ })->with([
[['']], [['']],
[['--parallel']], [['--parallel']],

View File

@ -1,8 +1,6 @@
<?php <?php
test('visual snapshot of help command output', function () { test('visual snapshot of help command output', function () {
$snapshot = __DIR__.'/../.snapshots/help-command.txt';
$output = function () { $output = function () {
$process = (new Symfony\Component\Process\Process(['php', 'bin/pest', '--help'], null, ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'])); $process = (new Symfony\Component\Process\Process(['php', 'bin/pest', '--help'], null, ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true']));
@ -11,11 +9,5 @@ test('visual snapshot of help command output', function () {
return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
}; };
if (getenv('REBUILD_SNAPSHOTS')) { expect($output())->toMatchSnapshot();
file_put_contents($snapshot, $output());
$this->markTestSkipped('Snapshot rebuilt.');
}
expect($output())->toContain(file_get_contents($snapshot));
})->skipOnWindows(); })->skipOnWindows();

View File

@ -11,8 +11,6 @@ $run = function () {
$process->run(); $process->run();
// expect($process->getExitCode())->toBe(0);
return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
}; };