Installation
Testing utilities are included in@langchain/core:
Fake Models
FakeChatModel
A simple chat model that echoes back the input or returns predefined responses.- Unit testing chains and agents
- Testing prompt templates
- Validating message handling logic
FakeStreamingChatModel
A chat model that simulates streaming responses with configurable behavior.sleep(number): Milliseconds between chunks (default: 50)responses(BaseMessage[]): Predefined messages to returnchunks(AIMessageChunk[]): Exact chunks to emittoolStyle(“openai” | “anthropic” | “bedrock” | “google”): Tool formatthrownErrorString(string): Error to throw instead of responding
FakeListChatModel
A chat model that cycles through a predefined list of responses.- Cycles through responses automatically
- Supports streaming
- Can emit custom events for testing
- Supports tool calling
- Can include generation info for testing metadata
FakeLLM
A basic LLM that echoes input or returns a predefined response.FakeStreamingLLM
A streaming LLM with configurable responses.Fake Embeddings
FakeEmbeddings
Returns fixed embeddings for any input.SyntheticEmbeddings
Generates deterministic embeddings based on input content.- Testing similarity search
- Testing vector store operations
- Benchmarking without API calls
- Deterministic test results
Other Fake Components
FakeRetriever
Returns predefined documents for any query.FakeTool
A tool that returns JSON representation of its input.Test Matchers
LangChain provides custom Jest/Vitest matchers for testing messages and tool calls.Setup
First, extend the test framework with LangChain matchers:Message Type Matchers
toBeHumanMessage
Asserts that a value is a HumanMessage.toBeAIMessage
Asserts that a value is an AIMessage.toBeSystemMessage
Asserts that a value is a SystemMessage.toBeToolMessage
Asserts that a value is a ToolMessage.Tool Call Matchers
toHaveToolCalls
Asserts that an AIMessage has specific tool calls.toHaveToolCallCount
Asserts the number of tool calls in an AIMessage.toContainToolCall
Asserts that an AIMessage contains at least one matching tool call.Message Array Matchers
toHaveToolMessages
Asserts that an array of messages contains specific ToolMessages.Special Matchers
toHaveBeenInterrupted
Asserts that a graph execution was interrupted (used with LangGraph).toHaveStructuredResponse
Asserts that a result has a structured response.Testing Patterns
Testing a Simple Chain
Testing Tool Calling
Testing Streaming
Testing Error Handling
Testing Retrieval
Best Practices
- Use Fake Models for Unit Tests: Don’t make actual API calls in unit tests
- Test Edge Cases: Use
thrownErrorStringto test error handling - Deterministic Tests: Use
SyntheticEmbeddingsfor reproducible results - Custom Matchers: Use LangChain matchers for cleaner assertions
- Test Streaming: Verify streaming behavior with
FakeStreamingChatModel - Mock Responses: Use
FakeListChatModelto test conversation flows - Integration Tests: Use real models in separate integration test suites
Related
- Testing Guide - Comprehensive testing strategies
- Standard Tests - Standard test suites for integrations
- LangGraph Testing - Testing stateful agents
- Debugging - Debugging LangChain applications
