I am working on an API in Typescript using Node and Express.
I have the following mock implementation for an API key manager class in Jest:
import {ApiKeyManager} from '../../api_key_manager_client/api_key_manager';
jest.mock('../../api_key_manager_client/api_key_manager', () => {
return {
ApiKeyManager: jest.fn().mockImplementation(() => {
return {
secretId: 'WEBHOOKS_API_KEY',
getApiKey: () => 'xxx',
};
}),
};
});
and I'm testing as follows:
it('should run the callback endpoint class when the given API key matches', async () => {
const payload = {...};
const res = await request(app)
.post('/callback')
.set({'X-API-KEY': 'xxx', 'Accept': 'application/json', 'Content-Type': 'application/json'})
.send(payload);
expect(res.statusCode).toEqual(201);
expect(ApiKeyManager).toHaveBeenCalledTimes(1);
});
This works and I can test successfully that the ApiKeyManager class is called.
However, I'd like to check that the public getApiKey method in that class is called specifically, but I'm not sure how to test that the mock received that particular method call? It should happen once so I'd like to test that. Thanks for any help.