1function mockFetch(data) {
2 return jest.fn().mockImplementation(() =>
3 Promise.resolve({
4 ok: true,
5 json: () => data
6 })
7 );
8}
9
10test('fetchPerson()', async () => {
11 fetch = mockFetch(someJson); // or window.fetch
12
13 const person = await fetchPerson('whatever id');
14 expect(person).toEqual(someJson);
15
16 // Make sure fetch has been called exactly once
17 expect(fetch).toHaveBeenCalledTimes(1);
18});