Socket VCR Test
socket-vcr-test is a TypeScript-first library (current version 2.0.1) for recording and replaying HTTP interactions in your test suite, ensuring fast, deterministic, and accurate tests. It functions as a fork of `epignosisx/vcr-test`, aiming to provide enhanced features or maintenance. The library offers flexible recording modes (e.g., `once`, `none`, `update`, `all`) to control cassette generation and playback behavior. Key differentiators include built-in support for request masking to handle sensitive data, and a highly extensible request matching system that allows custom logic for comparing URLs, headers, and bodies. It primarily targets modern JavaScript and TypeScript environments for development testing workflows. It's a useful tool for isolating tests from external network dependencies and ensuring consistent test results without relying on live APIs.
Common errors
-
Error: No matching HTTP interaction found in cassette 'cassette_name'
cause An HTTP request made during the test run did not exactly match any recorded interactions in the specified cassette. This can happen if the request URL, method, headers, or body changed.fixIf the request has genuinely changed, update the cassette by running the test with `vcr.mode = RecordMode.update` or by deleting the cassette file and letting `RecordMode.once` re-record it. If the difference is in dynamic headers (e.g., `Date`, `Authorization`), use `vcr.matcher.ignoreHeaders.add('header-name')` or implement a custom request masker. -
TypeError: Cannot read properties of undefined (reading 'headers')
cause This usually indicates that HTTP interception is not active or correctly configured, or that a request was made outside of the `vcr.useCassette` block.fixVerify that `vcr.useCassette()` properly wraps all code that makes HTTP requests. Ensure the `VCR` instance is correctly initialized with a `FileStorage` or custom storage. If using a custom HTTP client, ensure it's compatible with `socket-vcr-test`'s interception mechanism. -
SyntaxError: Named export 'VCR' not found. The requested module 'socket-vcr-test' does not provide an export named 'VCR'
cause Attempting to use `require()` syntax (`const { VCR } = require('socket-vcr-test');`) in a pure ESM project, or an incorrect import statement in TypeScript/ESM.fixEnsure you are using correct ESM import syntax: `import { VCR } from 'socket-vcr-test';`. For CommonJS environments that require this package, this specific error usually means the CJS compatibility isn't set up, or the project is trying to import an ESM-only package incorrectly.
Warnings
- breaking As a fork of `epignosisx/vcr-test`, this package may introduce breaking changes or behavioral differences compared to the original project. Users migrating or expecting parity should consult the source code and test thoroughly. The README explicitly states a 'TODO' regarding deviations, indicating potential differences.
- gotcha Sensitive data (e.g., API keys, bearer tokens) can be recorded into cassettes if not explicitly masked. Storing sensitive data in your test fixtures poses a security risk, especially in version control systems.
- gotcha Misusing `RecordMode` can lead to stale cassettes or unexpected network calls. For instance, `RecordMode.all` always makes live HTTP calls, which defeats the purpose of fast, deterministic tests in CI/CD environments. Conversely, `RecordMode.none` will fail if a cassette doesn't exist.
- gotcha VCR intercepts *all* HTTP traffic made during the `useCassette` block. Unintended HTTP requests from other parts of your test setup or third-party libraries can pollute your cassettes, leading to larger files, irrelevant interactions, or even non-deterministic behavior if those extra requests vary.
Install
-
npm install socket-vcr-test -
yarn add socket-vcr-test -
pnpm add socket-vcr-test
Imports
- VCR
const { VCR } = require('socket-vcr-test');import { VCR } from 'socket-vcr-test'; - FileStorage
import FileStorage from 'socket-vcr-test/FileStorage';
import { FileStorage } from 'socket-vcr-test'; - RecordMode
import { RecordingMode } from 'socket-vcr-test';import { RecordMode } from 'socket-vcr-test'; - DefaultRequestMatcher
import { DefaultRequestMatcher } from 'socket-vcr-test';
Quickstart
import { join } from 'node:path';
import { VCR, FileStorage, RecordMode } from 'socket-vcr-test';
import { readFileSync, writeFileSync, mkdirSync, existsSync, rmSync } from 'node:fs';
// Mock an external API call for demonstration purposes
const mockApi = {
myAwesomeApiCall: async () => {
console.log(' Making a simulated API call...');
return new Promise(resolve => setTimeout(() => resolve({ data: 'hello from real API', timestamp: Date.now() }), 50));
}
};
// Basic mocks for a runnable 'test' context
const expect = (value: any) => ({
toBeDefined: () => {
if (value === undefined || value === null) throw new Error('Expected value to be defined');
},
toEqual: (expected: any) => {
if (JSON.stringify(value) !== JSON.stringify(expected)) throw new Error(`Expected ${JSON.stringify(value)} to equal ${JSON.stringify(expected)}`);
}
});
const describe = (name: string, fn: () => void) => { console.log(`\n-- Running Suite: ${name} --`); fn(); };
const it = (name: string, fn: () => Promise<void>) => {
console.log(` Test: ${name}`);
fn().then(() => console.log(` ✓ Passed: ${name}`)).catch(e => console.error(` ✗ Failed: ${name}\n`, e));
};
// Setup a dummy directory for cassettes
const CASSETTES_DIR = join(process.cwd(), '__quickstart_cassettes__');
if (!existsSync(CASSETTES_DIR)) {
mkdirSync(CASSETTES_DIR, { recursive: true });
} else {
// Clean up previous run's cassettes for a fresh start
rmSync(CASSETTES_DIR, { recursive: true, force: true });
mkdirSync(CASSETTES_DIR, { recursive: true });
}
describe('socket-vcr-test quickstart', () => {
it('should record and replay an API call using VCR', async () => {
const cassetteName = 'quickstart_api_call';
const cassettePath = join(CASSETTES_DIR, cassetteName + '.yml');
// 1. Initial run: Configure VCR to record (default mode is 'once')
let vcr = new VCR(new FileStorage(CASSETTES_DIR));
vcr.mode = RecordMode.once; // Explicitly set, though it's the default
console.log(` First run (Recording mode: ${vcr.mode}): Cassette ${cassetteName}.yml should be created.`);
await vcr.useCassette(cassetteName, async () => {
const result = await mockApi.myAwesomeApiCall();
expect(result).toBeDefined();
expect((result as any).data).toEqual('hello from real API');
});
expect(existsSync(cassettePath)).toEqual(true);
console.log(` Cassette '${cassetteName}.yml' now exists.`);
// 2. Second run: VCR should replay from the cassette
// Re-initialize VCR to clear any internal state from the previous run
vcr = new VCR(new FileStorage(CASSETTES_DIR));
vcr.mode = RecordMode.once; // Still 'once', but cassette exists
console.log(`\n Second run (Replay mode: ${vcr.mode}): Cassette ${cassetteName}.yml should be replayed.`);
const startTime = Date.now();
await vcr.useCassette(cassetteName, async () => {
const result = await mockApi.myAwesomeApiCall();
expect(result).toBeDefined();
expect((result as any).data).toEqual('hello from real API');
});
const endTime = Date.now();
console.log(` API call completed in ${endTime - startTime}ms (expected very fast replay).`);
// Demonstrating `update` mode
vcr = new VCR(new FileStorage(CASSETTES_DIR));
vcr.mode = RecordMode.update;
console.log(`\n Third run (Update mode: ${vcr.mode}): Simulating change to trigger re-recording if needed.`);
await vcr.useCassette(cassetteName, async () => {
// Even if mockApi changes, 'update' would re-record or update existing entries
const result = await mockApi.myAwesomeApiCall();
expect(result).toBeDefined();
});
});
});
// To run this example: save as `quickstart.ts`, compile with `tsc quickstart.ts`, then `node quickstart.js`