# Write Livewire Component Tests for a Laravel App

Yes. An agent adds Livewire::test cases for a Laravel app's untested component actions, validation and locked properties, runs the suite and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/write-component-tests/livewire/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts by listing what is a component and what a test already calls. In an app like laravel/livewire-starter-kit, Livewire 4 components are single files under `resources/views/pages/settings`: an anonymous class and its Blade template in one `.blade.php`, addressed by a name such as `pages::settings.profile`. There are seven. The screens under `pages/auth` are plain Blade forms that post to Fortify, so they belong to HTTP feature tests and already have them. The existing `Livewire::test` calls reach three methods, `updateProfileInformation`, `deleteUser` and `updatePassword`, plus one mount path on the security page.

Everything else is the gap, and the agent writes it down before it writes a test. On the profile page: `resendVerificationNotification`, the `hasUnverifiedEmail` and `showDeleteUser` computed properties, and the rules in `ProfileValidationRules` for a taken email and a 256-character name. On the security page: `confirmDelete`, `deletePasskey`, `closeDeleteModal`, `disable` and the `two-factor-enabled` listener. The setup modal and the recovery-codes component have no test at all, so `startTwoFactorSetup`, `showVerificationIfNecessary`, `confirmTwoFactor`, `resetVerification` and `regenerateRecoveryCodes` are open. `User::initials()` is untested too, and the one file in `tests/Unit` asserts that true is true.

It follows the repository's conventions instead of bringing its own. `composer.json` requires `phpunit/phpunit` and no Pest, so the new files are PHPUnit classes that extend `Tests\TestCase`, use `RefreshDatabase` and name methods `test_...` like the ten that exist. `phpunit.xml` already points the suite at in-memory SQLite with array sessions and mail, so nothing needs a server. Two-factor and passkey cases call `skipUnlessFortifyHas` from the base class and set the Fortify feature options in `setUp` the way `SecurityTest` does, because a kit installed without those features must still pass. Users come from `UserFactory` and its `unverified()` and `withTwoFactor()` states.

The cases that matter most are about who may do what. `confirmDelete` and `deletePasskey` find the passkey through `auth()->user()->passkeys()->findOrFail(...)`, so a test gives a passkey to a second user and asserts the first can neither select nor delete it. Four properties on the security page, three in the setup modal and the recovery-codes array carry `#[Locked]`; a test tries to `set` each and expects Livewire to refuse. `resendVerificationNotification` gets `Notification::fake()` and both branches. `confirmTwoFactor` gets a five-character code that fails its `size:6` rule and a valid code computed from the stored secret. The agent makes sure PHP 8.3, Composer and Node 22 are present, the versions the workflow sets up, runs `composer setup` and then `composer test`, which is Pint in test mode, PHPStan and `php artisan test` in that order.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write the missing Livewire component tests for this Laravel app. Read composer.json, phpunit.xml, phpstan.neon, pint.json, .github/workflows/tests.yml, tests/TestCase.php, every file under tests/Feature and every single-file component under resources/views/pages first. List each component's public methods, computed properties, event listeners and #[Locked] properties, and mark which ones an existing test reaches. Make sure PHP 8.3, Composer and Node 22 are installed, run composer setup, and run composer test once before changing anything, keeping the output. Then write tests for what is not reached, as PHPUnit classes that extend Tests\\TestCase with RefreshDatabase and test_ method names, beside the existing files in tests/Feature/Settings; do not add Pest or any other package. Use Livewire::test with the pages:: component names, UserFactory states, Notification::fake and Event::fake. Gate two-factor and passkey tests with skipUnlessFortifyHas and set the feature options in setUp as SecurityTest does. Cover: profile validation (duplicate email, name over 255 characters), resendVerificationNotification for a verified and an unverified user, both computed properties, passkey listing, confirmDelete and deletePasskey including a passkey owned by another user, every #[Locked] property refusing an update, disable, the two-factor-enabled listener, the setup modal from start to confirmation with an invalid and a valid code, and recovery code regeneration. Add a unit test for User::initials with one, two and three word names. Assert on component state, validation errors, dispatched events and database rows, not on Flux markup. Do not change anything under app or resources; where behaviour looks wrong, test what the code does and list it. Run composer test until it passes and open a pull request with its output and a table of component, method and cases added.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds test classes under `tests/Feature/Settings` and one under `tests/Unit` for the initials helper. Nothing under `app` or `resources` changes. Its description has a table with one row per component method: the cases added and what each one pins down. Under the table is the output of `composer test` before and after, so the growth in the test count is visible and the Pint and PHPStan steps are shown passing with the new files in place.

