@aws/durable-execution-sdk-js-testing

中文文档
1.3.2发布于 2026/10/26Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 48.2万

AWS Durable Execution Testing SDK for TypeScript

原始使用文档
文档目录19 节
  1. Features
  2. Installation
  3. Quick Start
  4. Handler Code
  5. Test Code
  6. Documentation
  7. LocalDurableTestRunner
  8. Mocking
  9. Function Registration
  10. CloudDurableTestRunner
  11. Test Result API
  12. Operation Assertions
  13. Callback Operations
  14. Configuration Options
  15. LocalDurableTestRunner
  16. CloudDurableTestRunner
  17. Reset Runner State
  18. Security
  19. License

AWS Durable Execution Testing SDK for JavaScript

Testing utilities for the AWS Durable Execution SDK for JavaScript and TypeScript.

Features

This package provides testing tools for durable functions both locally and in the cloud:

  • LocalDurableTestRunner - Execute and test durable functions locally without AWS deployment

    • Time-skipping support - Skip wait operations and delays for faster test execution
    • Function registration - Register additional functions for chained invoke testing
    • Full operation inspection - Access detailed information about steps, waits, callbacks, and more
    • Mock-friendly - Works seamlessly with Jest and other mocking frameworks
  • CloudDurableTestRunner - Test against deployed AWS Lambda functions

    • Real environment testing - Validate behavior against actual your actual deployed AWS Lambda function
    • Configurable invocation - Support for different invocation types and polling intervals
  • Testing Capabilities

    • Operation assertions - Inspect individual operations by name, index, or ID
    • Status and result validation - Verify execution status, results, and error conditions
    • Callback testing - Send callback responses and verify callback behavior
    • Retry validation - Test step retry logic and failure scenarios
  • run-durable CLI - Command-line tool for quick testing without writing test code

    • Zero setup - Run any durable function with npx run-durable your-function.js
    • Operation inspection - View detailed operation tables and execution history
    • Time control - Skip time for fast testing or use real timing for validation

Installation

npm install --save-dev @aws/durable-execution-sdk-js-testing

Quick Start

Handler Code

import {
  withDurableExecution,
  DurableContext,
} from "@aws/durable-execution-sdk-js";

const handler = async (event: any, context: DurableContext) => {
  // Execute a durable step with automatic retry
  const userData = await context.step("fetch-user", async () =>
    fetchUserFromDB(event.userId),
  );

  // Wait for 5 seconds
  await context.wait({ seconds: 5 });

  // Process data in another step
  const result = await context.step("process-user", async () =>
    processUser(userData),
  );

  return result;
};

export const lambdaHandler = withDurableExecution(handler);

Test Code

Local Testing

import {
  LocalDurableTestRunner,
  WaitingOperationStatus,
} from "@aws/durable-execution-sdk-js-testing";
import { lambdaHandler } from "./lambdaHandler";

beforeAll(() =>
  LocalDurableTestRunner.setupTestEnvironment({
    skipTime: true,
  }),
);

afterAll(() => LocalDurableTestRunner.teardownTestEnvironment());

describe("LocalDurableTestRunner", () => {
  let runner;

  beforeEach(() => {
    runner = new LocalDurableTestRunner({
      handlerFunction: lambdaHandler,
    });
  });

  it("should wait for 5 seconds and return result", async () => {
    const execution = await runner.run({ payload: { userId: "123" } });

    expect(execution.getStatus()).toBe("SUCCEEDED");
    expect(execution.getOperations()).toHaveLength(3); // fetch-user, wait, process-user

    // Check the fetch-user step
    const fetchStep = runner.getOperation("fetch-user");
    await fetchStep.waitForData(WaitingOperationStatus.COMPLETED);
    const fetchDetails = fetchStep.getStepDetails();
    expect(fetchDetails?.result).toBeDefined();

    // Check the wait operation
    const waitOp = runner.getOperationByIndex(1);
    const waitDetails = waitOp.getWaitDetails();
    expect(waitDetails?.waitSeconds).toBe(5);

    // Check the process-user step
    const processStep = runner.getOperation("process-user");
    await processStep.waitForData(WaitingOperationStatus.COMPLETED);
    const processDetails = processStep.getStepDetails();
    expect(processDetails?.result).toBeDefined();
  });
});

Cloud Testing

import {
  CloudDurableTestRunner,
  WaitingOperationStatus,
} from "@aws/durable-execution-sdk-js-testing";

