JavaScript

See the:

This document provides a comprehensive reference for all APIs, configuration options, and concepts.


QUICK REFERENCE

What is stoobly-js?

stoobly-js is a library for HTTP request interception, recording, and mocking with Stoobly. The JavaScript library (stoobly npm package) integrates with the stoobly-agentarrow-up-right to enable:

  • Recording: Capture HTTP requests and responses for later replay

  • Mocking: Return pre-recorded responses instead of hitting real APIs

  • Replay: Re-execute recorded requests

  • Testing: Validate API responses against recorded data

Installation

npm install stoobly --save-dev

Minimum Requirements

  • Node.js 18 or higher

Import Patterns


CORE CONCEPTS

Intercept Modes

Intercept modes control what the Stoobly agent does with intercepted HTTP requests.

Mode
Description
Use Case

mock

Returns pre-recorded responses

Testing without real APIs

record

Saves requests/responses to storage

Capturing API behavior

replay

Re-executes recorded requests

Reproducing scenarios

test

Validates responses against recorded data

API contract testing

Scenarios

A scenario is a sequence of related HTTP requests that describes a workflow. For example, a "user registration" scenario might include:

  1. POST /user - Create user

  2. GET /user/{id} - Fetch user data

  3. GET /products - Load product list

Scenarios enable:

  • Grouping related requests together

  • Replaying entire workflows

  • Sharing test data with teammates

  • Mocking consistent sequences

Scenario Key: A unique Base64-encoded identifier for each scenario. Obtain from the Stoobly UI or CLI:

Sessions

A session groups requests within a test run. Sessions:

  • Default to current timestamp if not specified

  • Can be manually set for consistent identification

  • Enable switching between different test contexts

Context

A context is a collection of requests and scenarios. The default context is stored in ~/.stoobly. Contexts enable:

  • Separation of different APIs/applications

  • Collaboration by sharing entire contexts


API REFERENCE

Stoobly Class

The main entry point for the library.

Example:


InterceptorOptions Interface

Configuration object for all interceptor types.

URL Patterns:


RecordOptions Interface

Configuration for recording behavior.


Interceptor Class (Base)

The generic interceptor that patches fetch and XMLHttpRequest directly.

Key Behaviors:

  • apply() - Activates interception, returns session ID

  • applyRecord() - Activates recording mode, returns session ID

  • clear() - Removes all interception

  • clearRecord() - Stops recording but keeps other interception

Example:


Cypress Class

Specialized interceptor for Cypress test framework. Extends Interceptor.

Key Differences from Base Interceptor:

  • Uses cy.intercept() instead of patching fetch/XMLHttpRequest

  • Must be called in beforeEach() (Cypress clears intercepts before each test)

  • Test titles are automatically detected

Warning: Cypress hangs with synchronous XMLHttpRequest. See Cypress issue #29566arrow-up-right.


Playwright Class

Specialized interceptor for Playwright test framework. Extends Interceptor.

Key Differences from Base Interceptor:

  • Uses Playwright's page.route() API

  • All main methods are async

  • Requires explicit page setup via withPage()

  • Test titles must be set manually via withTestTitle()

Example:


CONSTANTS REFERENCE

Import from stoobly/constants:

InterceptMode

RecordPolicy

Controls which requests get recorded.

RecordOrder

Controls how duplicate recordings are handled.

RecordStrategy

Controls recording detail level.

MockPolicy

Controls mocking behavior.

ReplayPolicy

TestPolicy

TestStrategy

FirewallAction

RequestParameter


HTTP HEADERS INJECTED

The library injects custom headers into matching HTTP requests:

Header
Description

X-Stoobly-Proxy-Mode

Active intercept mode (mock/record/replay/test)

X-Stoobly-Record-Order

Recording order (append/overwrite)

X-Stoobly-Record-Policy

Recording policy (all/found/not_found)

X-Stoobly-Record-Strategy

Recording strategy (full/minimal)

