Skip to main content
After you’ve prototyped your LangGraph agent, a natural next step is to add tests. This guide covers some useful patterns you can use when writing unit tests. Note that this guide is LangGraph-specific and covers scenarios around graphs with custom structures - if you are just getting started, check out Test that uses LangChain’s built-in createAgent instead.

Prerequisites

First, make sure you have vitest installed:

Getting started

Because many LangGraph agents depend on state, a useful pattern is to create your graph before each test where you use it, then compile it within tests with a new checkpointer instance. The below example shows how this works with a simple, linear graph that progresses through node1 and node2. Each node updates the single state key my_key:

Testing individual nodes and edges

Compiled LangGraph agents expose references to each individual node as graph.nodes. You can take advantage of this to test individual nodes within your agent. Note that this will bypass any checkpointers passed when compiling the graph:

Partial execution

For agents made up of larger graphs, you may wish to test partial execution paths within your agent rather than the entire flow end-to-end. In some cases, it may make semantic sense to restructure these sections as subgraphs, which you can invoke in isolation as normal. However, if you do not wish to make changes to your agent graph’s overall structure, you can use LangGraph’s persistence mechanisms to simulate a state where your agent is paused right before the beginning of the desired section, and will pause again at the end of the desired section. The steps are as follows:
  1. Compile your agent with a checkpointer (the in-memory checkpointer MemorySaver will suffice for testing).
  2. Call your agent’s update_state method with an asNode parameter set to the name of the node before the one you want to start your test.
  3. Invoke your agent with the same thread_id you used to update the state and an interruptBefore parameter set to the name of the node you want to stop at.
Here’s an example that executes only the second and third nodes in a linear graph: