aws-sdk-client-mock
Advanced tools
Comparing version 4.0.0 to 4.0.1-beta.0
@@ -72,2 +72,18 @@ "use strict"; | ||
} | ||
/** | ||
* Allows specifying the behavior for any Command with given input (parameters). | ||
* | ||
* If the input is not specified, the given behavior will be used for any Command with any input. | ||
* | ||
* Calling `onAnyCommand()` without parameters is not required to specify the default behavior for any Command, | ||
* but can be used for readability. | ||
* | ||
* @example | ||
* ```js | ||
* clientMock.onAnyCommand().resolves({}) | ||
* ``` | ||
* | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
onAnyCommand(input, strict = false) { | ||
@@ -77,2 +93,18 @@ const cmdStub = this.send.withArgs(this.createInputMatcher(input, strict)); | ||
} | ||
/** | ||
* Allows specifying the behavior for a given Command type and its input (parameters). | ||
* | ||
* If the input is not specified, it will match any Command of that type. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand, {Message: 'My message'}) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param command Command type to match | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
on(command, input, strict = false) { | ||
@@ -88,17 +120,125 @@ const matcher = sinon_1.match.instanceOf(command).and(this.createInputMatcher(input, strict)); | ||
} | ||
/** | ||
* Sets a successful response that will be returned from any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolves(response) { | ||
return this.onAnyCommand().resolves(response); | ||
} | ||
/** | ||
* Sets a successful response that will be returned from one `Client#send()` invocation. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `resolvesOnce()` responses to use, invocations will return a response specified by `resolves()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolvesOnce(response) { | ||
return this.onAnyCommand().resolvesOnce(response); | ||
} | ||
/** | ||
* Sets a failure response that will be returned from any `Client#send()` invocation. | ||
* The response will always be an `Error` instance. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejects(error) { | ||
return this.onAnyCommand().rejects(error); | ||
} | ||
/** | ||
* Sets a failure response that will be returned from one `Client#send()` invocation. | ||
* The response will always be an `Error` instance. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `rejectsOnce()` responses to use, invocations will return a response specified by `rejects()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejectsOnce(error) { | ||
return this.onAnyCommand().rejectsOnce(error); | ||
} | ||
/** | ||
* Sets a function that will be called on any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFake(fn) { | ||
return this.onAnyCommand().callsFake(fn); | ||
} | ||
/** | ||
* Sets a function that will be called once, on any `Client#send()` invocation. | ||
* | ||
* Can be chained so that successive invocations call different functions. When there are no more | ||
* `callsFakeOnce()` functions to use, invocations will call a function specified by `callsFake()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFakeOnce(fn) { | ||
@@ -125,8 +265,28 @@ return this.onAnyCommand().callsFakeOnce(fn); | ||
} | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
onAnyCommand(input, strict) { | ||
return this.clientStub.onAnyCommand(input, strict); | ||
} | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
on(command, input, strict = false) { | ||
return this.clientStub.on(command, input, strict); | ||
} | ||
/** | ||
* Sets a successful response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolves(response) { | ||
@@ -136,2 +296,19 @@ this.send.resolves(response); | ||
} | ||
/** | ||
* Sets a successful response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `resolvesOnce()` responses to use, invocations will return a response specified by `resolves()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolvesOnce(response) { | ||
@@ -141,2 +318,24 @@ this.send = this.send.onCall(this.nextChainableCallNumber++).resolves(response); | ||
} | ||
/** | ||
* Sets a failure response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejects(error) { | ||
@@ -146,2 +345,20 @@ this.send.rejects(CommandBehavior.normalizeError(error)); | ||
} | ||
/** | ||
* Sets a failure response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `rejectsOnce()` responses to use, invocations will return a response specified by `rejects()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejectsOnce(error) { | ||
@@ -160,2 +377,32 @@ this.send.onCall(this.nextChainableCallNumber++).rejects(CommandBehavior.normalizeError(error)); | ||
} | ||
/** | ||
* Sets a function that will be called on `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFake(fn) { | ||
@@ -165,2 +412,19 @@ this.send.callsFake(cmd => this.fakeFnWrapper(cmd, fn)); | ||
} | ||
/** | ||
* Sets a function that will be called once on `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations call different functions. When there are no more | ||
* `callsFakeOnce()` functions to use, invocations will call a function specified by `callsFake()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFakeOnce(fn) { | ||
@@ -167,0 +431,0 @@ this.send.onCall(this.nextChainableCallNumber++).callsFake(cmd => this.fakeFnWrapper(cmd, fn)); |
@@ -70,2 +70,18 @@ import { match } from 'sinon'; | ||
}; | ||
/** | ||
* Allows specifying the behavior for any Command with given input (parameters). | ||
* | ||
* If the input is not specified, the given behavior will be used for any Command with any input. | ||
* | ||
* Calling `onAnyCommand()` without parameters is not required to specify the default behavior for any Command, | ||
* but can be used for readability. | ||
* | ||
* @example | ||
* ```js | ||
* clientMock.onAnyCommand().resolves({}) | ||
* ``` | ||
* | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
AwsStub.prototype.onAnyCommand = function (input, strict) { | ||
@@ -76,2 +92,18 @@ if (strict === void 0) { strict = false; } | ||
}; | ||
/** | ||
* Allows specifying the behavior for a given Command type and its input (parameters). | ||
* | ||
* If the input is not specified, it will match any Command of that type. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand, {Message: 'My message'}) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param command Command type to match | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
AwsStub.prototype.on = function (command, input, strict) { | ||
@@ -89,17 +121,125 @@ if (strict === void 0) { strict = false; } | ||
}; | ||
/** | ||
* Sets a successful response that will be returned from any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
AwsStub.prototype.resolves = function (response) { | ||
return this.onAnyCommand().resolves(response); | ||
}; | ||
/** | ||
* Sets a successful response that will be returned from one `Client#send()` invocation. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `resolvesOnce()` responses to use, invocations will return a response specified by `resolves()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
AwsStub.prototype.resolvesOnce = function (response) { | ||
return this.onAnyCommand().resolvesOnce(response); | ||
}; | ||
/** | ||
* Sets a failure response that will be returned from any `Client#send()` invocation. | ||
* The response will always be an `Error` instance. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
AwsStub.prototype.rejects = function (error) { | ||
return this.onAnyCommand().rejects(error); | ||
}; | ||
/** | ||
* Sets a failure response that will be returned from one `Client#send()` invocation. | ||
* The response will always be an `Error` instance. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `rejectsOnce()` responses to use, invocations will return a response specified by `rejects()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
AwsStub.prototype.rejectsOnce = function (error) { | ||
return this.onAnyCommand().rejectsOnce(error); | ||
}; | ||
/** | ||
* Sets a function that will be called on any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
AwsStub.prototype.callsFake = function (fn) { | ||
return this.onAnyCommand().callsFake(fn); | ||
}; | ||
/** | ||
* Sets a function that will be called once, on any `Client#send()` invocation. | ||
* | ||
* Can be chained so that successive invocations call different functions. When there are no more | ||
* `callsFakeOnce()` functions to use, invocations will call a function specified by `callsFake()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
AwsStub.prototype.callsFakeOnce = function (fn) { | ||
@@ -128,5 +268,13 @@ return this.onAnyCommand().callsFakeOnce(fn); | ||
} | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
CommandBehavior.prototype.onAnyCommand = function (input, strict) { | ||
return this.clientStub.onAnyCommand(input, strict); | ||
}; | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
CommandBehavior.prototype.on = function (command, input, strict) { | ||
@@ -136,2 +284,14 @@ if (strict === void 0) { strict = false; } | ||
}; | ||
/** | ||
* Sets a successful response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
CommandBehavior.prototype.resolves = function (response) { | ||
@@ -141,2 +301,19 @@ this.send.resolves(response); | ||
}; | ||
/** | ||
* Sets a successful response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `resolvesOnce()` responses to use, invocations will return a response specified by `resolves()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
CommandBehavior.prototype.resolvesOnce = function (response) { | ||
@@ -146,2 +323,24 @@ this.send = this.send.onCall(this.nextChainableCallNumber++).resolves(response); | ||
}; | ||
/** | ||
* Sets a failure response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
CommandBehavior.prototype.rejects = function (error) { | ||
@@ -151,2 +350,20 @@ this.send.rejects(CommandBehavior.normalizeError(error)); | ||
}; | ||
/** | ||
* Sets a failure response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `rejectsOnce()` responses to use, invocations will return a response specified by `rejects()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
CommandBehavior.prototype.rejectsOnce = function (error) { | ||
@@ -165,2 +382,32 @@ this.send.onCall(this.nextChainableCallNumber++).rejects(CommandBehavior.normalizeError(error)); | ||
}; | ||
/** | ||
* Sets a function that will be called on `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
CommandBehavior.prototype.callsFake = function (fn) { | ||
@@ -171,2 +418,19 @@ var _this = this; | ||
}; | ||
/** | ||
* Sets a function that will be called once on `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations call different functions. When there are no more | ||
* `callsFakeOnce()` functions to use, invocations will call a function specified by `callsFake()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
CommandBehavior.prototype.callsFakeOnce = function (fn) { | ||
@@ -173,0 +437,0 @@ var _this = this; |
@@ -5,3 +5,66 @@ import { Client, Command, MetadataBearer } from '@smithy/types'; | ||
export interface Behavior<TInput extends object, TOutput extends MetadataBearer, TCommandOutput extends TOutput, TConfiguration> { | ||
onAnyCommand<TCmdInput extends TInput>(input?: Partial<TCmdInput>, strict?: boolean): Behavior<TInput, TOutput, TOutput, TConfiguration>; | ||
on<TCmdInput extends TInput, TCmdOutput extends TOutput>(command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>, input?: Partial<TCmdInput>, strict?: boolean): Behavior<TInput, TOutput, TCmdOutput, TConfiguration>; | ||
resolves(response: CommandResponse<TCommandOutput>): AwsStub<TInput, TOutput, TConfiguration>; | ||
resolvesOnce(response: CommandResponse<TCommandOutput>): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
rejects(error?: string | Error | AwsError): AwsStub<TInput, TOutput, TConfiguration>; | ||
rejectsOnce(error?: string | Error | AwsError): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
callsFake(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): AwsStub<TInput, TOutput, TConfiguration>; | ||
callsFakeOnce(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
} | ||
/** | ||
* Type for {@link AwsStub} class, | ||
* but with the AWS Client class type as an only generic parameter. | ||
* | ||
* @example | ||
* ```ts | ||
* let snsMock: AwsClientStub<SNSClient>; | ||
* snsMock = mockClient(SNSClient); | ||
* ``` | ||
*/ | ||
export type AwsClientStub<TClient> = TClient extends Client<infer TInput, infer TOutput, infer TConfiguration> ? AwsStub<TInput, TOutput, TConfiguration> : never; | ||
/** | ||
* Wrapper on the mocked `Client#send()` method, | ||
* allowing to configure its behavior. | ||
* | ||
* Without any configuration, `Client#send()` invocation returns `undefined`. | ||
* | ||
* To define resulting variable type easily, use {@link AwsClientStub}. | ||
*/ | ||
export declare class AwsStub<TInput extends object, TOutput extends MetadataBearer, TConfiguration> implements Behavior<TInput, TOutput, TOutput, TConfiguration> { | ||
private client; | ||
/** | ||
* Underlying `Client#send()` method Sinon stub. | ||
* | ||
* Install `@types/sinon` for TypeScript typings. | ||
*/ | ||
send: SinonStub<[AwsCommand<TInput, TOutput>], Promise<TOutput>>; | ||
constructor(client: Client<TInput, TOutput, TConfiguration>, send: SinonStub<[AwsCommand<TInput, TOutput>], Promise<TOutput>>); | ||
/** Returns the class name of the underlying mocked client class */ | ||
clientName(): string; | ||
/** | ||
* Resets stub. It will replace the stub with a new one, with clean history and behavior. | ||
*/ | ||
reset(): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** Resets stub's calls history. */ | ||
resetHistory(): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** Replaces stub with original `Client#send()` method. */ | ||
restore(): void; | ||
/** | ||
* Returns recorded calls to the stub. | ||
* Clear history with {@link resetHistory} or {@link reset}. | ||
*/ | ||
calls(): SinonSpyCall<[AwsCommand<TInput, TOutput>], Promise<TOutput>>[]; | ||
/** | ||
* Returns n-th recorded call to the stub. | ||
*/ | ||
call(n: number): SinonSpyCall<[AwsCommand<TInput, TOutput>], Promise<TOutput>>; | ||
/** | ||
* Returns recorded calls of given Command only. | ||
* @param commandType Command type to match | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
commandCalls<TCmd extends AwsCommand<any, any>, TCmdInput extends TCmd extends AwsCommand<infer TIn, any> ? TIn : never, TCmdOutput extends TCmd extends AwsCommand<any, infer TOut> ? TOut : never>(commandType: new (input: TCmdInput) => TCmd, input?: Partial<TCmdInput>, strict?: boolean): SinonSpyCall<[TCmd], Promise<TCmdOutput>>[]; | ||
/** | ||
* Allows specifying the behavior for any Command with given input (parameters). | ||
@@ -15,16 +78,10 @@ * | ||
* @example | ||
* ```ts | ||
* clientMock.onAnyCommand().resolves(123) | ||
* ```js | ||
* clientMock.onAnyCommand().resolves({}) | ||
* ``` | ||
* | ||
* is same as: | ||
* | ||
* ```ts | ||
* clientMock.resolves(123) | ||
* ``` | ||
* | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
onAnyCommand<TCmdInput extends TInput>(input?: Partial<TCmdInput>, strict?: boolean): Behavior<TInput, TOutput, TOutput, TConfiguration>; | ||
onAnyCommand<TCmdInput extends TInput>(input?: Partial<TCmdInput>, strict?: boolean): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
/** | ||
@@ -35,2 +92,9 @@ * Allows specifying the behavior for a given Command type and its input (parameters). | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand, {Message: 'My message'}) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param command Command type to match | ||
@@ -40,9 +104,16 @@ * @param input Command payload to match | ||
*/ | ||
on<TCmdInput extends TInput, TCmdOutput extends TOutput>(command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>, input?: Partial<TCmdInput>, strict?: boolean): Behavior<TInput, TOutput, TCmdOutput, TConfiguration>; | ||
on<TCmdInput extends TInput, TCmdOutput extends TOutput>(command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>, input?: Partial<TCmdInput>, strict?: boolean): CommandBehavior<TInput, TOutput, TCmdOutput, TConfiguration>; | ||
private createInputMatcher; | ||
/** | ||
* Sets a successful response that will be returned from any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolves(response: CommandResponse<TCommandOutput>): AwsStub<TInput, TOutput, TConfiguration>; | ||
resolves(response: CommandResponse<TOutput>): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** | ||
@@ -56,6 +127,6 @@ * Sets a successful response that will be returned from one `Client#send()` invocation. | ||
* ```js | ||
* clientMock | ||
* .resolvesOnce('first call') | ||
* .resolvesOnce('second call') | ||
* .resolves('default'); | ||
* snsMock | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
@@ -65,3 +136,3 @@ * | ||
*/ | ||
resolvesOnce(response: CommandResponse<TCommandOutput>): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
resolvesOnce(response: CommandResponse<TOutput>): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
/** | ||
@@ -71,2 +142,16 @@ * Sets a failure response that will be returned from any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
@@ -84,6 +169,6 @@ */ | ||
* ```js | ||
* clientMock | ||
* .rejectsOnce('first call') | ||
* .rejectsOnce('second call') | ||
* .rejects('default'); | ||
* snsMock | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
@@ -93,6 +178,29 @@ * | ||
*/ | ||
rejectsOnce(error?: string | Error | AwsError): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
rejectsOnce(error?: string | Error | AwsError): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
/** | ||
* Sets a function that will be called on any `Client#send()` invocation. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
@@ -102,3 +210,3 @@ */ | ||
/** | ||
* Sets a function that will be called on any `Client#send()` invocation. | ||
* Sets a function that will be called once, on any `Client#send()` invocation. | ||
* | ||
@@ -110,6 +218,6 @@ * Can be chained so that successive invocations call different functions. When there are no more | ||
* ```js | ||
* clientMock | ||
* .callsFakeOnce(cmd => 'first call') | ||
* .callsFakeOnce(cmd => 'second call') | ||
* .callsFake(cmd => 'default'); | ||
* snsMock | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
@@ -119,66 +227,2 @@ * | ||
*/ | ||
callsFakeOnce(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): Behavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
} | ||
/** | ||
* Type for {@link AwsStub} class, | ||
* but with the AWS Client class type as an only generic parameter. | ||
* | ||
* @example | ||
* ```ts | ||
* let snsMock: AwsClientStub<SNSClient>; | ||
* snsMock = mockClient(SNSClient); | ||
* ``` | ||
*/ | ||
export type AwsClientStub<TClient> = TClient extends Client<infer TInput, infer TOutput, infer TConfiguration> ? AwsStub<TInput, TOutput, TConfiguration> : never; | ||
/** | ||
* Wrapper on the mocked `Client#send()` method, | ||
* allowing to configure its behavior. | ||
* | ||
* Without any configuration, `Client#send()` invocation returns `undefined`. | ||
* | ||
* To define resulting variable type easily, use {@link AwsClientStub}. | ||
*/ | ||
export declare class AwsStub<TInput extends object, TOutput extends MetadataBearer, TConfiguration> implements Behavior<TInput, TOutput, TOutput, TConfiguration> { | ||
private client; | ||
/** | ||
* Underlying `Client#send()` method Sinon stub. | ||
* | ||
* Install `@types/sinon` for TypeScript typings. | ||
*/ | ||
send: SinonStub<[AwsCommand<TInput, TOutput>], Promise<TOutput>>; | ||
constructor(client: Client<TInput, TOutput, TConfiguration>, send: SinonStub<[AwsCommand<TInput, TOutput>], Promise<TOutput>>); | ||
/** Returns the class name of the underlying mocked client class */ | ||
clientName(): string; | ||
/** | ||
* Resets stub. It will replace the stub with a new one, with clean history and behavior. | ||
*/ | ||
reset(): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** Resets stub's calls history. */ | ||
resetHistory(): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** Replaces stub with original `Client#send()` method. */ | ||
restore(): void; | ||
/** | ||
* Returns recorded calls to the stub. | ||
* Clear history with {@link resetHistory} or {@link reset}. | ||
*/ | ||
calls(): SinonSpyCall<[AwsCommand<TInput, TOutput>], Promise<TOutput>>[]; | ||
/** | ||
* Returns n-th recorded call to the stub. | ||
*/ | ||
call(n: number): SinonSpyCall<[AwsCommand<TInput, TOutput>], Promise<TOutput>>; | ||
/** | ||
* Returns recorded calls of given Command only. | ||
* @param commandType Command type to match | ||
* @param input Command payload to match | ||
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match) | ||
*/ | ||
commandCalls<TCmd extends AwsCommand<any, any>, TCmdInput extends TCmd extends AwsCommand<infer TIn, any> ? TIn : never, TCmdOutput extends TCmd extends AwsCommand<any, infer TOut> ? TOut : never>(commandType: new (input: TCmdInput) => TCmd, input?: Partial<TCmdInput>, strict?: boolean): SinonSpyCall<[TCmd], Promise<TCmdOutput>>[]; | ||
onAnyCommand<TCmdInput extends TInput>(input?: Partial<TCmdInput>, strict?: boolean): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
on<TCmdInput extends TInput, TCmdOutput extends TOutput>(command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>, input?: Partial<TCmdInput>, strict?: boolean): CommandBehavior<TInput, TOutput, TCmdOutput, TConfiguration>; | ||
private createInputMatcher; | ||
resolves(response: CommandResponse<TOutput>): AwsStub<TInput, TOutput, TConfiguration>; | ||
resolvesOnce(response: CommandResponse<TOutput>): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
rejects(error?: string | Error | AwsError): AwsStub<TInput, TOutput, TConfiguration>; | ||
rejectsOnce(error?: string | Error | AwsError): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
callsFake(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): AwsStub<TInput, TOutput, TConfiguration>; | ||
callsFakeOnce(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): CommandBehavior<TInput, TOutput, TOutput, TConfiguration>; | ||
@@ -201,10 +245,134 @@ } | ||
constructor(clientStub: AwsStub<TInput, TOutput, TConfiguration>, send: SinonStub<[AwsCommand<TInput, TOutput>], unknown>); | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
onAnyCommand<TCmdInput extends TInput>(input?: Partial<TCmdInput>, strict?: boolean): Behavior<TInput, TOutput, TOutput, TConfiguration>; | ||
/** | ||
* @deprecated Using this method means that the previously set `.on(Command)` was not followed by resolves/rejects/callsFake call. | ||
* If this is legitimate behavior, please open an issue with your use case. | ||
*/ | ||
on<TCmdInput extends TInput, TCmdOutput extends TOutput>(command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>, input?: Partial<TCmdInput>, strict?: boolean): CommandBehavior<TInput, TOutput, TCmdOutput, TConfiguration>; | ||
/** | ||
* Sets a successful response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolves({MessageId: '111'}); | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolves(response: CommandResponse<TCommandOutput>): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** | ||
* Sets a successful response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `resolvesOnce()` responses to use, invocations will return a response specified by `resolves()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .resolvesOnce({MessageId: '111'}) // first call | ||
* .resolvesOnce({MessageId: '222'}) // second call | ||
* .resolves({MessageId: '333'}); // default | ||
* ``` | ||
* | ||
* @param response Content to be returned | ||
*/ | ||
resolvesOnce(response: CommandResponse<TCommandOutput>): CommandBehavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
/** | ||
* Sets a failure response that will be returned from `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects('mocked rejection'); | ||
*``` | ||
* | ||
* @example | ||
* ```js | ||
* const throttlingError = new Error('mocked rejection'); | ||
* throttlingError.name = 'ThrottlingException'; | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejects(throttlingError); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejects(error?: string | Error | AwsError): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** | ||
* Sets a failure response that will be returned from one `Client#send()` invocation for the current `Command`. | ||
* The response will always be an `Error` instance. | ||
* | ||
* Can be chained so that successive invocations return different responses. When there are no more | ||
* `rejectsOnce()` responses to use, invocations will return a response specified by `rejects()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .rejectsOnce('first mocked rejection') | ||
* .rejectsOnce('second mocked rejection') | ||
* .rejects('default mocked rejection'); | ||
* ``` | ||
* | ||
* @param error Error text, Error instance or Error parameters to be returned | ||
*/ | ||
rejectsOnce(error?: string | Error | AwsError): CommandBehavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
private static normalizeError; | ||
/** | ||
* Sets a function that will be called on `Client#send()` invocation for the current `Command`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(input => { | ||
* if (input.Message === 'My message') { | ||
* return {MessageId: '111'}; | ||
* } else { | ||
* throw new Error('mocked rejection'); | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* Result based on the `Client` configuration: | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFake(async (input, getClient) => { | ||
* const client = getClient(); | ||
* const region = await client.config.region(); | ||
* return {MessageId: region.substring(0, 2)}; | ||
* }); | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFake(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): AwsStub<TInput, TOutput, TConfiguration>; | ||
/** | ||
* Sets a function that will be called once on `Client#send()` invocation for the current `Command`. | ||
* | ||
* Can be chained so that successive invocations call different functions. When there are no more | ||
* `callsFakeOnce()` functions to use, invocations will call a function specified by `callsFake()`. | ||
* | ||
* @example | ||
* ```js | ||
* snsMock | ||
* .on(PublishCommand) | ||
* .callsFakeOnce(cmd => {MessageId: '111'}) // first call | ||
* .callsFakeOnce(cmd => {MessageId: '222'}) // second call | ||
* .callsFake(cmd => {MessageId: '000'}); // default | ||
* ``` | ||
* | ||
* @param fn Function taking Command input and returning result | ||
*/ | ||
callsFakeOnce(fn: (input: any, getClient: () => Client<TInput, TOutput, TConfiguration>) => any): CommandBehavior<TInput, TOutput, TCommandOutput, TConfiguration>; | ||
@@ -211,0 +379,0 @@ private fakeFnWrapper; |
{ | ||
"name": "aws-sdk-client-mock", | ||
"description": "Easy and powerful mocking of AWS SDK v3 Clients", | ||
"version": "4.0.0", | ||
"version": "4.0.1-beta.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -93,3 +93,3 @@ <div align="center"> | ||
|--------------|-----------------------| | ||
| ≥ 3.363.0 | 3.x | | ||
| ≥ 3.363.0 | ≥ 3.x | | ||
| < 3.363.0 | 2.x | | ||
@@ -233,2 +233,14 @@ | ||
```typescript | ||
const throttlingError = new Error('mocked rejection'); | ||
throttlingError.name = 'ThrottlingException'; | ||
snsMock | ||
.rejects(throttlingError); | ||
``` | ||
In `rejects()`, you can pass a string, an `Error` instance, | ||
or an object with properties. | ||
In each case, it will be converted to an `Error` instance. | ||
Specify custom mock function: | ||
@@ -517,3 +529,3 @@ | ||
// Any command was sent to SNS | ||
// at least one command was sent to SNS | ||
expect(snsMock).toHaveReceivedAnyCommand(); | ||
@@ -524,10 +536,21 @@ | ||
// a PublishCommand with Message "My message" was sent to SNS | ||
expect(snsMock).toHaveReceivedCommandWith(PublishCommand, {Message: 'My message'}); | ||
// a PublishCommand with Message "hello world" was sent to SNS | ||
expect(snsMock).toHaveReceivedCommandWith( | ||
PublishCommand, {Message: 'hello world'} | ||
); | ||
// the second command sent to SNS is a PublishCommand with Message "My message" | ||
expect(snsMock).toHaveReceivedNthCommandWith(2, PublishCommand, {Message: 'My message'}); | ||
// a PublishCommand with Message containing "hello" was sent to SNS | ||
expect(snsMock).toHaveReceivedCommandWith( | ||
PublishCommand, {Message: expect.stringContaining('hello')} | ||
); | ||
// the second PublishCommand sent to SNS has Message "My message" | ||
expect(snsMock).toHaveReceivedNthSpecificCommandWith(2, PublishCommand, {Message: 'My message'}); | ||
// the second command sent to SNS was a PublishCommand with Message "hello world" | ||
expect(snsMock).toHaveReceivedNthCommandWith( | ||
2, PublishCommand, {Message: 'hello world'} | ||
); | ||
// the second PublishCommand sent to SNS had Message "hello world" | ||
expect(snsMock).toHaveReceivedNthSpecificCommandWith( | ||
2, PublishCommand, {Message: 'hello world'} | ||
); | ||
``` | ||
@@ -534,0 +557,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
90861
1354
760
1