pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: WebSocket client is not connected error thrown when using visit() in helper function

Open Admiral-Enigma opened this issue 1 year ago • 2 comments

What Happened

In pest.php I expect to be able to write a helper method for logging in via the frontend, but it seems you cant use the visit() function in a helper method, as it then throws a WebSocket client is not connected when using the helper method in your test.

Image

How to Reproduce

Create a helper method like this (simplified version)

function loginHelper()
{
    return visit(frontendRoute('login'))
        ->type('email', '[email protected]')
        ->type('password', 'Password123');
}

Then use the helper in a test

it('Can visit something using helper', function () {
    loginhelper()->assertSee('Hello World');
});

When running the test, you should see the WebSocket client is not connected. error

Sample Repository

No response

Pest Version

4.0.4

PHP Version

8.3

Operation System

macOS

Notes

Might seem weird why we do it like this, but its because our application is a more traditional SPA with token auth, so to get the tests to work we have to login via the frontend route, which works but the code is verbose, therefore it would be great if you could actually write a helper function that uses the visit function

Admiral-Enigma avatar Sep 09 '25 07:09 Admiral-Enigma

Just as a note I have also tried with

function loginHelper()
{
    return test()->visit(frontendRoute('login'))
        ->type('email', '[email protected]')
        ->type('password', 'Password123');
}

But that gives the same error

Admiral-Enigma avatar Sep 09 '25 07:09 Admiral-Enigma

Related #1439

michielkempen avatar Sep 16 '25 06:09 michielkempen