spatie / phpunit-snapshot-assertions

A way to test without writing actual test cases
https://spatie.be/courses/testing-laravel-with-pest/snapshot-testing
MIT License
639 stars 70 forks source link

Assertions broken for tests with dataprovider #161

Closed l-x closed 1 year ago

l-x commented 1 year ago

Generating (and asserting) snapshots is broken for tests that use dataproviders in 5.0.0:

PHP 8.2.1 phpunit/phpunit:10.0.7 spatie/phpunit-snapshot-assertions:5.0.0

Testcase

<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

class SnapshotTest extends TestCase
{
    use MatchesSnapshots;

    public static function dataProvider(): Generator
    {
        yield [1];
        yield [2];
    }

    /** @dataProvider dataProvider */
    public function testSomething(int $value): void
    {
        $this->assertMatchesSnapshot($value);
    }
}

Result for 4.2.16 (expected)

PHPUnit 9.5.28 by Sebastian Bergmann and contributors.

II                                                                  2 / 2 (100%)

Time: 00:00.005, Memory: 6.00 MB

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 2, Incomplete: 2.

Result for 5.0.0 (actual)

PHPUnit 10.0.7 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.1

.F                                                                  2 / 2 (100%)

Time: 00:00.045, Memory: 8.00 MB

There was 1 failure:

1) SnapshotTest::testSomething with data set #1
Failed asserting that two strings are equal.

Snapshots can be updated by passing `-d --update-snapshots` through PHPUnit's CLI arguments.
--- Expected
+++ Actual
@@ @@
-'1'
+'2'

[redacted]

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
freekmurze commented 1 year ago

Thanks for reporting. Could you PR a failing test for us to look at?

If you already know the fix PR that as well 👍

freekmurze commented 1 year ago

This has hopefully been fixed by #163