Monday 12 June 2017

simple aws-sdk-mock times out without calling callback

I am currently writing some unit tests in nodejs using the mocha framework. Recently, I've been trying to learn how to mock the Amazon Web Services SDK, using the aws-sdk-mock module and the explanations on the same page.
I have come up with a short test which is supposed to mock the DynamoDB DocumentClient 'get' operation, call it once, and log the return value. Here is the code:

var AWS = require('aws-sdk');
var AwsMock = require('aws-sdk-mock');
describe("xyz", function() {
    it('should call "get" on mock database', function(done) {
        AwsMock.mock('DynamoDB.DocumentClient', 'get', function(params, callback) {
            callback(null, {
                Item: {
                    Key: 'Value'
                }
            });
        });
        var dynamoDb = new AWS.DynamoDB.DocumentClient();
        dynamoDb.get({}, function(err, data) {
            console.log('data: ' + JSON.stringify(data));
            done();
        });
    });
});

However, for some reason that is escaping me, this test keeps failing with a timeout error, as the callback error is seemingly never called. This is the full test result:

xyz
    1) should call "get" on mock database

  0 passing (2s)
  1 failing

  1) xyz should call "get" on mock database:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

npm ERR! Test failed.  See above for more details.

I believe I followed the explanations on the aws-sdk-mock page pretty closely (I copied the mock part without changes), and I cannot find the fault in this code. I searched google and Stackoverflow, but couldn't find a similar question, so can anyone tell me where the problem is?

Thanks in advance.



via deadpool

No comments:

Post a Comment