liveBook · Manning

Mocking

As explained in Build AI-Enhanced Web Apps

Mocking is the replacement of a real dependency or external response with a controlled test double.

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

FakeLLM

Simulates a language model’s behavior, allowing you to specify predefined responses or errors for testing purposes.

FakeStreamingLLM

Similar to FakeLLM but designed for scenarios where streaming responses are expected. It yields responses over time.

FakeChatModel

Simulates chat interactions by overriding the _generate method to return fixed messages based on input.

FakeStreamingChatModel

Extends FakeChatModel to support streaming responses, yielding messages over time while simulating chat behavior.

FakeRetriever

Simulates a retriever that returns a fixed set of documents when queried, useful for testing retrieval logic.

FakeRunnable

A fake runnable that can return input as output or options based on configuration, useful for testing runnable logic.

FakeSplitIntoListParser

Parses comma-separated values from text and returns them as an array, useful for testing parsing logic.

FakeListChatModel

A fake chat model that returns a predefined list of responses, useful for simulating sequential chat interactions.

FakeEmbeddings

Provides fake embeddings by overriding methods to return fixed values, useful for testing embedding-related functionality.

FakeTracer

A mock tracer that collects run data during tests, allowing you to verify tracing behavior without actual execution.

FakeChatMessageHistory

Simulates chat message history management, allowing you to test how your application handles message storage and retrieval.

FakeListChatMessageHistory

Similar to FakeChatMessageHistory but designed for list-based message management in chat applications.

FakeTool

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

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains what to replace and works through function, model, streaming, and retrieval substitutes; defers real-model performance to other testing approaches.
Examples
Uses a mocked `generateText` call, a `MockLanguageModelV1` server-action test, and `FakeLLM` with `FakeRetriever` in a RAG test.
What it assumes
Assumes familiarity with React and server-side code, SDK interactions, and AI workflows; defines mocking before applying it to LLM responses.
What you can do after
Apply it via Vitest, Jest, or SDK testing helpers to verify application behavior under predefined responses without a live model call.
How it connects
Connects mocking to unit testing, integration testing, error handling, streaming responses, and retrieval-augmented generation.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.