pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: mixed-in expectation object causes IDE type errors

Open wand2016 opened this issue 1 year ago • 1 comments

What Happened

With using a mixed-in expectation object, method chaining results in IDE type errors:

        expect($response->content())
            ->toBeJson()
            ->json()
            ->not()->toHaveKey('errors')
            ->toHaveKey('data')
            ->data->toEqual(...);
             // ^ IDE type error occurs

https://github.com/pestphp/pest/blob/b33af71036ec9babcdacbc0e49db45a5152191d0/src/Mixins/Expectation.php#L60

Suggested fix:

-    * @return self<TValue>
+    * @return $this

Changing the PHPDoc return type to $this seems to be a better solution to avoid these IDE type errors.

How to Reproduce

        expect($response->content())
            ->toBeJson()
            ->json()
            ->not()->toHaveKey('errors')
            ->toHaveKey('data')
            ->data->toEqual([]);

image

The suggested fix above solves the problem:

image

Sample Repository

No response

Pest Version

2.x-dev

PHP Version

8.2.15

Operation System

Linux

Notes

pestphp/pest-plugin version is v2.1.1

wand2016 avatar Jun 07 '24 02:06 wand2016

Flagging this one @nunomaduro based on our chat.

I'm less concerned with the type errors, and more about the lack of completion of the proxied object.

expect($user)-> will provide completion for methods on the Pest\Expectation object, but nothing from the $user, which makes it possible to introduce failing tests because typos, for example, will lead to an assertion on null.

CleanShot 2024-09-11 at 10 17 31

We certainly get the @template annotations for the generics, etc., but to no avail.

CleanShot 2024-09-11 at 10 18 30

I've tried making the change as in the linked PR (#1168), but that doesn't seem to yield the expected outcome.

I'm not sure how exactly to remedy this situation, or if it's possible. Or if this is just something in my setup, though one of my colleagues has suggested he also has no completion in PhpStorm (and seems to make it chug, at that).

michaeldyrynda avatar Sep 11 '24 00:09 michaeldyrynda

Hey 👋 PhpStorm is now the ones managing the Pest plugin, so to report issues with wrong types in phpstorm, it should be done on their issue tracker instead, however thanks for opening issue!

https://youtrack.jetbrains.com/issues/WI

olivernybroe avatar Jul 26 '25 03:07 olivernybroe