`Attempt to read property "form" on null` error in test

I'm getting this error in a test:
Attempt to read property "form" on null

at vendor/filament/forms/src/Testing/TestsForms.php:125
121▕ public function assertFormExists(): Closure
122▕ {
123▕ return function (string $name = 'form'): static {
124▕ /** @var ComponentContainer $form */
➜ 125▕ $form = $this->instance()->{$name};
Attempt to read property "form" on null

at vendor/filament/forms/src/Testing/TestsForms.php:125
121▕ public function assertFormExists(): Closure
122▕ {
123▕ return function (string $name = 'form'): static {
124▕ /** @var ComponentContainer $form */
➜ 125▕ $form = $this->instance()->{$name};
This is the test:
it("needs an existing account user", function () {
\Pest\Livewire\livewire(CompleteRegistration::class)
->fillForm([
'email' => '[email protected]',
])
->call('register')
->assertRedirect(filament()->getLoginUrl());
});
it("needs an existing account user", function () {
\Pest\Livewire\livewire(CompleteRegistration::class)
->fillForm([
'email' => '[email protected]',
])
->call('register')
->assertRedirect(filament()->getLoginUrl());
});
CompleteRegistration is a filament page:
<?php

namespace App\Filament\Pages\Auth;

use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Filament\Forms\Form;
use Filament\Http\Middleware\Authenticate;
use Filament\Pages\Page;
use Livewire\Features\SupportRedirects\Redirector;

/**
* @property-read \Filament\Forms\ComponentContainer $form
*/
class CompleteRegistration extends Page
{
use WithRateLimiting;

public ?array $data = [];

protected static string $view = 'filament.auth.complete-registration';

protected static string $layout = 'filament-panels::components.layout.simple';

protected static string | array $withoutRouteMiddleware = [
Authenticate::class,
];

public function mount(): void
{
$this->form->fill([
// defaults
]);
}

public function register(): Redirector
{
// register
}

public function form(Form $form): Form
{
return $form
->statePath('data')
->schema([
// schema
]);
}

public function hasLogo(): bool
{
return true;
}
}
<?php

namespace App\Filament\Pages\Auth;

use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Filament\Forms\Form;
use Filament\Http\Middleware\Authenticate;
use Filament\Pages\Page;
use Livewire\Features\SupportRedirects\Redirector;

/**
* @property-read \Filament\Forms\ComponentContainer $form
*/
class CompleteRegistration extends Page
{
use WithRateLimiting;

public ?array $data = [];

protected static string $view = 'filament.auth.complete-registration';

protected static string $layout = 'filament-panels::components.layout.simple';

protected static string | array $withoutRouteMiddleware = [
Authenticate::class,
];

public function mount(): void
{
$this->form->fill([
// defaults
]);
}

public function register(): Redirector
{
// register
}

public function form(Form $form): Form
{
return $form
->statePath('data')
->schema([
// schema
]);
}

public function hasLogo(): bool
{
return true;
}
}
I don't get this error on other pages. Can anyone see what I'm doing wrong?
Solution:
Turns out it was because I had a check in the mount that triggered a redirect if the user was already registered
Jump to solution
1 Reply
Solution
oddvalue
oddvalue15mo ago
Turns out it was because I had a check in the mount that triggered a redirect if the user was already registered
Want results from more Discord servers?
Add your server