axios-vcr2 v1.1.0
axios-vcr2
About The Project
This is a library intended to mock HTTP responses in node - specifically jest or similar testing frameworks.
It is intended to be easy to use: mount a cassette, record a session, and play it back on subsequent requests, recording only exactly what you want and need without hooking into the global node libraries.
If you are looking for something more powerful, try nock instead.
This is primarily a TypeScript rewrite of axios-vcr, addressing some small bugs and adding the ability to use custom Axios instances.
Getting Started
Prerequisites
You will need a package manager of your choice, and you will probably want to use this in some testing framework. There is no dependency on any specific framework - you can use this package in any node environment.
yarn
npm install yarn@latest -g
axios
yarn add axios@latest
Installation
axios-vcr2
yarn add -D axios-vcr2
Usage
Basic usage
import axios from "axios";
import { mountCassette, ejectCassette } from "axios-vcr2";
mountCassette("myCassettes.json");
// Record the request the first time this line runs
// Next time you run this file, the request will be played back.
axios.get("http://www.example.com/");
// After ejecting the cassette, requests will not be recorded.
ejectCassette("myCassettes.json");
axios.get("http://www.example.com/"); // <- not recorded/played back
Advanced usage
It is possible to use custom axios instances, use more than one cassette, and/or to specify custom matchers.
import axios, { AxiosRequestConfig } from "axios";
import { mountCassette, ejectCassette } from "axios-vcr2";
const myAxios = axios.create();
// This matcher looks at the method and URL. This is very similar to
// the `defaultMatcher` included with this package.
function exampleMatcher(requestConfig: AxiosRequestConfig): string {
const { baseURL, method } = config;
const url = config.baseURL ? new URL(url, config.baseURL).toString() : config.url;
// This can be any string. It will be used as the cassette id, to match subsequent
// requests. The matchers included with this package use md5 to hash a processed object.
// Return 'null' not to save the request in this file.
//
// This matcher only returns IDs for URLs matching example.com
return url.includes("example.com") ? `${method}:${url}` : null;
}
// This matcher only returns IDs for URLs matching askjeeves.com
function jeevesMatcher(requestConfig: AxiosRequestConfig): string {
const { baseURL, method } = config;
const url = config.baseURL ? new URL(url, config.baseURL).toString() : config.url;
return url.includes("askjeeves.com") ? `${method}:${url}` : null;
}
mountCassette("example.com.json", myAxios, exampleMatcher);
mountCassette("askjeeves.com.json", myAxios, jeevesMatcher);
// Two files will be created; example.com.json containing 2 requests to example.com,
// and askjeeves.com.json containing 1 request to askjeeves.com
myAxios.get("http://www.example.com/");
myAxios.post("http://www.example.com/", { a: 1 });
myAxios.get("http://www.askjeeves.com/");
Development
testing
yarn test
Debugging
This package uses debug. To enable debugging, set the environment variable
DEBUG
to axios-vcr2:*
:
DEBUG="axios-vcr2:*" yarn test
Contributing
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Cris van Pelt - cris@melkfl.es
Project Link: https://github.com/crisvp/axios-vcr2