When I've coded in Ruby or Python, I've been able to use libraries like VCR that intercept HTTP requests, and record them, so when for example I'm hitting an 3rd party API in tests, I can save that response as a fixture instead of manually building a huge mock objects to check l behaviour against.
It's not perfect, but it has saved a load of time when I've been exploring which API requests to make against a third party API, (often wrapping a 3rd party library), then writing tests to check this behaviour.
What's the closest thing in JS these days to this?
Monkey patching an existing library or function
I see things fetch-vcr, or axios-vcr, but these seem to rely on explicitly reaching into a library to replace say, a call to fetch
with the http-intercepting version instead, be reading a 'cassette' file containing the canned response.
I'm looking for a way to avoid patching 3rd party code if I can help it, a this is how I understand VCR works for other languages.
Presumably, if there's an HTTP client built somewhere into node then that would be the place you'd patch a function - I haven't come across a specific library that does this.
Running an entire HTTP server
Alternatively I can see libraries like vcr.js, or yakbak, which essentially set up an HTTP server which serves JSON blobs you define, at various urls, like serving a saved users.json
file at http://localhost:8100/users/
This is okay, but again, if I don't need to spin up a whole HTTP server, and make actual HTTP requests, that would be wonderful.
Aucun commentaire:
Enregistrer un commentaire