# Write Tests for API Error States With Mock Service Worker

Yes. An agent overrides your MSW handlers per test with 500s, 401s and network errors, asserts what the UI shows and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/test-api-error-states/msw/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own sandbox and reads the mock setup first, because error tests only work if they bend the existing handlers rather than adding a second mock layer. In alan2207/bulletproof-react, the app in `apps/react-vite` runs Vitest with `src/testing/setup-tests.ts`, which starts an MSW 2.2.14 `setupServer` from `src/testing/mocks/server.ts` with `onUnhandledRequest: 'error'`, calls `server.resetHandlers()` after each test, and seeds and resets an `@mswjs/data` database around every test. The handlers in `src/testing/mocks/handlers` cover auth, comments, discussions, teams and users, and they fail only when the fake backend itself decides to: a `401` without a session cookie on most routes, a `400` for an email that is taken or a team that does not exist, a `404` for an unknown discussion, and a `500` when a handler throws, which is what a wrong password produces on `POST /auth/login`. `GET /auth/me` without a cookie answers `200` with `data: null`. No existing test makes a request fail on purpose; the login, register and discussions tests drive happy paths.

Then it reads what the app does when a request fails. `src/lib/api-client.ts` builds one axios instance with a response interceptor: on any error it takes `error.response?.data?.message`, falling back to axios's own `error.message`, and adds an error notification through the `useNotifications` zustand store with the title `Error`. On a `401` it also sets `window.location.href` to the login path from `paths.auth.login.getHref`, with the current path as `redirectTo`. `src/app/provider.tsx` renders `<Notifications />` inside the app, and `src/lib/react-query.ts` sets `retry: false`, so a failed query fails once and the test does not wait through retries.

Each test overrides one endpoint with `server.use(...)` for that test only, using the same `env.API_URL` the handlers use: a `500` with a `message` body on `GET /discussions`, a `500` with no body (so the fallback message shows), `HttpResponse.error()` for a network failure, a `401` on `GET /auth/me`, and a `403` or `500` on mutations such as `POST /discussions` and `PATCH /users/profile`. It renders the real route with `renderApp` from `src/testing/test-utils.tsx` and asserts what a person sees: the notification text, that the form stays open with what was typed, and that no success message appears. jsdom does not navigate, so for the `401` case the test replaces `window.location` and asserts the `href` the interceptor wrote.

It keeps to the repository's conventions: test files in `__tests__` folders, `screen`, `userEvent` and `waitFor` from `test-utils`, and `console.error` silenced the way the discussions test does. The zustand mock in `__mocks__/zustand.ts` resets every store after each test, so notifications from one test never leak into the next. It runs `yarn test --run` until every test passes, then `yarn lint` and `yarn check-types`, the same commands as the CI workflow.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "In apps/react-vite, write tests for what the app shows when API calls fail. Read src/testing/setup-tests.ts, src/testing/mocks/, src/testing/test-utils.tsx, src/lib/api-client.ts, src/lib/react-query.ts, src/app/provider.tsx and the existing tests first. Override handlers per test with server.use, never by editing the shared handlers: a 500 with a message body, a 500 with no body, a network error, a 401 on /auth/me, and failures on mutations such as creating a discussion and updating the profile. Render the real routes with renderApp and assert what a user sees: the error notification text, forms that keep their input, and no success message. For the 401 case, replace window.location and assert the login URL with redirectTo. Follow the existing test style and folder layout. Do not change application code; list anything that looks wrong as a finding. Run yarn test --run, yarn lint and yarn check-types until they pass. Open a pull request.",
  "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

What comes back is a pull request that adds test files under `__tests__` folders, for example next to the discussions and profile routes, and changes no handler in `src/testing/mocks/handlers` and no application code. Each test names the endpoint it breaks and the status it returns, and the description has a table of endpoint, failure and what the UI is expected to show.

The session transcript keeps every `yarn test --run` with its output, including the runs that failed while the agent learned how the interceptor and the notification store behave, and the final passing run with lint and type check. The pull request's checks run `react-vite-ci.yml`, whose `yarn test` step now includes the new tests.

Findings come as a list, not as changes. One example the agent would check: `redirectTo` in the interceptor is read from a new, empty `URLSearchParams`, so it is always the current path, and a reviewer decides whether that is the intent. Before merging, a reviewer also checks that every override is per test and that no test passes only because of the order it runs in.

## Run it on every new API endpoint

Each new endpoint in the app needs its failure states tested once. An automation in the Project with a `github` trigger on `pull_request.opened` and a prompt that says "for any new MSW handler this pull request adds, write the error-state tests and open a pull request with them" starts one run per pull request; each fire adds the pull request after the prompt, and only work by an owner, member or collaborator fires it.

For a one-off backfill across many features, send one `POST /o/{org}/sessions` per feature folder from a script, naming the folder in `input`. Each run has its own sandbox and its own `node_modules`, and a start past the key's `max_concurrent` limit answers `202` with `status: "queued"` until a slot frees.

## Why run it on Rush?

Error states are the tests people mean to write and do not, because each one needs a handler override, a render through the real providers, and an assertion on a notification that appears asynchronously. In a Rush sandbox the agent iterates through those runs with Vitest and jsdom on a machine that exists for this job, with no local dev server, browser or backend, and keeps going until the assertions describe what the interceptor really does.

The pull request shows exactly which failures are covered and how the app answers each, with every test run kept in the transcript. The next time someone changes the interceptor in `api-client.ts`, these tests are the ones that say whether the user still sees the error.

## Frequently asked questions

### Will it change our shared MSW handlers?

No. Every failure is a `server.use(...)` override inside one test, and `setup-tests.ts` already calls `server.resetHandlers()` after each test, so the shared handlers stay the happy path that other tests rely on.

### Does this work with MSW 1.x?

Yes, with different calls. MSW 1 uses `rest.get` and `res(ctx.status(500))` instead of `http.get` and `HttpResponse`. The agent reads the version in your lockfile and writes the overrides for it.

### We use fetch, not axios. Does that matter?

Only for what the tests assert. The agent reads your API client to see where errors are caught and what the user is shown, and asserts that. MSW intercepts both fetch and axios requests in Node the same way.

### Can it test errors in Storybook too?

In a follow-up, yes. Storybook stories can use MSW handlers per story through an addon; this run keeps to Vitest, where the setup already exists.

## More MSW jobs

- [Serve Storybook Stories From the App's MSW Handlers](https://getrush.ai/run/add-msw-to-storybook/msw/)

## More jobs like this: Write the missing tests

- [Write Tests for a Custom GitHub Action](https://getrush.ai/run/write-action-tests/github-actions/)
- [Write Tests for an Actix Web API](https://getrush.ai/run/write-actix-tests/actix/)
- [Write Unit Tests for an Angular App](https://getrush.ai/run/write-angular-unit-tests/angular/)
- [Write API Tests for an Axum App](https://getrush.ai/run/write-api-tests/axum/)
- [Write API Tests for a Django REST Framework API](https://getrush.ai/run/write-api-tests/django-rest-framework/)

## 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/)