X-Stoobly-Scenario-Key

Base64-encoded scenario identifier

X-Stoobly-Scenario-Name

Human-readable scenario name

X-Stoobly-Session-Id

Current session identifier

X-Stoobly-Test-Title

Current test name (auto-detected or manual)


CONFIG API

Access Stoobly agent configuration:


COMMON PATTERNS

Basic Mocking (No Test Framework)

Recording New Requests

Switching Sessions Mid-Test

Overwriting Existing Recordings

Recording Only New Requests


REQUEST MATCHING (Mocking)

When mocking, Stoobly matches requests using these components (case-sensitive):

  1. HTTP Method (required) - GET, POST, PUT, DELETE, etc.

  2. Path (required) - e.g., /users

  3. Query Parameters - Sorted alphabetically before comparison

  4. Headers - Sorted alphabetically before comparison

  5. Body - Strict matching if provided

  6. Body Parameters - Parsed from JSON or form-urlencoded, sorted alphabetically

With Scenario: Only requests in that scenario are matched. Multiple matches return responses in recording order.

Without Scenario: Any matching request is considered.


PROXY SETTINGS (Agent Configuration)

These settings are configured in the Stoobly agent UI or via CLI, not the JS library:

Data Rules

  • Scenario: Which scenario to record to / mock from

  • Policy: Which requests to record/mock

Firewall Rules

Fine-grained URL filtering:

  • Pattern: Regex to match request URLs

  • Action: Include or Exclude

  • Methods: Which HTTP verbs

  • Modes: Which intercept modes (mock, record, replay)

Example: Exclude OAuth token requests from recording:

Match Rules

Customize which request components are used for matching (mocking only):

  • Components: Header, Body Param, Query Param

  • Default: Query params and body only (method/path always required)

Rewrite Rules

Modify request components before processing:

  • Pattern: URL pattern to match

  • Parameter Type: Header, Body Param, or Query Param

  • Name/Value: What to rewrite

Example: Redact auth tokens during recording:


TROUBLESHOOTING

Common Issues

Q: Requests aren't being intercepted

  • Verify URLs match your patterns (check regex)

  • Ensure apply() or applyRecord() was called

  • For Cypress: Must call in beforeEach(), not before()

  • For Playwright: Must call withPage(page) before apply()

Q: Cypress tests hang

Q: Test titles not being captured

  • Cypress: Automatic

  • Playwright: Must call withTestTitle(testInfo.title) manually

Q: How to find scenario key?

  • UI: Scenarios page → Click scenario → Key field

  • CLI: stoobly-agent scenario list

Q: Recordings not matching during mock

  • Check all matching components (method, path, query params, body, headers)

  • Use Match Rules to relax constraints

  • Ensure correct scenario is selected


TYPE DEFINITIONS

Full TypeScript types available in stoobly/types:


COMPLETE EXAMPLE: CYPRESS E2E TEST


COMPLETE EXAMPLE: PLAYWRIGHT E2E TEST


SUMMARY TABLE

Feature
Method/Property
Notes

Create interceptor

stoobly.interceptor(options)

Generic (patches fetch/XHR)

Create Cypress interceptor

stoobly.cypressInterceptor(options)

Uses cy.intercept

Create Playwright interceptor

stoobly.playwrightInterceptor(options)

Uses page.route

Start intercepting

interceptor.apply()

Returns session ID

Start recording

interceptor.applyRecord()

Sets mode to record

Stop all

interceptor.clear()

Removes interception

Stop recording

interceptor.clearRecord()

Keeps other modes

Set scenario

interceptor.withScenarioKey(key)

Chainable

Set session

interceptor.withSessionId(id)

Chainable

Set test title

interceptor.withTestTitle(title)

Chainable

Set intercept mode

interceptor.withInterceptMode(mode)

Chainable

Get config

stoobly.config.dump()

Returns Promise

Set scenario (config)

stoobly.config.scenario.set(key)

Returns Promise

Last updated