# Write Service Tests for a Feathers App

Yes. An agent reads your Feathers services, hooks and resolvers, writes Mocha tests for the access rules and validation they encode, and opens a pull request.

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

## What the agent does

The agent reads how the suite runs before it reads a service. In an app laid out like feathersjs/feathers-chat, the server lives in `feathers-chat-ts`, and its `package.json` defines `test` as a Knex migration followed by `mocha`, which runs `test/` through `ts-node` with `NODE_ENV=test`. `config/test.json` overrides one setting, the port, so the tests use the `feathers-chat.sqlite` file that `config/default.json` names for development. There are four test files. `test/app.test.ts` checks the index page and a JSON 404, `test/client.test.ts` creates a user through the typed REST client and signs in, and the two service tests each assert one thing: that the service is registered.

Everything the app actually enforces sits in hooks and resolvers that no test touches, and the agent lists those rules from the source. In `users.ts`, `create` is open and `find`, `get`, `patch` and `remove` require a JWT; `users.shared.ts` exposes no `update`. In `users.schema.ts` the data resolver hashes the password and fills `avatar` with a Gravatar URL built from the lower-cased email, the external resolver hides `password`, the data schema refuses unknown properties, and the query resolver pins `id` to the caller for every method except `find`. In `messages.ts` every method requires a JWT; `messages.schema.ts` accepts only `text` on create, sets `userId` and `createdAt` on the server, attaches the sender as a virtual `user`, and pins `userId` to the caller outside `find`. `authentication.ts` adds a GitHub strategy whose `getEntityData` falls back to the login when the profile has no email.

It writes one test per rule, calling services two ways. Internal calls such as `app.service('users').create(...)` check the stored result: the password is a hash, the avatar URL is the expected one. External calls go through the client from `src/client.ts` over REST, as `test/client.test.ts` already does, because `authenticate('jwt')` and the external resolvers only apply to a call that has a provider: an anonymous `find` is refused, a second user cannot `get`, `patch` or `remove` the first user's record or message, a `userId` sent in the body of a create never ends up on the stored message, `$limit` above the configured `max` of 50 returns at most 50, and the `user` nested in a message carries no `password`. A unit test covers `getEntityData` with and without an email. Each file creates its users with unique emails in `before` and removes them in `after`, so a failed assertion cannot leave a row behind to collide with the unique `email` column on the next run.

Two things in the source would make any new test misleading, so the agent raises them and does not paper over them. The patch schemas are `Type.Partial` of the full schemas with no restricting resolver, so the agent writes a test that records what happens when an author patches `userId` on their own message and marks it for your decision. And `messages.ts` has a `before` hook that prints `params.user` with `console.log` on every call, which floods the test output. Because the prompt asks for it, the agent also gives the tests their own database by adding a `sqlite.connection` entry to `config/test.json`, then runs `npm test` until it passes twice in a row from a clean file.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write the missing service tests for this Feathers 5 app. The server is in feathers-chat-ts. Read its package.json, config/default.json, config/test.json, knexfile.ts, the migrations, src/app.ts, src/authentication.ts, src/channels.ts, src/client.ts, every file under src/services and src/hooks, and all four files under test first. List each rule the hooks, schemas and resolvers enforce, then write one Mocha test per rule in the existing style (assert, ts-node, the REST client from src/client.ts for external calls and app.service for internal ones): open sign-up, JWT required elsewhere, no update method, password hashing and hiding, the Gravatar default, unknown properties refused, users limited to their own record outside find, messages taking only text with userId and createdAt set by the server, authors only patching and removing their own messages, the pagination max, no password on the user nested in a message, and GitHubStrategy.getEntityData with and without an email. Create test users with unique emails and remove them afterwards. Add a sqlite.connection entry to config/test.json so tests stop using the development database file. Change nothing under src: where a test shows behaviour that looks unintended, such as patching userId on a message, keep the test, mark it clearly and describe it in the pull request. Run npm test twice from a clean database and paste the output.",
  "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 rewrites `test/services/users/users.test.ts` and `test/services/messages/messages.test.ts`, adds a test for the GitHub strategy, and changes one line of configuration, `config/test.json`. Nothing under `src/` is touched. The description lists each rule beside the test that covers it and pastes the Mocha output from two consecutive runs, the second proving the suite cleans up after itself.

