showing results for - "mock sqs sendmessage node js"
Alexander
08 Sep 2018
1import sendMessage from './';
2import AWS from 'aws-sdk';
3
4jest.mock('aws-sdk', () => {
5  const SQSMocked = {
6    sendMessage: jest.fn().mockReturnThis(),
7    promise: jest.fn()
8  };
9  return {
10    SQS: jest.fn(() => SQSMocked)
11  };
12});
13
14const sqs = new AWS.SQS({
15  region: 'us-east-1'
16});
17
18describe.only('Test case for SQS SendMessage', () => {
19  beforeEach(() => {
20    (sqs.sendMessage().promise as jest.MockedFunction<any>).mockReset();
21  });
22  it('should return the UserEvent', async () => {
23    expect(jest.isMockFunction(sqs.sendMessage)).toBeTruthy();
24    expect(jest.isMockFunction(sqs.sendMessage().promise)).toBeTruthy();
25    (sqs.sendMessage().promise as jest.MockedFunction<any>).mockResolvedValueOnce('mocked data');
26    const actualValue = await sendMessage('testURL', 'data');
27    expect(actualValue).toEqual('mocked data');
28    expect(sqs.sendMessage).toBeCalledWith({ MessageBody: '"testURL"', QueueUrl: 'data' });
29    expect(sqs.sendMessage().promise).toBeCalledTimes(1);
30  });
31
32  it('should throw an error when send message error', async () => {
33    const sendMessageErrorMessage = 'network error';
34    (sqs.sendMessage().promise as jest.MockedFunction<any>).mockRejectedValueOnce(sendMessageErrorMessage);
35    await expect(sendMessage('testURL', 'data')).rejects.toThrowError(new Error(sendMessageErrorMessage));
36    expect(sqs.sendMessage).toBeCalledWith({ MessageBody: '"testURL"', QueueUrl: 'data' });
37    expect(sqs.sendMessage().promise).toBeCalledTimes(1);
38  });
39});