跳到主要内容
在您原型化您的 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. 使用检查点编译您的代理(内存检查点 MemorySaver 足以用于测试)。
  2. 使用 asNode 参数调用代理的 update_state 方法,该参数设置为您要开始测试的节点之前的节点的名称。
  3. 使用您用于更新状态的相同 thread_id 和设置为您要停止的节点的名称的 interruptBefore 参数调用您的代理。
这是一个只执行线性图中第二个和第三个节点的示例
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');
});

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.