-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
74 lines (62 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var events = require('events');
var sinon = require('sinon');
var _AWS = require('aws-sdk');
var traverse = require('traverse');
module.exports = _AWS;
module.exports.stub = stubMethod;
/**
* Replaces a single AWS service method with a stub.
*
* @param {string} service - the name of the AWS service. Can include `.` for
* nested services, e.g. `'DynamoDB.DocumentClient'`.
* @param {string} method - the name of the service method to stub.
* @param {function} [replacement] - if specified, this function will be called
* when the service method stub is invoked. `this` in the context of the function
* will provide a reference to stubbed AWS.Request and AWS.Response objects to
* simulate more advanced aws-sdk-js usage patterns.
* @returns {object} stub - [a sinon stub](http://sinonjs.org/docs/#stubs).
*/
function stubMethod(service, method, replacement) {
if (!isStubbed(service)) stubService(service);
if (!replacement) return sinon.stub(getService(service).prototype, method);
return sinon.stub(getService(service).prototype, method).callsFake(function(params, callback) {
var _this = { request: stubRequest(), response: stubResponse() };
replacement.call(_this, params, callback);
return _this.request;
});
}
function isStubbed(service) {
return getService(service).isSinonProxy;
}
function getService(name) {
return traverse(_AWS).get(name.split('.'));
}
function setService(name, fn) {
traverse(_AWS).set(name.split('.'), fn);
}
function stubService(service) {
var Original = getService(service);
var client = new Original();
function FakeService(config) { Object.assign(this, new Original(config)); }
FakeService.prototype = Object.assign(
{},
client.__proto__.__proto__.__proto__,
client.__proto__.__proto__,
client.__proto__
);
var spy = sinon.spy(FakeService);
spy.restore = function() { setService(service, Original); };
setService(service, spy);
}
function stubRequest() {
var req = new events.EventEmitter();
var stubbed = sinon.createStubInstance(_AWS.Request);
for (var method in req.__proto__) delete stubbed[method];
return Object.assign(req, stubbed);
}
function stubResponse() {
var req = new events.EventEmitter();
var stubbed = sinon.createStubInstance(_AWS.Response);
for (var method in req.__proto__) delete stubbed[method];
return Object.assign(req, stubbed);
}