A second list holds behaviour the tests now fix in place and that you may not have chosen on purpose. Reading for cases turns up things like `showDeleteUser` hiding the delete-account form from a user whose email is unverified, `updatePassword` clearing all three password fields when validation fails, and the profile page setting `email_verified_at` to null whenever the address changes. Each test asserts what the code does today and the list names the line, so changing the behaviour later is a deliberate edit to one test.

A reviewer checks three things. The feature-gated tests should skip, not fail, when two-factor or passkeys are switched off in `config/fortify.php`. The ownership test should really use a second user's passkey and not a missing id, since both produce a not-found answer. And no assertion should depend on text inside a Flux component, because that markup belongs to a package and moves with its releases. The repository's own workflow runs `composer ci:check` on the pull request, which is the same command the agent ran.

## Run it on every night

Components grow methods faster than they grow tests, so this works as a standing job. In the Project that holds the repository, create an automation with a `schedule` trigger, `cron` set to `0 2 * * *` and your `timezone`, and this page's request as its prompt with one more sentence: pick the component under `resources/views/pages` that changed in the last week and has the most public methods no test calls, and if there is none, end with one line saying so and open nothing. The automation has its own max runs per hour, so a mistake in the schedule cannot start more than that.

For a backlog, start one run per component from your own script: send the body to `POST /o/{org}/sessions` with the component's path written into `input`, the repository in `resources`, and a `request_id` that is a UUID v5 of the repository's full name and that path, so running the script twice returns the runs it already started. Each run adds its own test class, so the pull requests do not touch the same file.

## Why run it on Rush?

Livewire tests are quick to write and easy to write shallowly. A test that mounts a component and asserts it renders passes forever and proves little; the valuable ones need a second user, a faked notification, a feature flag set the way production sets it, and a look at which properties the browser is allowed to change. That is an afternoon of reading seven files closely, and it competes with feature work. The agent does that reading in its own sandbox and runs the suite as CI runs it, with Pint and PHPStan in front, so the count it reports is the one the workflow will show.

It is also work that parallelises. One run per component, each in a separate sandbox with its own in-memory database, gives several small pull requests that can be reviewed in any order. Every command and its output stays in the run's transcript, so when a test looks odd you can read how the agent arrived at it instead of guessing.

## Frequently asked questions

### Does it write Pest or PHPUnit tests?

Whichever the repository already uses. In the starter kit `composer.json` requires PHPUnit and the existing tests are classes, so the new ones are classes too. If your app has `pestphp/pest` installed and tests written as closures, the agent reads that and writes `it(...)` tests with the same `Livewire::test` calls.

### Can it test single-file and multi-file components alike?

Yes. `Livewire::test` takes the component's name, and the name is the same whether the class sits in a `.blade.php` file under `resources/views/pages`, in a multi-file folder, or in `app/Livewire`. The agent reads how your routes and existing tests name components and uses those names.

### How does it test a property marked #[Locked]?

It calls `set` on the property in a test and expects an exception, because Livewire refuses a client update to a locked property. That check is cheap and catches the case where someone removes the attribute from an id such as `deletingPasskeyId`, which would let a browser choose which record a later action touches.

### Does it need a browser or Dusk?

No. `Livewire::test` runs the component in PHP: it mounts it, sets properties, calls actions and reads the resulting state. Alpine behaviour inside the template, such as the recovery input toggle on the two-factor challenge screen, is outside what these tests see. Ask for browser tests separately if you want that covered.

### What if a test it writes fails because the code is wrong?

The request tells it not to change files under `app` or `resources`. It writes the test to the current behaviour, lists the finding with its file and line in the pull request, and leaves the fix to you. You can then send a follow-up message to the same session asking for the fix, and the sandbox wakes with the repository as it left it.

## More Livewire jobs

- [Add File Uploads to a Livewire Component](https://getrush.ai/run/add-file-uploads/livewire/)
- [Review Pull Requests on a Laravel Livewire App](https://getrush.ai/run/review-pull-requests/livewire/)

## The same job on other stacks

- [Write Component Tests for a SolidJS App](https://getrush.ai/run/write-component-tests/solidjs/)
- [Write Component Tests for a Svelte App](https://getrush.ai/run/write-component-tests/svelte/)
- [Write Vitest Component Tests for an Untested Feature](https://getrush.ai/run/write-component-tests/vitest/)
- [Write Cypress Component Tests for the Untested Forms in a React App](https://getrush.ai/run/write-component-tests/cypress/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
