Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitsolanki24_'s avatar

How can I submit Ajax form with LARAVEL unit test

Hey all, I want to test an AJAX form in laravel but I don't now how to test ajax form.

Image

https://ibb.co/MgJF71K

Please help me.

0 likes
9 replies
s4muel's avatar

use postJson() and check the response, as in the example from documentation: https://laravel.com/docs/11.x/http-tests#testing-json-apis

public function test_making_an_api_request(): void
    {
        $response = $this->postJson('/api/user', ['name' => 'Sally']);
 
        $response
            ->assertStatus(201)
            ->assertJson([
                'created' => true,
            ]);
    }

if you need a browser interaction instead of request-response test, you will need browser test (e.g. a dusk test), read more here: https://laravel.com/docs/11.x/dusk

1 like
amitsolanki24_'s avatar

@s4muel thanks for your response. okay I thought Ajax form submission would be done by some otherway, this is done by same as api testing.

But my question is how can I check code writen inside ajax success method like redirect to another page and and how can I check js form validation before executing ajax request.

s4muel's avatar

@amitsolanki24_ start with dusk (as i mentioned in my first reply) and you will see if that covers your expectation

1 like
amitsolanki24_'s avatar

@s4muel and @tray2 thanks,

I will check what is dusk/cypress and how to use it.

Could you please you provide some useful resources.

amitsolanki24_'s avatar

@Tray2 could you please suggest me any solution to run tests faster because it take to much time.

Sometimes it's talking around 30 sec. to submit simple form.

Please or to participate in this conversation.