score:0

assuming you import the getinitialdata function from somewhere, a mock for that would look like this

  jest.mock("./getinitialdata", () =>
    jest.fn().mockresolvedvalue(initialresponsemock)
  );
  // rest of your test in here

the concept you are missing is that you need to use mockresolvedvalue when mocking async code.

https://blog.jimmydc.com/mock-asynchronous-functions-with-jest/

score:2

this is the way i use the jest to test something after the api has finished fetching.

create mock function, (may be in other file to be used by all test cases, change it the way you want to setup)

const mockapi = new promise((resolve, reject) => {
  settimeout(() => {
    resolve('foo');
  }, 300);
});

define apis

 import * as apis from '~/api/[api_path]';

  const mockgetinitialdata = (params = {}) =>
    jest
      .spyon(apis, 'getinitialdata')
      .mockimplementation(() => mockapi(params));

test case

it('initial data loading failed', async () => {
    mockgetinitialdata({
      error: { errors: {name: 'invalid name' } }
    });
});

Related Query

More Query from same tag