在您为 LangGraph 智能体制作原型后,自然的下一步是添加测试。本指南涵盖了在编写单元测试时可以使用的一些有用模式。 请注意,本指南特定于 LangGraph,涵盖具有自定义结构的图的场景 - 如果您刚开始,请查看本节,其中使用 LangChain 的内置 @[create_agent]。

先决条件

首先,确保您已安装 vitest
$ npm install -D vitest

入门

由于许多 LangGraph 智能体依赖于状态,一个有用的模式是在使用它的每个测试之前创建您的图,然后在测试中使用新的检查点器实例编译它。 下面的示例显示了这如何与一个简单的线性图一起工作,该图通过 node1node2 进展。每个节点更新单个状态键 my_key
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import { z } from "zod/v4";

const State = z.object({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('basic agent execution', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  const result = await compiledGraph.invoke(
    { my_key: 'initial_value' },
    { configurable: { thread_id: '1' } }
  );
  expect(result.my_key).toBe('hello from node2');
});

测试单个节点与边

编译后的 LangGraph 智能体会通过 graph.nodes 暴露每个节点的引用。您可以利用这一点测试智能体中的单个节点。请注意,这样做会绕过编译图时传入的任何检查点:
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import { z } from "zod/v4";

const State = z.object({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('individual node execution', async () => {
  const uncompiledGraph = createGraph();
  // Will be ignored in this example
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  // Only invoke node 1
  const result = await compiledGraph.nodes['node1'].invoke(
    { my_key: 'initial_value' },
  );
  expect(result.my_key).toBe('hello from node1');
});

部分执行

对于由较大图组成的智能体,您可能希望测试智能体内的部分执行路径,而不是端到端的整个流程。在某些情况下,将这些部分重构为子图可能在语义上是有意义的,您可以像平常一样单独调用它们。 但是,如果您不希望更改智能体图的整体结构,可以使用 LangGraph 的持久化机制来模拟智能体在所需部分开始之前暂停的状态,并在所需部分结束时再次暂停。步骤如下:
  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.
以下是一个仅在线性图中执行第二个和第三个节点的示例:
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import { z } from "zod/v4";

const State = z.object({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addNode('node3', (state) => ({ my_key: 'hello from node3' }))
    .addNode('node4', (state) => ({ my_key: 'hello from node4' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', 'node3')
    .addEdge('node3', 'node4')
    .addEdge('node4', END);
};

test('partial execution from node2 to node3', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  await compiledGraph.updateState(
    { configurable: { thread_id: '1' } },
    // The state passed into node 2 - simulating the state at
    // the end of node 1
    { my_key: 'initial_value' },
    // Update saved state as if it came from node 1
    // Execution will resume at node 2
    'node1',
  );
  const result = await compiledGraph.invoke(
    // Resume execution by passing None
    null,
    {
      configurable: { thread_id: '1' },
      // Stop after node 3 so that node 4 doesn't run
      interruptAfter: ['node3']
    },
  );
  expect(result.my_key).toBe('hello from node3');
});

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.