Configs2026年7月25日·1 分钟阅读

Polly.js — Record, Replay, and Stub HTTP Interactions by Netflix

An HTTP interaction recording and playback library by Netflix for deterministic testing of API-dependent code in Node.js and browsers.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Polly.js Overview
先审查命令
npx -y tokrepo@latest install 2ef78570-8869-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

Polly.js by Netflix records HTTP interactions during test runs and replays them on subsequent executions. This eliminates flaky tests caused by network variability while keeping tests realistic by using actual API responses as fixtures.

What Polly.js Does

  • Records HTTP requests and responses to persistent storage (filesystem, REST API, or localStorage)
  • Replays recorded interactions on subsequent test runs for deterministic results
  • Intercepts and stubs requests that match configurable rules
  • Supports request matching by URL, method, headers, and body
  • Provides timing simulation to mirror real-world latency

Architecture Overview

Polly.js uses an adapter layer to intercept HTTP calls (fetch, XHR, or Node.js http) and a persister layer to store recordings as HAR-like JSON files. On replay, incoming requests are matched against recordings using a configurable matching strategy. Unmatched requests can passthrough, record, or throw depending on the mode.

Self-Hosting & Configuration

  • Install the core package plus your chosen adapter and persister
  • Register adapters and persisters before creating a Polly instance
  • Set recordIfMissing to control behavior for unrecorded requests
  • Configure request matching strictness via matchRequestsBy options
  • Use expiresIn to auto-expire recordings and force re-recording

Key Features

  • Three modes: record, replay, and passthrough for flexible test workflows
  • HAR-compatible recording format for interoperability
  • Request interception with custom response handlers
  • Timing simulation preserving original response latency
  • Built-in adapters for fetch, XHR, and Node.js http

Comparison with Similar Tools

  • MSW (Mock Service Worker) — Mocks at the service worker level; Polly.js records real interactions
  • Nock — Node.js-only HTTP mocking; Polly.js works in both Node.js and browsers
  • VCR (Ruby) — Ruby HTTP recording; Polly.js brings the same pattern to JavaScript
  • WireMock — Java-based mock server; Polly.js runs in-process without a separate server
  • Mirage.js — Client-side mock server; Polly.js focuses on recording real responses

FAQ

Q: Can I use Polly.js with Jest? A: Yes. Use the @pollyjs/core setup/teardown in beforeEach/afterEach hooks.

Q: How do I handle sensitive data in recordings? A: Use the recordFailedRequests option and custom serializers to redact headers or body fields.

Q: Does it support GraphQL? A: Yes. Configure matchRequestsBy to include the request body so different queries are recorded separately.

Q: Can recordings be shared across CI environments? A: Yes. Commit recordings to your repository or use the REST persister to store them centrally.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产