13
13
* - mock of the method on the service
14
14
**/
15
15
16
- import sinon from 'sinon'
16
+ import sinon , { SinonStubStatic } from 'sinon'
17
17
import traverse from 'traverse'
18
18
import { default as _AWS_SDK } from 'aws-sdk' ;
19
19
import { Readable } from 'stream'
20
20
21
21
import AWS_Clients from 'aws-sdk/clients/all'
22
22
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' ;
23
25
24
26
// TYPES -----------------------------------
25
27
// AWS type that is to serve as a mock
@@ -40,10 +42,11 @@ type MethodMock = {
40
42
}
41
43
42
44
interface Service {
43
- Constructor : Partial < typeof AWS_Clients > ,
45
+ Constructor : new ( ... args : any [ ] ) => any ,
44
46
methodMocks : MethodMock ,
45
47
invoked : boolean ,
46
- clients ?: Client < ClientName > [ ]
48
+ clients ?: Client < ClientName > [ ] ,
49
+ stub ?: SinonStubStatic
47
50
}
48
51
49
52
type SERVICES < T extends string > = {
@@ -139,30 +142,42 @@ AWS.mock = function<C extends ClientName, M extends MethodName<C> & string>(serv
139
142
* Stub the constructor for the service on AWS.
140
143
* E.g. calls of new AWS.SNS() are replaced.
141
144
*/
142
- function mockService ( service ) {
143
- const nestedServices = service . split ( '.' ) ;
145
+ function mockService ( service : ClientName ) {
146
+ const nestedServices : string [ ] = service . split ( '.' ) ;
147
+
144
148
const method = nestedServices . pop ( ) ;
145
149
const object = traverse ( _AWS ) . get ( nestedServices ) ;
146
150
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
+
166
181
} ;
167
182
168
183
/**
0 commit comments