It also carries a short section headed by what the agent found and did not fix: the patch schemas that accept every field, the `console.log` in the messages hooks, and anything else a test surfaced. Each item names the file and the test that demonstrates it, so turning one into a fix is a follow-up message on the same session, not a new investigation.

In a repository that keeps the example's `.github/workflows/nodejs.yml`, the `feathers-chat-ts` job runs `npm install` and `npm test` in that directory on every push and pull request, so the new tests run in CI with no workflow change. A reviewer reads the tests as a statement of the app's access rules and checks that each one is a rule they intended.

## Run it on every night

In the console, add an automation to the Project with a `schedule` trigger, `cron` set to an early hour in your `timezone`, and a prompt that asks for tests for any service, hook or resolver under `src/` that has changed since the tests beside it were last touched. Each night's run works on the default branch as it stands and opens one pull request on its own branch, or says in a sentence that nothing was missing.

A team that generates a service per feature can also start a run when a pull request adds one: a `github` trigger on `pull_request.opened` puts the pull request after the prompt, and the prompt says to cover the services that pull request adds. Only work by an owner, member or collaborator fires the trigger.

## Why run it on Rush?

Feathers puts authorisation in hook order and resolver functions, and whether they hold depends on how a call arrives. The only way to know is to make the call. The agent has a sandbox of its own where it installs the dependencies, runs the migrations against SQLite, starts the app on the test port and calls each service as an anonymous client, as one user and as another. A test that passes only because it called the service internally, where `authenticate` never runs, is caught there, and the output of every run is kept with the session.

The result is a suite that states the rules in the open. The next change to a resolver either keeps those tests green or shows exactly which rule moved, and the work of writing them did not come out of a developer's week. Several services, or several apps, can each have a run at the same time without sharing a database file.

## Frequently asked questions

### Why test through the REST client and not just call the service?

Because the rules differ. A call made with `app.service(...)` on the server has no provider, so `authenticate('jwt')` lets it through and the external resolver is not applied. Access rules and hidden fields can only be tested by a call that comes in over a transport, which is what the client in `src/client.ts` does.

### Does it test real-time events and channels?

Not in this request. `src/channels.ts` publishes every event to the `authenticated` channel, and testing that needs a Socket.io client, which the server package does not list among its dev dependencies. Ask for it and the agent adds the client as a dev dependency and asserts who receives a `created` event.

### Will the tests wipe my development data?

They would share its file today, since `config/test.json` only changes the port. The request has the agent add a separate `sqlite.connection` for tests, and each test removes the rows it creates. Remove that sentence from the prompt if you prefer to keep one file.

### What does it do with a test that fails because of a real bug?

It keeps the test, marks it so the suite still passes, and describes the bug in the pull request with the file and the failing assertion. The prompt forbids changes under `src/`, so the fix is your decision and can be a follow-up on the same session.

## More Feathers jobs

- [Migrate a Feathers App From SQLite to Postgres](https://getrush.ai/run/migrate-sqlite-to-postgres/feathers/)
- [Run a Security Audit on a Feathers App](https://getrush.ai/run/security-audit/feathers/)

## More jobs like this: Write the missing tests

- [Write Snapshot Tests for a Textual App](https://getrush.ai/run/write-snapshot-tests/textual/)
- [Write Tests for Scrapy Spiders](https://getrush.ai/run/write-spider-tests/scrapy/)
- [Write Supertest Tests for an Express API](https://getrush.ai/run/write-supertest-tests/express/)
- [Write Supertest Tests for a Koa App](https://getrush.ai/run/write-supertest-tests/koa/)

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