describe("CloudDurableTestRunner", () => {
  let runner;

  beforeEach(() => {
    runner = new CloudDurableTestRunner({
      functionName: "my-durable-function", // Your deployed function name
    });
  });

  it("should wait for 5 seconds and return result", async () => {
    const execution = await runner.run({ payload: { userId: "123" } });

    expect(execution.getStatus()).toBe("SUCCEEDED");
    expect(execution.getOperations()).toHaveLength(3); // fetch-user, wait, process-user

    // Check the fetch-user step
    const fetchStep = runner.getOperation("fetch-user");
    const fetchDetails = fetchStep.getStepDetails();
    expect(fetchDetails?.result).toBeDefined();
    expect(fetchDetails?.attempt).toBe(1);

    // Check the wait operation
    const waitOp = runner.getOperationByIndex(1);
    const waitDetails = waitOp.getWaitDetails();
    expect(waitDetails?.waitSeconds).toBe(5);

    // Check the process-user step
    const processStep = runner.getOperation("process-user");
    const processDetails = processStep.getStepDetails();
    expect(processDetails?.result).toBeDefined();
  });
});

Documentation

This README provides a quick reference for the Testing SDK's main features. For more detailed information:

  • API Reference - Complete technical reference with detailed type definitions and operation specifications
  • run-durable CLI Guide - Complete guide for using the command-line tool to quickly test durable functions
  • Contributing - Learn about contributing to the AWS Durable Execution Testing SDK for JavaScript

LocalDurableTestRunner

Run durable functions locally with a simulated durable execution backend.

import { LocalDurableTestRunner } from "@aws/durable-execution-sdk-js-testing";
import { handler } from "./my-durable-function";

// Set up test environment with optional time skipping
await LocalDurableTestRunner.setupTestEnvironment({
  skipTime: true, // Skip wait delays for faster tests
});

const runner = new LocalDurableTestRunner({
  handlerFunction: handler,
});

const execution = await runner.run({ payload: { test: "data" } });

// Assert on results
expect(execution.getStatus()).toBe("SUCCEEDED");
expect(execution.getResult()).toEqual(expectedValue);

// Assert on operations
const operations = execution.getOperations();
expect(operations).toHaveLength(3);

// Clean up test environment
await LocalDurableTestRunner.teardownTestEnvironment();

Mocking

Mock external dependencies using Jest or your preferred testing framework:

import { LocalDurableTestRunner } from "@aws/durable-execution-sdk-js-testing";

// Mock external functions
jest.mock("../services/userService", () => ({
  fetchUserFromDB: jest.fn(),
  processUser: jest.fn(),
}));

import { fetchUserFromDB, processUser } from "../services/userService";
import { lambdaHandler } from "./lambdaHandler";

const mockFetchUser = fetchUserFromDB as jest.MockedFunction<
  typeof fetchUserFromDB
>;
const mockProcessUser = processUser as jest.MockedFunction<typeof processUser>;

describe("Mocked Dependencies", () => {
  let runner: LocalDurableTestRunner;

  beforeAll(() =>
    LocalDurableTestRunner.setupTestEnvironment({ skipTime: true }),
  );
  afterAll(() => LocalDurableTestRunner.teardownTestEnvironment());

  beforeEach(() => {
    runner = new LocalDurableTestRunner({ handlerFunction: lambdaHandler });

    // Reset mocks
    mockFetchUser.mockClear();
    mockProcessUser.mockClear();
  });

  it("should call mocked functions and return expected results", async () => {
    // Setup mocks
    const userData = { id: "123", name: "John Doe" };
    const processedResult = { id: "123", processed: true };

    mockFetchUser.mockResolvedValue(userData);
    mockProcessUser.mockResolvedValue(processedResult);

    // Run test
    const execution = await runner.run({ payload: { userId: "123" } });

    // Verify results
    expect(execution.getStatus()).toBe("SUCCEEDED");
    expect(execution.getResult()).toEqual(processedResult);

    // Verify mock calls
    expect(mockFetchUser).toHaveBeenCalledWith("123");
    expect(mockProcessUser).toHaveBeenCalledWith(userData);

    // Verify operations
    const fetchStep = runner.getOperation("fetch-user");
    const fetchDetails = fetchStep.getStepDetails();
    expect(fetchDetails?.result).toEqual(userData);
  });
});

Function Registration

Register additional functions that can be invoked during local testing of chained invocations:

Handler Code

const mainHandler = withDurableExecution(async (event, context) => {
  await context.invoke("workflow-a:$LATEST");
  await context.invoke("workflow-b:$LATEST");

  await context.invoke("utility-function");
  await context.invoke("utility-helper");
});

Test code

const runner = new LocalDurableTestRunner({ handlerFunction: mainHandler });

// Register durable functions
runner.registerDurableFunction("workflow-a:$LATEST", durableWorkflowA);

// Register regular functions
runner.registerFunction("utility-function", utilityHandler);

// Method chaining is supported
runner
  .registerDurableFunction("workflow-b:$LATEST", durableWorkflowB)
  .registerFunction("helper", helperHandler);

CloudDurableTestRunner

Test against deployed Lambda functions in AWS.

import {
  CloudDurableTestRunner,
  InvocationType,
} from "@aws/durable-execution-sdk-js-testing";
import { LambdaClient } from "@aws-sdk/client-lambda";

