0

I am using OpenSearch client and I would like to test the indices.exists, however when I run jest test, I get the error openSearchClient.indices.exists is not a function. How can I access the exists property using jest? I understand the below code is trying to access mockOpenSearchClient.indices().exists() but I've looked at the documentation but can't seem to figure out how to make it mock mockOpenSearchClient.indices.exists(), below is my mock setup

const mockOpenSearchClient = {
indices: jest.fn().mockImplementation(() => {
      return {
        exists: jest.fn((obj, cb) => {
          return cb("", existsResp);
        }),
      };
    }),
} as unknown as Client;

After following the jest documentation and example here I was able to modify my setup to the below code but still no success,

const mockOpenSearchClient = {
    indices: jest.fn().mockImplementation(() => {
      return {
        exists: jest.fn().mockImplementation(() => {
          return {
            statusCode: 200,
            body: true,
          };
        }),
      };
    }),
  } as unknown as Client;

If I log mockOpenSearchClient this is what I get back

openSearchClient {
      indices: [Function: mockConstructor] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        withImplementation: [Function: bound withImplementation],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }
    }

How do I successfully access the mockOpenSearchClient.indices.exists method?

Tim Chosen
  • 41
  • 8
  • `indices` isn't a method, `exists` is a property of it not a property of its return value. But this is a really good example of why you shouldn't mock what you don't own - that's a very complicated test double, and putting your own facade over that API would be easier to mock _and_ decouple the rest of your code from the dependency. – jonrsharpe Feb 08 '23 at 21:52
  • Thanks @jonrsharpe I'm not that versed with jest mocking, I've edited the question and added additional info. What I am looking for is a way to access `mockOpenSearchClient.indices.exists()` as I understand my current approach is looking for `mockOpenSearchClient.indices().exists()` – Tim Chosen Feb 09 '23 at 12:18
  • Your test double of indices is a function, `jest.fn()`, when the real thing is an object. So change the test double - **at minimum** a test double needs to have an API compatible with what it's supposed to replace. Or, again, implement a facade to protect the rest of your code from the third party API, then create a (much simpler) test double _of the facade_ to test against. – jonrsharpe Feb 09 '23 at 12:21

1 Answers1

0

So I was finally able to mock it by simply constructing an object and then mocking the resolved value to the function:

const mockOpenSearchClient = {
   indices: {
     exists: jest.fn().mockResolvedValue(existsResp);
     get: jest.fn().mockResolvedValue(getResp);
   }
} as unknown as Client;
Tim Chosen
  • 41
  • 8