HTTP request recording and replay system for API testing
—
Replay's mode system controls how HTTP requests are handled, providing four distinct operational modes for different testing scenarios.
Controls the operational mode of the replay system.
/**
* Current operational mode
* @type {'replay' | 'record' | 'cheat' | 'bloody'}
*/
Replay.mode: string;Usage Examples:
const Replay = require('replay');
// Set to record mode to capture new responses
Replay.mode = 'record';
// Set to replay mode for tests (default)
Replay.mode = 'replay';
// Set to cheat mode for development
Replay.mode = 'cheat';
// Set to bloody mode to bypass all replay functionality
Replay.mode = 'bloody';The default mode can be set via environment variable:
/**
* Environment variable for default mode
* @type {'replay' | 'record' | 'cheat' | 'bloody'}
*/
process.env.REPLAY: string;Usage Examples:
# Run tests in record mode
REPLAY=record npm test
# Run in cheat mode
REPLAY=cheat node app.js
# Run in bloody mode (live requests)
REPLAY=bloody npm testThe default mode for running tests. Only replay captured responses, no outbound network access.
Behavior:
Use Cases:
Captures new HTTP responses while replaying existing ones.
Behavior:
Use Cases:
Allows both replay and live requests without recording.
Behavior:
Use Cases:
Bypasses all replay functionality, all requests go live.
Behavior:
Use Cases:
Thrown when an invalid mode is specified:
/**
* Error thrown for invalid mode
* @throws {Error} When mode is not one of the supported values
*/
new Error(`Unsupported mode '${mode}', must be one of ${MODES.join(', ')}.`);Thrown in replay mode when no matching fixture is found:
/**
* Error thrown when request cannot be replayed
* @throws {Error} When in replay mode and no fixture matches
*/
new Error(`Connection to ${url} refused: not recording and no network access`);Usage Examples:
const Replay = require('replay');
try {
Replay.mode = 'invalid-mode';
} catch (error) {
console.log(error.message); // "Unsupported mode 'invalid-mode', must be one of bloody, cheat, record, replay."
}
// In replay mode with no matching fixture
Replay.mode = 'replay';
http.get('http://new-api.example.com/data', (response) => {
// This will throw: Connection to http://new-api.example.com:80/data refused: not recording and no network access
});Install with Tessl CLI
npx tessl i tessl/npm-replay