How to mock the v3 aws-sdk's paginators?

180 Views Asked by At

I'm migrating some old aws-sdk v2 code to the new v3 and found some pretty neat new paginator logic. I wanted to use that but am having trouble unit testing it which is making this process take much longer than I was hoping.

Here is the logic I'm trying to implement & test:

const getMyApis = async (): Promise<RestApi[]> => {
  const paginatorConfig = {
    client: new APIGatewayClient({}),
    pageSize: 25,
  };

  const apis: RestApi[] = [];
  for await (const page of paginateGetRestApis(paginatorConfig, {})) {
    if (!page.items) return apis;
    apis.push(...page.items);
  }
  return apis;
};

The issue I'm running up against is likely some misunderstanding on how these asyncGenerators work, but it seems it should be reasonably straightforward.

However, I cannot seem to mock the return value nor the implementation correctly due to some really complex typing I'm having a hard time wrapping my head around. I'm going to e2e test this logic further up so I'm not super concerned with the exact typing, but even trying to cast the types is giving me grief. Here is the basic premise of my test:

    const mockApiItems: RestApi[] = [
      {
        name: 'bumpy_dove',
      },
    ];
    const mockRestApis: RestApis = {
      items: mockApiItems,
    };

    const mockPaginageGetRestApis = mocked(paginateGetRestApis, true);
    
    // **Here I was trying to mock the implementation**
    // mockPaginageGetRestApis.mockImplementation(() => mockRestApis as any);

    mockPaginageGetRestApis.mockReturnValue(mockRestApis as any);

    const apisReturned = await getAllRestApisV3();

    expect(apisReturned).toStrictEqual(mockApiItems);

Neither mock implementation nor return value works for me and gives me the following error

TypeError: (0, client_api_gateway_1.paginateGetRestApis)(...) is not a function or its return value is not async iterable

which I can see and certainly understand... but I'm just trying to mock this at a unit level and move on to test the actual API call in my e2es.

0

There are 0 best solutions below