Lets say I have the following
// file sample.js
var aws = require('aws-sdk');
var dynamoDB = new aws.DynamoDB();
exports.processData = function(){
var data = dynamoDB.getItem(params);
// so something with data
};
//file sample_test.js
var aws = require('aws-sdk');
var sinon = require('sinon');
// the following code doesnt seem to stub the function
// the actual function is still used in sample.js
var getItemStub = sinon.stub();
aws.DynamoDB.prototype.getItem = getItemStub;
var sample = require('./sample');
MY current solution is to expose a function in sample.js like shown below.
function overRide(data) {
dynamoDB = data.dynamoDB;
}
if(process.env.NODE_ENV === 'test') {
exports.overRide = overRide;
}
Now in my test case I can do the following which will stub the aws api.
//file sample_test.js
var aws = require('aws-sdk');
var sinon = require('sinon');
var sample = require('./sample');
var ddb = new aws.DynamoDB();
ddb.getItem = sinon.stub();
sample.overRideAWS({dynamoDB: ddb});