Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
write-pest-test
alizharb/agent-skills
claude-code
pest
testing
phpunit
mocking
database-assertions
test-scaffolding

Use when writing or updating Pest PHP tests for controllers, Livewire components, API endpoints, actions, or console commands.

Install
php artisan boost:add-skill alizharb/agent-skills

Save this content to: .claude/skills/write-pest-test/SKILL.md

# Write Pest PHP Test

## When To Use

Use this skill when introducing tests for newly written features or writing regression tests using Pest PHP.

## Workflow

1. **Verify setup**: Ensure Pest is initialized in `tests/Pest.php`.
2. **Scaffold test file**: Place files under `tests/Feature/` or `tests/Unit/` with `.php` suffix.
3. **Use descriptive test blocks**: Write tests using `it('does something', ...)` or `test('does something', ...)`.
4. **Mock Side-effects**: Stub out mail, events, notifications, and queues.
5. **Assert JSON payloads & database state**: Use Pest expectations `$this->assertDatabaseHas()` and `$response->assertJson()`.

## Examples

### Good Example

```php
// tests/Feature/Actions/CreateUserTest.php
use App\Actions\CreateUser;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use App\Events\UserCreated;

it('creates a user record and dispatches creation event', function () {
    Event::fake();

    $action = new CreateUser();
    $user = $action->handle([
        'name' => 'John Doe',
        'email' => '[email protected]',
    ]);

    expect($user)->toBeInstanceOf(User::class);
    expect($user->name)->toBe('John Doe');

    $this->assertDatabaseHas('users', [
        'email' => '[email protected]',
    ]);

    Event::assertDispatched(UserCreated::class);
});
```

### Bad Example

```php
// ❌ Using verbose PHPUnit classes with boilerplate when Pest syntax is cleaner and simpler.
namespace Tests\Feature;

use Tests\TestCase;

class CreateUserTest extends TestCase
{
    public function testItCreatesUser()
    {
        // Boilerplate class structure is harder to read and maintain.
    }
}
```

Write Pest PHP Test

When To Use

Use this skill when introducing tests for newly written features or writing regression tests using Pest PHP.

Workflow

  1. Verify setup: Ensure Pest is initialized in tests/Pest.php.
  2. Scaffold test file: Place files under tests/Feature/ or tests/Unit/ with .php suffix.
  3. Use descriptive test blocks: Write tests using it('does something', ...) or test('does something', ...).
  4. Mock Side-effects: Stub out mail, events, notifications, and queues.
  5. Assert JSON payloads & database state: Use Pest expectations $this->assertDatabaseHas() and $response->assertJson().

Examples

Good Example

// tests/Feature/Actions/CreateUserTest.php
use App\Actions\CreateUser;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use App\Events\UserCreated;

it('creates a user record and dispatches creation event', function () {
    Event::fake();

    $action = new CreateUser();
    $user = $action->handle([
        'name' => 'John Doe',
        'email' => '[email protected]',
    ]);

    expect($user)->toBeInstanceOf(User::class);
    expect($user->name)->toBe('John Doe');

    $this->assertDatabaseHas('users', [
        'email' => '[email protected]',
    ]);

    Event::assertDispatched(UserCreated::class);
});

Bad Example

// ❌ Using verbose PHPUnit classes with boilerplate when Pest syntax is cleaner and simpler.
namespace Tests\Feature;

use Tests\TestCase;

class CreateUserTest extends TestCase
{
    public function testItCreatesUser()
    {
        // Boilerplate class structure is harder to read and maintain.
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager