JavaScript
See the:
README & Quick Start - Installation and basic setup
Full API Documentation - Complete TypeDoc reference
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-agent 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-devMinimum Requirements
Node.js 18 or higher
Import Patterns
CORE CONCEPTS
Intercept Modes
Intercept modes control what the Stoobly agent does with intercepted HTTP requests.
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:
POST /user- Create userGET /user/{id}- Fetch user dataGET /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 IDapplyRecord()- Activates recording mode, returns session IDclear()- Removes all interceptionclearRecord()- 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/XMLHttpRequestMust be called in
beforeEach()(Cypress clears intercepts before each test)Test titles are automatically detected
Warning: Cypress hangs with synchronous XMLHttpRequest. See Cypress issue #29566.
Playwright Class
Specialized interceptor for Playwright test framework. Extends Interceptor.
Key Differences from Base Interceptor:
Uses Playwright's
page.route()APIAll 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:
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):
HTTP Method (required) - GET, POST, PUT, DELETE, etc.
Path (required) - e.g.,
/usersQuery Parameters - Sorted alphabetically before comparison
Headers - Sorted alphabetically before comparison
Body - Strict matching if provided
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()orapplyRecord()was calledFor Cypress: Must call in
beforeEach(), notbefore()For Playwright: Must call
withPage(page)beforeapply()
Q: Cypress tests hang
Check for synchronous XMLHttpRequest usage (not supported)
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
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