# Write Unit Tests for a LangChain.js App

Yes. An agent writes unit tests for a LangChain.js app's chains, agents and route handlers that run with a faked model response and no API key, and opens a PR.

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

## What the agent does

The agent begins with what is tested and how. In an app like langchain-ai/langchain-nextjs-template there is one test file, `utils/integrations.test.mjs`, written for `node:test` and run by a command in the README, `node --experimental-strip-types --test utils/integrations.test.mjs`. It covers the `calculator` tool and the `SupabaseVectorStore` adapter with a hand-written fake client and fake embeddings. `package.json` has no `test` script and `.github` holds only `dependabot.yml`, so nothing runs it automatically. The six route handlers under `app/api`, the two server actions under `app/ai_sdk` and the graph in `app/langgraph/agent/agent.ts` have no tests.

Two things make those handlers awkward, and the agent deals with each. They construct `new ChatOpenAI(...)` inside `POST`, so a fake model cannot be passed in. Instead of rewriting the handlers, the tests replace `globalThis.fetch` with a function that returns an OpenAI-shaped streaming response, built with the helpers `@langchain/core/utils/testing` exports for this (`sseResponseFromOpenAIChunks`, `openAITextOnlyChunks`, `openAIToolCallChunks`), and set a dummy `OPENAI_API_KEY`. The real client, the real chain and the real route code all run; only the network is fake. The second obstacle is that the routes import `@/utils/calculator` through the `paths` alias in `tsconfig.json`, which plain `node --test` does not resolve. The request allows one new devDependency for a runner that reads that alias, and asks the agent to say why.

The cases come from each handler's code. `app/api/chat/route.ts` should put every earlier message into `chat_history` as `role: text` lines and the last one into `input`, and stream the model's text back. With an empty `messages` array it reads `parts` of `undefined`, lands in the `catch` and answers 500, and the test pins that down. The agents route drops every message that is not from the user or assistant, streams only `on_chat_model_stream` chunks that have content, and with `show_intermediate_steps` returns JSON whose assistant entries carry `tool_calls`. The retrieval route sets `x-message-index` and an `x-sources` header holding base64 JSON with the first 50 characters of each document. The ingest route answers 403 when `NEXT_PUBLIC_DEMO` is `true`.

The agent keeps the existing test passing, adds a `test` script so one command runs both the old file and the new ones, and runs `yarn lint` and `yarn build`, since a test helper imported by mistake from a route file would end up in the edge bundle. It asserts on what a caller sees: status, headers, the streamed text, the JSON body. It does not assert on prompt wording, which changes often and is not behaviour.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write unit tests for this LangChain.js app's route handlers and tools. Read package.json, tsconfig.json, the README's test section, utils/integrations.test.mjs, utils/calculator.ts, utils/supabase-vector-store.ts and every route.ts under app/api first, and say what is tested today and how it is run. No test may call OpenAI, Tavily or Supabase or need a real key. The handlers build their model inside POST: do not refactor them to accept one. Instead stub globalThis.fetch with OpenAI-shaped responses built from the helpers in @langchain/core/utils/testing, set dummy keys in the test environment, and restore both after each test. Reuse the fake Supabase client and embeddings pattern from the existing test for the retrieval and ingest routes. If node:test cannot import the routes because of the @/ path alias, add one devDependency for a runner that resolves it and explain the choice; keep the existing test file passing either way. Cover for each route: the happy path's status, headers and body or stream; an empty messages array; an upstream error status; and for the agents routes the filtering of system messages, the streaming branch and the show_intermediate_steps branch with a tool call. Cover demo mode on the ingest route. Assert on what a caller receives, not on prompt text. Where a handler's behaviour looks wrong, test what it does today and list it. Add a test script to package.json, run it with yarn lint and yarn build, and open a pull request with the output and a table of routes and cases.",
  "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 beside the routes or in one `tests` folder, a small helper that builds fake model responses, a `test` script and, if it was needed, one devDependency with its lockfile change. Route files are untouched. The description has a table with a row per route and the cases covering it, then the output of the test script, `yarn lint` and `yarn build`.

