最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jasmine: How to test a fetch in a fetch? - Stack Overflow

programmeradmin1浏览0评论

I am looking to test nested fetches. So far I can test the result of the first fetch but can't access the other one. I have tried nesting the next fetch call in the first but that does not seem to work. The second SpyOn function call creates a jasmine errors saying I can't have multiple spys for the same function.

Jasmine test

  let response = new Response(
  JSON.stringify({
    test: "test",
  })
);

// creating a fetch spy
spyOn(window, "fetch").and.returnValue(Promise.resolve(response));

// make call to fetch
callFirstFetch().then((result) => {
  expect(window.fetch).toHaveBeenCalledWith("/test");

  spyOn(window, "fetch").and.returnValue(Promise.resolve(response));

  done();
});

and the nested fetch code looks like

function firstFetch(() => {
   return fetch("/test")
        .then(response => { 
                           response.json();
                           return secondFetch(); 
        })
});

function secondFetch(() => {
   return fetch("/test2")
        .then(response => { 
                           response.json();
                           console.log('done');
        })
});
 
firstFetch();

I am looking to test nested fetches. So far I can test the result of the first fetch but can't access the other one. I have tried nesting the next fetch call in the first but that does not seem to work. The second SpyOn function call creates a jasmine errors saying I can't have multiple spys for the same function.

Jasmine test

  let response = new Response(
  JSON.stringify({
    test: "test",
  })
);

// creating a fetch spy
spyOn(window, "fetch").and.returnValue(Promise.resolve(response));

// make call to fetch
callFirstFetch().then((result) => {
  expect(window.fetch).toHaveBeenCalledWith("/test");

  spyOn(window, "fetch").and.returnValue(Promise.resolve(response));

  done();
});

and the nested fetch code looks like

function firstFetch(() => {
   return fetch("/test")
        .then(response => { 
                           response.json();
                           return secondFetch(); 
        })
});

function secondFetch(() => {
   return fetch("/test2")
        .then(response => { 
                           response.json();
                           console.log('done');
        })
});
 
firstFetch();
Share Improve this question edited Feb 14, 2021 at 22:44 Mohammad Yaser Ahmadi 5,0514 gold badges20 silver badges44 bronze badges asked Feb 14, 2021 at 22:33 user2301893user2301893 1211 silver badge3 bronze badges 2
  • What's wrong with expect(window.fetch).toHaveBeenCalledWith("/test"); expect(window.fetch).toHaveBeenCalledWith("/test2"); or count the # calls to fetch? – Randy Casburn Commented Feb 14, 2021 at 22:52
  • Why would you want to test mocked return data from an API that is internal. It is a waste of time/resources testing the internals. – Randy Casburn Commented Feb 14, 2021 at 23:48
Add a ment  | 

1 Answer 1

Reset to default 6

This is how you can mock your fetch api call:

const okResponse = new Response(JSON.stringify({}), { status: 200, statusText: 'OK', });
spyOn(window, 'fetch').and.resolveTo(okResponse);

And then test it like this:

expect(window.fetch).toHaveBeenCalledWith(requestUrl, options);
发布评论

评论列表(0)

  1. 暂无评论