Mocking replaces a real dependency with a controlled substitute. In an AI web application, that often means returning a predefined model response without calling an LLM API. Tests can then check application logic repeatedly without paying for inference, waiting on network calls, or accounting for different answers to the same prompt. section 8.4.2 section 5.1.3
Choose the boundary that matches the behavior under test:
- Replace an imported function such as `generateText` when testing how application code handles a returned value. section 5.1.3
- Use `MockLanguageModelV1` when testing a Vercel AI SDK call with a controlled model; `simulateReadableStream` can supply stream chunks for streaming tests. section 8.4.3
- Use LangChain.js fakes, including `FakeLLM` and `FakeRetriever`, to test model and retrieval behavior in a RAG workflow. section 8.4.4
Test a server action without a live model
A `continueConversation` action selects a model, calls `generateText`, and returns the conversation history alongside the generated message. In a Jest test, mock `getSupportedModel` to return a `MockLanguageModelV1` whose `doGenerate` result contains fixed text. Call the action and assert that it preserves the history and returns that text. This exercises the action’s behavior without depending on a provider response. section 8.4.3
Table 8.2 Common testing handlers in LangChain.js
| Helper Class |
Description |
|
|
Simulates a language model’s behavior, allowing you to specify predefined responses or errors for testing purposes. |
|
|
Similar to |
|
|
Simulates chat interactions by overriding the |
|
|
Extends |
|
|
Simulates a retriever that returns a fixed set of documents when queried, useful for testing retrieval logic. |
|
|
A fake runnable that can return input as output or options based on configuration, useful for testing runnable logic. |
|
|
Parses comma-separated values from text and returns them as an array, useful for testing parsing logic. |
|
|
A fake chat model that returns a predefined list of responses, useful for simulating sequential chat interactions. |
|
|
Provides fake embeddings by overriding methods to return fixed values, useful for testing embedding-related functionality. |
|
|
A mock tracer that collects run data during tests, allowing you to verify tracing behavior without actual execution. |
|
|
Simulates chat message history management, allowing you to test how your application handles message storage and retrieval. |
|
|
Similar to |
|
|
A mock tool that can be used in testing scenarios to simulate tool behavior and validate interactions with tools. |
The helper table distinguishes LangChain.js substitutes for models, streaming responses, retrievers, runnables, and parsers, making it easier to select a test double for each dependency. section 8.4.4
Keep the mock faithful to the response format the application expects, and test error, asynchronous, and streaming paths where relevant. A passing mock test shows how the system handles the supplied response—not whether a real model will produce it. Fixed answers can miss context-dependent behavior or become stale as models change, so use real-model evaluation when model quality is the question. section 8.4.2 section 8.5




















