MohamedAbdalla's avatar

PEST Browser Test Doesn't Create Record, but Manual Testing Works

Hi everyone,

I'm running into an issue with a PEST browser test that I can't figure out.

The feature works perfectly when I test it manually in the browser, but the browser test fails because the record is never created.

Test

it('create idea', function () {

    $this->actingAs($user = User::factory()->create());

    $title = 'some test title';
    $description = 'some test description';
    $status = 'in_progress';

    visit('/ideas')
        ->click('@create-idea-button')
        ->fill('title', $title)
        ->click('@status-button-'.$status)
        ->fill('description', $description)

        ->fill('@new-step-input', 'some step description')
        ->click('@add-step-button')

        ->fill('@new-link-input', 'https://example.com')
        ->click('@add-link-button')

        ->fill('@new-link-input', 'https://example2.com')
        ->click('@add-link-button')
  
        ->click('Create')
        ->assertPathIs('/ideas');

        expect($user->ideas()->count())->toBe(1);

});

The assertion fails:

Expected: 1 Actual: 0

So it looks like the form submission never creates the Idea.

Controller

public function store(StoreIdeaRequest $request, CreateIdea $action)
{
    $action->handle($request->safe()->all());

    return to_route('idea.index')
        ->with('success', 'Idea created successfully.');
}

Action

class CreateIdea
{
    public function __construct(
        #[CurrentUser] protected User $user
    ) {}

    public function handle(array $attributes): void
    {
        $data = collect($attributes)->only([
            'title',
            'description',
            'status',
            'links',
        ])->toArray();

        if ($attributes['image'] ?? false) {
            $data['image_path'] = $attributes['image']->store('ideasImages', 'public');
        }

        $steps = collect($attributes['steps'] ?? [])
            ->map(fn ($step) => ['description' => $step]);

        DB::transaction(function () use ($steps, $data) {
            $idea = $this->user->ideas()->create($data);
            $idea->steps()->createMany($steps);
        });
    }
}

Form

The form is a normal POST form, but it uses Alpine.js to manage the dynamic steps[] and links[] inputs.

The submit button is:

<button type="submit" class="btn btn-primary">
    Create
</button>

What I've verified

  • The feature works correctly when used manually in the browser.
  • The browser test reaches assertPathIs('/ideas').
  • No Idea record is created ($user->ideas()->count() returns 0).
  • Earlier I also saw the exception: No query results for model [App\Models\Idea]

which I now suspect is just a consequence of the record never being created.

Has anyone experienced this with PEST Browser Testing? Is there something about authentication, Alpine.js updates, form submission, or browser sessions that I might be missing?

Any suggestions would be greatly appreciated.

0 likes
0 replies

Please or to participate in this conversation.