const runner = new CloudDurableTestRunner({
  functionName: "MyDurableFunction",
  client: new LambdaClient({ region: "us-east-1" }), // optional
  config: {
    pollInterval: 1000, // optional, default 1000ms
    invocationType: InvocationType.RequestResponse, // optional
  },
});

const execution = await runner.run({ payload: { userId: "123" } });

expect(execution.getStatus()).toBe("SUCCEEDED");

Test Result API

Both runners return a TestResult object with methods for assertions:

const execution = await runner.run();

// Get execution status and results
execution.getStatus(); // "SUCCEEDED" | "FAILED" | "RUNNING" | etc.
execution.getResult(); // Returns the function result
execution.getError(); // Returns error details if failed

// Get operations
execution.getOperations(); // All operations
execution.getOperations({ status: "SUCCEEDED" }); // Filter by status

// Get execution history and invocations
execution.getHistoryEvents(); // Detailed event history
execution.getInvocations(); // Handler invocation details

// Print operations table to console
execution.print(); // Default columns
execution.print({ name: true, status: true, duration: true }); // Custom columns

Operation Assertions

Access specific operations for detailed assertions:

// Get operations by different methods
const operation = runner.getOperation("my-step"); // By name
const firstOp = runner.getOperationByIndex(0); // By execution order
const secondNamedOp = runner.getOperationByNameAndIndex("my-step", 1); // By name + index
const opById = runner.getOperationById("abc123"); // By unique ID

// Wait for operation data to be available
await operation.waitForData(); // Default to STARTED status
await operation.waitForData(WaitingOperationStatus.COMPLETED);

// Get operation details based on type
const stepDetails = operation.getStepDetails(); // For step operations
const contextDetails = operation.getContextDetails(); // For context operations
const callbackDetails = operation.getCallbackDetails(); // For callback operations
const waitDetails = operation.getWaitDetails(); // For wait operations

// Get basic operation information
operation.getName();
operation.getStatus();
operation.getStartTimestamp();
operation.getEndTimestamp();

Callback Operations

For callback operations, you can send responses:

const callback = runner.getOperation("my-callback");
await callback.waitForData(WaitingOperationStatus.SUBMITTED);

// Send callback responses
await callback.sendCallbackSuccess("result data");
await callback.sendCallbackFailure({ errorMessage: "Failed" });
await callback.sendCallbackHeartbeat();

Configuration Options

LocalDurableTestRunner

// Constructor
new LocalDurableTestRunner({ handlerFunction: handler });

// Environment setup
await LocalDurableTestRunner.setupTestEnvironment({ skipTime: true });

// Environment teardown
await LocalDurableTestRunner.teardownTestEnvironment();

CloudDurableTestRunner

// Basic configuration
new CloudDurableTestRunner({ functionName: "MyFunction:$LATEST" });

// Advanced configuration
new CloudDurableTestRunner({
  functionName: "MyFunction:$LATEST",
  client: new LambdaClient({ region: "us-east-1" }),
  config: { pollInterval: 500, invocationType: InvocationType.Event },
});

Reset Runner State

The reset() method is required if you reuse the same runner instance between tests. Data about an individual execution is cleared from the runner instance when reset() is called.

describe("Reusing Runner Instance", () => {
  let runner: LocalDurableTestRunner;

  beforeAll(() => {
    // Create runner once
    runner = new LocalDurableTestRunner({ handlerFunction: handler });
  });

  beforeEach(() => {
    // Reset state when reusing the same instance
    runner.reset();
  });

  // ... tests
});

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

版本与发布记录

@aws/durable-execution-sdk-js-testing 共发布 12 个版本,首次发布于 2025/11/25;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.2latest2026/10/2612 天前
1.3.12026/10/1722 天前
1.3.02026/10/021 个月前
1.2.02026/09/301 个月前
1.1.42026/09/091 个月前
1.1.32026/07/074 个月前

关于 @aws/durable-execution-sdk-js-testing 的常见问题

@aws/durable-execution-sdk-js-testing 是什么?

AWS Durable Execution Testing SDK for TypeScript

如何安装 @aws/durable-execution-sdk-js-testing?

在项目目录运行 npm install @aws/durable-execution-sdk-js-testing,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@aws/durable-execution-sdk-js(>=1.0.1 <4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 run-durable,可以用 npx run-durable 直接运行。

@aws/durable-execution-sdk-js-testing 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/durable-execution-sdk-js-testing 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aws/durable-execution-sdk-js-testing 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws/durable-execution-sdk-js-testing 可以商用吗?

@aws/durable-execution-sdk-js-testing 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/durable-execution-sdk-js-testing 的下载量有多少?

根据 npm 官方下载统计,@aws/durable-execution-sdk-js-testing 在 2026/10/08 – 2026/11/06 这 30 天内被下载 482,354 次,日均约 16,078 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/durable-execution-sdk-js-testing 还在维护吗?

最新版本 1.3.2 发布于 2026/10/26(12 天前);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2025/11/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。