Skip to content

nettofarah/axios-vcr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 17, 2019
240fcd9 · Sep 17, 2019

History

38 Commits
Sep 17, 2019
Feb 10, 2017
Sep 17, 2019
Jun 9, 2016
Sep 17, 2019
Jun 10, 2016
May 27, 2016
Sep 17, 2019
Sep 17, 2019
Sep 17, 2019

Repository files navigation

axios-vcr

📼 Record and Replay requests in JavaScript

axios-vcr is a set of axios middlewares that allow you to record and replay axios requests. Use it for reliable, fast and more deterministic tests.

Build Status

Features

  • Record http requests to JSON cassette files
  • Replay requests from cassete files
  • Multiple request/response fixtures per cassette
  • Cassette expiration logic
  • Mocha integration
  • non-global axios instances support

Installation

$ npm install --save-dev axios-vcr

Usage

Using axios-vcr is very simple. All you need to do is to provide a cassette path and wrap your axios code with axiosVCR.mountCassette and axiosVCR.ejectCassette.

const axiosVCR = require('axios-vcr');

axiosVCR.mountCassette('./test/fixtures/cats.json')

axios.get('https://reddit.com/r/cats.json').then(response => {
  // axios-vcr will store the remote response from /cats.json
  // in ./test/fixtures/cats.json
  // Subsequent requests will then load the response directly from the file system

  axiosVCR.ejectCassette('https://reddit.com/r/cats.json')
})

Usage in a test case

it('makes your requests load faster and more reliably', function(done) {
  // mount a cassette
  axiosVCR.mountCassette('./fixtures/test_case_name.json')

  myAPI.fetchSomethingFromRemote().then(function(response) {
    assert.equal(response.something, 'some value')
    done()

    // Eject the cassette when all your promises have been fulfilled
    axiosVCR.ejectCassette('./fixture/test_case_name.json')
  })
})

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nettofarah/axios-vcr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Code of Conduct.

To run the specs check out the repo and follow these steps:

$ npm install
$ npm test

License

The module is available as open source under the terms of the MIT License.