Testing async SQS sendMessage

Salvatore Q Zeroastro picture Salvatore Q Zeroastro · Jan 18, 2019 · Viewed 16.6k times · Source

I have an async lambda, which performs an async SQS sendMessage request. The SQS queue is a standard queue, not FIFO, just to clarify.

Here's an example of code (without irrelevant part of the logic):

exports.functionHandler = async (event, context, callback) => {
    try {
        let parsedBody = JSON.parse(event.Records[0].body);
        let modifiedBody = await doStuff(parsedBody);

        let sqsPayload = {
            MessageBody: JSON.stringify(modifiedBody),
            QueueUrl: my-queue-url
        };

        await sqs.sendMessage(sqsPayload).promise();

        callback(null, utils.respondSuccess("Done"));
    } catch (err) {
        // Handle error

        callback(null, utils.respondError(err));
    }
};

const doStuff = async payload => {
    // Do stuff 
}

Pretty simple.

Now the problem: I'm trying to test this function using the package aws-sdk-mock. This is how I was stubbing the sendMessage function when the lambda wasn't async and the sendMessage function was using the callback:

it("an awesome title for my test", async () => {
    let payload = {
        Records: [
            // Data here
        ]
    };

    AWS.mock("SQS", "sendMessage", (param, callback) => {
        let response = {
            ResponseMetadata: {
                RequestId: "test-request-id"
            },
            MD5OfMessageBody: "a892e8d8589e97ca92fb70020f01c16c",
            MessageId: "test-message-id"
        };

        callback(null, response);
    });

    await app.functionHandler(payload, {}, (err, result) => {
        let parsedBody = JSON.parse(result.body);
        expect(parsedBody.message).to.be.equal("Done");
        // More stuff
    });

    AWS.restore();
});

If I use this test, the sendMessage function throws the following error:

sendMessage returned an invalid MD5 response. Got "undefined", expecting "a892e8d8589e97ca92fb70020f01c16c".

I'm not sure how to test sendMessage asynchronously. I don't mind adopting a different package if it helps me to get the job done.

Can anyone help?

Thanks a lot

Answer

A.Khan picture A.Khan · Jan 19, 2019

I've not used aws-sdk-mock but apparently in your mock you are using callback and in the lambda handler it is an async call. I use proxyquire for mocking dependencies. Here is an example:

functionHandler.js

Don't need to use callback and context in Lambda runtime Node8.10.

let AWSSQS = require('aws-sdk/clients/sqs');
let sqs = new AWSSQS();

exports.functionHandler = async (event) => {

  // No need to use callback when Lambda runtime is 8.10.
  try {

    let parsedBody = JSON.parse(event.Records[0].body);
    let modifiedBody = await doStuff(parsedBody);

    let sqsPayload = {
      MessageBody: JSON.stringify(modifiedBody),
      QueueUrl: my-queue-url
    };

    await sqs.sendMessage(sqsPayload).promise();

    return utils.respondSuccess('Done');
  } catch (err) {
    throw utils.respondError(err);
  }
};

test.spec.js

Pretty much self explanatory. Your define an object with name of dependency as property.

const proxyquire = require('proxyquire');

let app = require('path/to/function');

describe('SQS', () => {

  it("an awesome title for my test", async (done) => {

    const app = proxyquire(app, {
      'aws-sdk/clients/sqs': function() {
        this.sendMessage = (params) => {
          return {
            promise: () => {
              return Promise.resolve({
                ResponseMetadata: {
                  RequestId: 'test-request-id'
                },
                MD5OfMessageBody: 'a892e8d8589e97ca92fb70020f01c16c',
                MessageId: 'test-message-id'
              });
            }
          }
        }
      }
    });

    let payload = {
      Records: [
        // Data here
      ]
    };

    const data = await app.functionHandler(payload);
    let parsedBody = JSON.parse(data.body);
    expect(parsedBody.message).to.be.equal("Done");
    done();
  });
});