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
640 stars 70 forks source link

feat: Driver with JSONPath support #191

Closed chr-hertel closed 4 weeks ago

chr-hertel commented 5 months ago

Implementing JsonPathDriver - resolves #190

With this feature it is possible to have changing values in a JSON snapshot and add regex assertions on those values.

Implementation

Examples

Example 1: Simple Document

image

The corresponding test code would look like this:

self::assertMatchesJsonPathSnapshot($json, [
    '$.id' => '@\d+@',
    '$.cover' => '@https://bucket.foo/bar/\d+.[webp|jpg]@',
    '$.createdAt' => '@\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+\-]\d{2}:\d{2}@',
])

Example 2: json:api

image

The corresponding test code would look like this:

self::assertMatchesJsonPathSnapshot($json, [
    '$.data..id' => '@\d+@',
    '$.data..links.*' => '@http://example.com/articles/\d+(/[a-z/]+)?@',
    '$.included..id' => '@\d+@',
    '$.included..links.self' => '@http://example.com/(people|comments)/\d+@',
])
4lxndr commented 5 months ago

Great proposal. Thanks.

freekmurze commented 5 months ago

The tests seem to fail, could you take a look at that?

chr-hertel commented 5 months ago

oh snap, yes, i will

seg fault 👀 😆

chr-hertel commented 5 months ago

that's pretty weird ... running on ubuntu php8.3 with the same command:

image

doesn't look like this thing is flaky, so it's most likely my change ... can't wrap my head around it - need to give it another thought later. added some docs though & did a rebase. was easier, ha.

chr-hertel commented 5 months ago

@freekmurze would you mind to just approve another run for the pipeline?

i set up a seperate repo with the same pipeline and all phpunit jobs passed: Actions: https://github.com/chr-hertel/json-assert-test/actions/runs/8636697242 PR: https://github.com/chr-hertel/json-assert-test/pull/2

chr-hertel commented 5 months ago

Found another issue while testing - need to extend this further

spatie-bot commented 4 weeks ago

Dear contributor,

because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it.