Skip to content

Commit 075a4a1

Browse files
committed
feat: Typing mockService function.
1 parent 5bcbc2e commit 075a4a1

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

index.ts

+39-24
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* - mock of the method on the service
1414
**/
1515

16-
import sinon from 'sinon'
16+
import sinon, { SinonStubStatic } from 'sinon'
1717
import traverse from 'traverse'
1818
import {default as _AWS_SDK} from 'aws-sdk';
1919
import {Readable} from 'stream'
2020

2121
import AWS_Clients from 'aws-sdk/clients/all'
2222
import { ReplaceFn, ClientName, MethodName, mock, remock, restore, setSDK, setSDKInstance, Client } from '.';
23+
import { ServiceName } from 'aws-sdk/clients/inspector';
24+
import { String } from 'aws-sdk/clients/cloudsearch';
2325

2426
// TYPES -----------------------------------
2527
// AWS type that is to serve as a mock
@@ -40,10 +42,11 @@ type MethodMock = {
4042
}
4143

4244
interface Service {
43-
Constructor: Partial<typeof AWS_Clients>,
45+
Constructor: new(...args: any[]) => any,
4446
methodMocks: MethodMock,
4547
invoked: boolean,
46-
clients?: Client<ClientName>[]
48+
clients?: Client<ClientName>[],
49+
stub?: SinonStubStatic
4750
}
4851

4952
type SERVICES<T extends string> = {
@@ -139,30 +142,42 @@ AWS.mock = function<C extends ClientName, M extends MethodName<C> & string>(serv
139142
* Stub the constructor for the service on AWS.
140143
* E.g. calls of new AWS.SNS() are replaced.
141144
*/
142-
function mockService(service) {
143-
const nestedServices = service.split('.');
145+
function mockService(service: ClientName) {
146+
const nestedServices: string[] = service.split('.');
147+
144148
const method = nestedServices.pop();
145149
const object = traverse(_AWS).get(nestedServices);
146150

147-
const serviceStub = sinon.stub(object, method).callsFake(function(...args) {
148-
services[service].invoked = true;
149-
150-
/**
151-
* Create an instance of the service by calling the real constructor
152-
* we stored before. E.g. const client = new AWS.SNS()
153-
* This is necessary in order to mock methods on the service.
154-
*/
155-
const client = new services[service].Constructor(...args);
156-
services[service].clients = services[service].clients || [];
157-
services[service].clients.push(client);
158-
159-
// Once this has been triggered we can mock out all the registered methods.
160-
for (const key in services[service].methodMocks) {
161-
mockServiceMethod(service, client, key, services[service].methodMocks[key].replace);
162-
};
163-
return client;
164-
});
165-
services[service].stub = serviceStub;
151+
// Method type guard
152+
if(!method)
153+
return
154+
155+
const service_obj = services[service]
156+
157+
if(service_obj) {
158+
const serviceStub = sinon.stub(object, method).callsFake(function(...args) {
159+
160+
service_obj.invoked = true;
161+
162+
/**
163+
* Create an instance of the service by calling the real constructor
164+
* we stored before. E.g. const client = new AWS.SNS()
165+
* This is necessary in order to mock methods on the service.
166+
*/
167+
const client = new service_obj.Constructor(...args);
168+
service_obj.clients = service_obj.clients || [];
169+
service_obj.clients.push(client);
170+
171+
// Once this has been triggered we can mock out all the registered methods.
172+
for (const key in service_obj.methodMocks) {
173+
mockServiceMethod(service, client, key, service_obj.methodMocks[key].replace);
174+
};
175+
return client;
176+
});
177+
service_obj.stub = serviceStub;
178+
}
179+
180+
166181
};
167182

168183
/**

0 commit comments

Comments
 (0)