Struggling to find the right approach for unit testing this function. I almost have it, but can't quite nail it down.
Take a look at the function below:
receiveMessage(callback: Function): any {
this.sqs.receiveMessage(
this.params,
(err: AWSError, data: ReceiveMessageResult) => callback(err, data)
);
}
When it comes to testing, I've set up an initial AWS mock and then customized it in the test:
jest.mock('aws-sdk', () => {
const SQSMocked = {
receiveMessage: jest.fn().mockReturnThis()
};
return {
SQS: jest.fn(() => SQSMocked),
config: {
update: jest.fn(() => {
return { region: getConfig().region };
})
}
};
});
const sqs = new AWS.SQS({
region: 'us-east-1'
});
Next steps involve executing the actual test. I feel like my issue lies in how I'm overriding the receiveMessage on the aws sdk.
it('verifies that the callback is invoked', (done) => {
const callback = (err: AWSError, data: ReceiveMessageResult) => {
console.log('called');
done();
};
// Unsure about how to trigger the callback on the mock???
sqs.receiveMessage = jest.fn().mockReturnValue(callback);
sqsQueue.receiveMessage(callback);
expect(callback).toHaveBeenCalled();
});
I've attempted various approaches to modifying the aws sdk, sometimes encountering TypeScript errors, but haven't quite cracked how to mock the function callback.
Any suggestions?