HTTP Interaction Recorder for Tests

1.4.0 · active · verified Wed Apr 22

vcr-test is a testing utility that records and replays HTTP interactions, enabling fast, deterministic, and accurate tests by eliminating reliance on live external APIs. It intercepts HTTP traffic from any client (`fetch`, `axios`, `got`, etc.) using `@mswjs/interceptors` and stores request/response pairs in YAML "cassette" files. The current stable version is 1.4.0. It provides flexible recording modes (once, none, update, all) and extensibility for request masking, pass-through, and custom matching. Its key differentiator is its framework-agnostic nature and support for modern JavaScript features like `await using`, while also offering a callback API for older environments. While no explicit release cadence is stated, the project appears actively maintained on GitHub.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates recording and replaying an HTTP interaction using `VCR` and `FileStorage` with the modern `await using` syntax. It shows how to initialize VCR, wrap an API call with `useCassette`, and assert the replayed result.

import { join } from 'node:path';
import { VCR, FileStorage } from 'vcr-test';

// This would be your actual API client, e.g., using fetch or axios
const api = {
  async myAwesomeApiCall() {
    const response = await fetch('https://httpbin.org/post', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name: 'john' })
    });
    return response.json();
  }
};

describe('some suite', () => {
  const cassettesPath = join(process.cwd(), '__cassettes__');
  const vcr = new VCR(new FileStorage(cassettesPath));

  it('should record and replay an API call using await using', async () => {
    // Ensure the cassette directory exists and is clean for testing
    // For real usage, you'd manage this outside the test.

    await using _cassette = await vcr.useCassette('my_test_cassette');
    const result = await api.myAwesomeApiCall();

    expect(result).toBeDefined();
    expect(result.data).toBe('{"name":"john"}');
    expect(result.headers['Content-Type']).toContain('application/json');
  }, 10000); // Increase timeout for initial recording
});

view raw JSON →