skip to Main Content

I have an endpoint in API Gateway which is mapped to a Lambda function in AWS. While writing test cases for the new handler function of the endpoint, I do not want the spec file to call the actual API or connect to DynamoDB. I tried to add a sinon.stub, but it still made the call to connect to DynamoDB and the test-case failed. I am unable to locate where the stub went wrong.

Handler.js:

saveUser(userName, logger) {
  const Item = {
    id: uuid.v4(),
    userName,
    ttl: parseInt(Date.now() / 1000) + 900 // expire the name after 15 minutes from now
  };
  const params = {
    TableName: "my-table-name",
    Item
  };
  logger.log(`Saving new user name to DynamoDB: ${JSON.stringify(params)}`);

  return new Promise(function(resolve, reject) {
    db.put(params, function(err, _) {
      if (err) {
        logger.exception(`Unable to connect to DynamoDB to create: ${err}`);
        reject({
          statusCode: 404,
          err
        });
      } else {
        logger.log(`Saved data to DynamoDB: ${JSON.stringify(Item)}`);
        resolve({
          statusCode: 201,
          body: Item
        });
      }
    });
  });
}

Handler.spec.js:

import AWS from "aws-sdk";
const db = new AWS.DynamoDB.DocumentClient({
  apiVersion: "2012-08-10"
});

describe("user-name-handler", function() {
  const sandbox = sinon.createSandbox();
  afterEach(() => sandbox.restore());

  it("Test saveUser() method", async function(done) {
    const {
      saveUser
    } = userHandler;

    sandbox.stub(db, "put")
      .returns(new Promise((resolve, _) => resolve({
        statusCode: 200
      })));

    try {
      const result = await saveUser("Sample User", {
        log: () => {},
        exception: () => {}
      });

      expect(result).to.be.equal({
        data: "some data"
      });
      done();
    } catch (err) {
      console.log(err);
      done();
    }
  });
});

Error:

Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

I console-logged the err object and it gave me this error, which made me think it is trying to connect to the DynamoDB.

Error: connect ENETUNREACH 127.0.0.1:80
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',       
errno: 'ENETUNREACH',
code: 'CredentialsError',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
time: 2023-05-07T10:45:25.835Z,
originalError: {
  message: 'Could not load credentials from any providers',
  errno: 'ENETUNREACH',
  code: 'CredentialsError',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,
  time: 2023-05-07T10:45:25.835Z,
  originalError: [Object]
}

Related: How to test a method which returns data from AWS DynamoDB

2

Answers


  1. Chosen as BEST ANSWER

    The yields() function

    Instead of the stub directly return a Promise, it should be chained with a .yields() along with the parameters its callback will accept. We can change the parameters to cover various branches of the code.

    Code

    describe("user-handler connection success", function () {
        const sandbox = sinon.createSandbox();
        afterEach(() => sandbox.restore());
        before(() => {
            sinon.stub(db, "put")
                .yields(null, true);
            sinon.stub(db, "get")
                .yields(null, { sampleKey: "sample value" });
            sinon.stub(db, "delete")
                .yields(null, { sampleKey: "sample value" });
        });
        after(() => {
            db.put.restore();
            db.get.restore();
            db.delete.restore();
        });
    
        it("Test saveUser() method success", async function () {
            const result = await userHandler.saveToken("sample user", {
                log: () => {},
                exception: () => {}
            });
    
            expect(result.statusCode).to.be.equal(201);
        });
    });
    

    Useful Link

    https://www.youtube.com/watch?v=vXDbmrh0xDQ


  2. You are mocking the db that is declared in the test file – not the db that saveUser is actually using.

    The solution is to move the db declaration to its own module, say: db.js

    const AWS = require("aws-sdk");
    
    const db = new AWS.DynamoDB.DocumentClient({
      apiVersion: "2012-08-10"
    });
    
    module.exports = db;
    

    and then import it both from the module of saveUser and from the test – so that we’ll be mocking the same db instance that saveUser uses.

    UPDATE

    I was able to run the test with the following code successfully:

    The test code:

    const sinon = require('sinon');
    const { saveUser } = require('../userHandler');
    const { expect } = require('chai');
    
    const db = require('../db');
    
    describe('user-name-handler', function() {
      afterEach(() => sinon.restore());
    
      it('Test saveUser() method', async function() {
    
          sinon.stub(db, 'put')
            .returns(new Promise((resolve, _) => resolve({
                statusCode: 201,
                body: 'some data'
          })));
    
          try {
              const result = await saveUser(db, 'Sample User', {
                log: () => {},
                exception: () => {}
              });
    
              expect(result).to.deep.equal({
                statusCode: 201,
                body: 'some data'
              });
          } catch (err) {
            console.log('err', err);
          }
      });
    });
    

    userHandler file:

    const db = require('./db');
    
    const saveUser = (db, userName, logger) => {
      const Item = {
        id: uuid.v4(),
        userName,
        ttl: parseInt(Date.now() / 1000) + 900 // expire the name after 15 minutes from now
      };
      const params = {
        TableName: "my-table-name"
      };
      logger.log(`Saving new user name to DynamoDB: ${JSON.stringify(params)}`);
    
        return db.put(params, function(err, _) {
          if (err) {
            logger.exception(`Unable to connect to DynamoDB to create: ${err}`);
            return reject({
              statusCode: 404,
              err
            });
          } else {
            logger.log(`Saved data to DynamoDB: ${JSON.stringify(Item)}`);
            return resolve({
              statusCode: 201,
              body: Item
            });
          }
        });
    }
    
    module.exports = { saveUser };
    

    package.json

    {
      "name": "play",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "mocha --timeout 5000"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "aws-sdk": "^2.1373.0",
        "chai": "^4.3.7",
        "mocha": "^10.2.0",
        "sinon": "^15.0.4"
      }
    }
    

    OUTPUT
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search