philschatz / fetch-vcr

:vhs: Stop mocking HTTP Requests! Just record and then play them back
https://philschatz.com/2017/05/15/record-fetch-requests/
33 stars 11 forks source link

Multiple cassettes for the same endpoint #22

Open philippegirard opened 6 years ago

philippegirard commented 6 years ago

Hi,

I am wondering if there is a clean way to have multiple cassettes for the same endpoint and specify which one to use in a test case.

This would be useful when an endpoint might return different JSON body.

My current solution This seams a bit a hacky to me, but it works. I have created a folder architecture and each folder can record a different response for the same endpoint.

test('#saveCurrentUser authorized', async ()=> {
    fetch.configure({
      fixturePath: './_fixtures/authorized/'
    });
    // doing fetch to the endpoint example.com/api/test
    // this returns {"id":"e4130aaa-f5c","cip":"ciei200","enabled":true}
...

  test('#saveCurrentUser unauthorized', async () => {
    fetch.configure({
      fixturePath: './_fixtures/unauthorized/'
    });

   // doing fetch to the same endpoint example.com/api/test
   // this returns {"error":"invalid_token","error_description":"Invalid access token: 55e854e7b1b"}

Very good work on this lib ! It is very awesome.

boxman0617 commented 5 years ago

Would be nice to handle these the way Jest snapshots work and colocate them: cassetteName: "foo". Will try and spin up a PR with this