Skip to main content
在您原型化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');
});