A separate list holds what the tests documented and did not fix. In this repository that would include the 500 for an empty `messages` array where a 400 would be more accurate, and that every handler returns `e.message` to the caller in its `catch`, so an upstream error's text reaches the browser. The existing adapter test takes care that database failures do not expose upstream details; the routes do not. Each item has the file and line, and whether to change it is left to you.

A reviewer checks that the fetch stub is restored after every test, that no test passes merely because the stub was never called, and that the streaming assertions read the whole stream. A test that awaits only the response object proves the handler returned, not that it streamed the right text.

## Run it on every merged pull request

An app like this grows by adding a route for each new chain or agent, and each new route arrives untested for the same reasons the first six did. Create an automation in the Project that holds the repository with a `github` trigger on `pull_request.closed.merged` and your default branch in `branch_patterns`. The saved prompt is this page's request plus one sentence: cover only the handlers and tools the merged pull request added or changed. The trigger places the pull request after the prompt and fills in nothing, and it does not fire for forks or bots.

To cover a backlog across several apps, send the body once per repository to `POST /o/{org}/sessions` with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name. An API key runs four at a time by default; further runs answer `202` with `status` `queued` and start as slots free, so the script that sends them needs no retry loop.

## Why run it on Rush?

The usual way to test an LLM route is to run it against the real model, which costs money on every run, needs a secret in CI and gives a different answer each time. That is why these routes have no tests. Faking the HTTP response keeps the real client and chain in the test and makes the output deterministic, but setting it up takes reading the library's testing helpers and getting a streaming response exactly right. The agent does that reading in its sandbox against the installed version.

A fresh sandbox is also the honest place to prove no key is needed. On a developer's machine `.env.local` has an `OPENAI_API_KEY`, and a test that quietly calls the real API passes there. In the sandbox there is no key and the request forbids the calls, so a test that leaks to the network fails where you can see it, with the command and its output kept in the run's transcript.

## Frequently asked questions

### Do the tests check that the model gives good answers?

No. They check your code: that messages are formatted and filtered correctly, that tool calls are passed through, that headers and status codes are right, and that errors are handled. Answer quality is an evaluation problem and needs a real model and a dataset. This job keeps the plumbing from breaking between evaluations.

### Why stub fetch instead of mocking ChatOpenAI?

Because the handlers create the model inside `POST`, so there is nothing to inject, and replacing the whole class would skip the code that parses the provider's streaming format. A fake HTTP response exercises the client your users depend on. The library exports helpers that build OpenAI-shaped chunks for this purpose.

### Does it work for Anthropic or other providers?

The approach does, with that provider's response shape. The agent reads which chat model classes the routes construct and builds the fake responses to match. If your code already accepts a model as a parameter, it uses a fake chat model from the same testing module and skips the fetch stub.

### Will it add a CI workflow to run the tests?

Not in this request. The repository has no workflow today, and adding one is a separate decision about runners and secrets. The pull request adds the `test` script, so a workflow later is one step that runs `yarn test`.

### What about the LangGraph agent and the server actions?

The graph in `app/langgraph/agent/agent.ts` is compiled at import time with a model at module scope, so it is tested through the same fetch stub by invoking the exported `graph`. The two `app/ai_sdk` actions use React server streaming, and one fetches a prompt from the hub at request time; the agent covers what runs without a network and lists the rest.

## More LangChain.js jobs

- [Fix "ImportError: cannot import name 'ChatOpenAI' from 'langchain.chat_models'" in LangChain 1.0](https://getrush.ai/run/fix-cannot-import-name-chatopenai-from-langchain-chat-models/langchain/)
- [Fix Deprecated APIs in a LangChain.js App](https://getrush.ai/run/fix-deprecated-apis/langchain/)

## The same job on other stacks

- [Write Unit Tests for a Laravel Project](https://getrush.ai/run/write-unit-tests/laravel/)
- [Write Unit Tests for an MCP Server](https://getrush.ai/run/write-unit-tests/mcp-server/)
- [Write Unit Tests for a Medusa Backend](https://getrush.ai/run/write-unit-tests/medusa/)
- [Write Unit Tests for a Node.js App](https://getrush.ai/run/write-unit-tests/nodejs/)

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