# Write Unit Tests for a Deno App

Yes. An agent adds Deno.test unit tests for a Deno app's untested modules, within the repo's pinned std version and test permissions, and opens a PR.

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

## What the agent does

The agent starts by mapping tests to modules. In an app like ryo-ma/github-profile-trophy, four files hold `Deno.test` cases: the retry helper, the GitHub API service, the request function and one case for `UserInfo` that adds up stargazers. `test/test.ts` is empty. The modules that decide what a visitor sees have none: `src/utils.ts` parses the query string and formats scores, `src/trophy.ts` turns a number into a rank, `src/trophy_list.ts` filters and sorts, `src/card.ts` does the layout arithmetic, and `src/StaticRenderRegeneration` caches the response. It writes tests for those, in `__tests__` folders beside each module as the existing ones are.

It works inside the repository's constraints, not around them. The test task in `deno.json` is `ENV_TYPE=test deno test --allow-env`, and the workflow runs it on Deno `1.44.4`, so the agent installs that version and writes tests that pass on it. Assertions and stubs come from `deps.ts`, which re-exports `assertEquals`, `assertRejects`, `stub`, `spy` and `returnsNext` from `std@0.203.0`. `UserInfo` calls `new Date()` in its constructor to compute `durationYear` and `durationDays`, so the agent adds `FakeTime` from the same std version to `deps.ts` and freezes the clock, instead of asserting on values that change tomorrow.

The cases come from reading the code. `abridgeScore` returns `0pt` below 1, a one-decimal `kpt` figure above 999 and plain points between, with the sign kept. `getNumberValue` uses `parseInt`, so `3abc` is 3 and `abc` falls back to the default. `getBooleanValue` is true only for the exact string `true`. `Trophy.setRank` sorts its conditions by `RANK_ORDER` and takes the first one the score reaches, leaving `?` when none is. `TrophyList` builds seven base trophies and eight secret ones, drops hidden trophies with an unknown rank, and treats a rank or title starting with `-` as an exclusion. `Card` computes width from panel size, columns and margin, sizes itself to the number of trophies when `maxColumn` is -1, and caps rows at `maxRow`.

The cache needs care. `staticRenderRegeneration` writes responses to `/tmp` under a SHA-256 of the path and query, skips `/favicon.ico`, and saves only 2xx answers. Under `--allow-env` every file call is refused, `existsSync` swallows the error and the cache simply never hits, so a naive test passes while proving nothing. The agent stubs `Deno.statSync`, `Deno.readFileSync` and `Deno.writeFile` to drive the hit, miss and stale paths without widening the task's permissions, and restores each stub in a `finally` block the way `request.test.ts` does. It runs `deno fmt --check`, `deno lint` and `deno task test` before opening the pull request.

## Start it with one API call

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

