Registry / testing / socket-vcr-test

socket-vcr-test

JSON →
library2.0.1jsnpmunverified

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.

npm install socket-vcr-test
INSTALL
IMPORT
SIG · SOCKET-VCR-TEST
S
socket-vcr-test
testingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VCR
import { VCR } from 'socket-vcr-test';
const { VCR } = require('socket-vcr-test');
The library is primarily designed for ESM usage in TypeScript or modern JavaScript environments. For CommonJS, named imports are still possible but less idiomatic for newer projects.
FileStorage
import { FileStorage } from 'socket-vcr-test';
import FileStorage from 'socket-vcr-test/FileStorage';
FileStorage is a named export, not a default export or submodule. Ensure correct named import syntax.
RecordMode
import { RecordMode } from 'socket-vcr-test';
import { RecordingMode } from 'socket-vcr-test';
The enum for recording behavior is named `RecordMode`, not `RecordingMode`.
DefaultRequestMatcher
import { DefaultRequestMatcher } from 'socket-vcr-test';
Used for customizing how VCR matches incoming requests against recorded interactions.

This quickstart demonstrates how to initialize `socket-vcr-test` with `FileStorage`, use `useCassette` to wrap an asynchronous API call, and verifies that the HTTP interactions are recorded and then replayed. It shows both recording (first run) and replaying (subsequent runs) behavior, as well as an example of `RecordMode.update`.

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`
Debug
Known issues
breakingAs 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.
fix
Review the GitHub repository's commit history and issue tracker for details on changes from the upstream `epignosisx/vcr-test`. Thoroughly test your application when migrating or using this fork.
affects: >=2.0.0
gotchaSensitive 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.
fix
Implement a `requestMasker` function to redact or replace sensitive information within request headers or bodies before they are written to the cassette. For example: `vcr.requestMasker = (req) => { req.headers['authorization'] = 'masked'; };`
affects: >=1.0.0
gotchaMisusing `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.
fix
Use `RecordMode.once` (the default) for most development, which records if no cassette exists and replays otherwise. For updating existing cassettes, use `RecordMode.update` temporarily. Ensure `RecordMode.none` is used in CI to prevent unintended network access.
affects: >=1.0.0
gotchaVCR 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.
fix
Ensure that HTTP-making code is tightly scoped within the `vcr.useCassette` block. Consider using `requestMatch` customization to ignore specific URLs or request patterns that are irrelevant to the test but might occur within the same context. Clear VCR state between tests if necessary.
affects: >=1.0.0
Errors
Common errors & fixes
Error: No matching HTTP interaction found in cassette 'cassette_name'
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.
fix
If 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')
This usually indicates that HTTP interception is not active or correctly configured, or that a request was made outside of the `vcr.useCassette` block.
fix
Verify 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'
Attempting to use `require()` syntax (`const { VCR } = require('socket-vcr-test');`) in a pure ESM project, or an incorrect import statement in TypeScript/ESM.
fix
Ensure 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.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
1
Resources
socket-vcr-test — npm install socket-vcr-test · libregistry