fetch-vcr
Stop mocking HTTP Requests. Just record and then play them back. See vcr/vcr for the main idea.
Usage
The basics are:
- turn on
cache
mode - run your tests
This will record (and load) all the HTTP responses into the ./_fixtures/
directory.
And when you run the steps again, no network traffic happens.
How do I set this up?
import fetch from 'fetch-vcr';
fetch.configure({
mode: 'record',
fixturePath: __dirname + '/_fixtures'
})
fetch('http://openstax.org')
.then(response => {
response.text()
.then(text => {
console.log(text)
})
})
What are the different modes?
playback
: (default) only uses the local fixture filescache
: tries to use the fixture and if not found then it is fetched and then saved (useful when adding new tests)record
: forces files to be written (useful for regenerating all the fixtures)erase
: deletes the fixture corresponding to the request
How can I set the VCR mode?
You can set the mode either by:
- setting the
VCR_MODE=record
environment variable - explicitly running
fetch.configure({mode: 'record'})
How can I use this in a browser?
Currently you can record HTTP requests in NodeJS and play them back in the browser.
To play them back in a browser, just run fetchVCR.configure({fixturePath: './path/to/_fixtures'})
and fetchVCR
will use that path to load the files via AJAX requests.