```json
{
  "agent": "codex",
  "input": "Write unit tests for the untested modules of this Deno app. Read deno.json, deps.ts, .github/workflows/testing.yml, CONTRIBUTING.md and every existing *.test.ts first, and list which modules under src have tests and which do not. Install the Deno version the workflow pins and make the tests pass on it. Put each new test file in a __tests__ folder beside its module, use Deno.test, and import assertions and stubs from deps.ts; if you need FakeTime, add it to deps.ts from the std version already used there. Do not change the test task's permissions and do not add a dependency. Cover src/utils.ts (the three typed getters, parseParams, abridgeScore including negative and boundary values), src/trophy.ts (rank selection at, below and above each threshold for one trophy of each kind, and the next-rank bar at the top rank), src/trophy_list.ts (hidden filtering, title and rank inclusion and exclusion, sort order), src/card.ts (width, automatic columns, the row cap, trophy positions), src/user_info.ts with the clock frozen, and src/StaticRenderRegeneration by stubbing the Deno file calls for the hit, miss, stale, favicon and non-2xx paths. Each test creates and restores its own stubs; do not add cases to the shared stub sequence in the API service tests. Do not change files under src except deps.ts. Where current behaviour looks wrong, test the current behaviour and list it. Run deno fmt --check, deno lint and deno task test, and open a pull request with their output and a table of modules and the 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 files under `src/__tests__` and `src/StaticRenderRegeneration/__tests__` and one export line to `deps.ts`. Nothing else under `src` changes. The description has a table with one row per module: the cases added and the behaviour each pins down. Below it is the output of `deno fmt --check`, `deno lint` and `deno task test` on the pinned Deno version. Formatting matters because the workflow fails on `deno fmt --check`, so an unformatted test file would turn CI red by itself.

A second list holds what the tests turned up. Reading for cases surfaces things like `durationDays` being divided by 100 in `UserInfo`, or the file cache never hitting when the process lacks read permission. The agent writes each test to what the code does today, names the line, and leaves the decision to you. A test suite that silently fixes behaviour is harder to review than one that documents it.

A reviewer checks that no test reaches the network or needs a GitHub token, that every `stub` is restored, and that the boundary cases are the real boundaries: 999 and 1000 for `abridgeScore`, the exact `requiredScore` for a rank. The existing API service tests should be unchanged, since their stub is one shared sequence and a case inserted in the middle shifts every answer after it.

## Run it on every issue labelled needs-tests

Create an automation in the Project that holds the repository with a `github` trigger on `issues.labeled` and `needs-tests` in its `labels`. The trigger puts the issue after the saved prompt and fills in nothing, so the prompt is this page's request with one more sentence: write tests for the module the issue names, and mention the issue in the pull request. It fires only for an issue that an owner, member or collaborator opened. In a project where anyone can open an issue, a maintainer opens the one that asks for tests, because a label on an outside contributor's issue starts nothing.

For a backlog, send the body once per module from your own script to `POST /o/{org}/sessions`, with the module'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. A second run of the script returns the runs already started. Each run opens a small pull request, and they do not collide because each adds files in a different folder; only `deps.ts` is shared.

## Why run it on Rush?

These tests are cheap to write badly. Run with whatever Deno is on a laptop, with `-A` because a prompt got in the way once, they pass locally and fail in a workflow pinned to an older version with one permission. The agent's sandbox starts from a clone, installs the version the workflow names and runs the task as written, so the result it reports is the one CI will report.

The other cost is attention. Enumerating thresholds for fifteen trophy classes and the edge cases of a query-string parser is slow, mechanical reading, and it is the reading that finds the odd line. The agent does it in the background, keeps each command and its output in the run's transcript, and hands back a pull request you can review in the order of the table. Several modules can be covered by several runs at once, each in a separate sandbox.

## Frequently asked questions

### Does it use deno test or bring in another runner?

`deno test` only. The request forbids new dependencies, and the existing tests use `Deno.test` with assertions and stubs from the standard library through `deps.ts`. New tests follow that, so the one `deno task test` command still runs everything.

### Why not just give the tests more permissions?

Because the task's `--allow-env` is a statement about what tests are allowed to do, and widening it changes that for every future test. Stubbing `Deno.statSync` and `Deno.readFileSync` exercises the cache logic without touching `/tmp`. If you would prefer a real temporary directory and `--allow-read --allow-write`, say so in the prompt.

### How does it test code that depends on today's date?

With `FakeTime` from the standard library's testing module, at the std version `deps.ts` already pins. The test sets the clock, builds a `UserInfo` from fixed repository dates and asserts exact values for the duration fields and the 2008, 2010 and 2020 thresholds, then restores real time.

### Will the tests still pass after a move to Deno 2?

They are written against the pinned version and use nothing that Deno 2 removes. The `std@0.203.0` URL imports in `deps.ts` keep resolving there. If you migrate first, run this job afterwards and the agent reads the new version from the workflow.

### Does it measure coverage?

It can. `deno test --coverage` followed by `deno coverage` gives per-file numbers with no extra package. Ask for it in the prompt and the pull request includes the table before and after. It is not in the default request because the repository has no coverage step to compare against.

## More Deno jobs

- [Migrate a Deno 1 App to Deno 2](https://getrush.ai/run/migrate-to-deno-2/deno/)
- [Review Pull Requests in a Deno App](https://getrush.ai/run/review-pull-requests/deno/)

## The same job on other stacks

- [Write Unit Tests for a .NET App](https://getrush.ai/run/write-unit-tests/dotnet/)
- [Write Unit Tests for an Echo API](https://getrush.ai/run/write-unit-tests/echo/)
- [Write Unit Tests for an Elixir Project](https://getrush.ai/run/write-unit-tests/elixir/)
- [Write Unit Tests for a Fiber v3 App](https://getrush.ai/run/write-unit-tests/fiber/)

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