Skip to content

Commit 7acde27

Browse files
committed
docs: add code examples for Jest matchers including asymmetric matchers
Closes #217
1 parent 776836a commit 7acde27

File tree

3 files changed

+99
-56
lines changed

3 files changed

+99
-56
lines changed

README.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -524,20 +524,31 @@ import 'aws-sdk-client-mock-jest';
524524
// a PublishCommand was sent to SNS
525525
expect(snsMock).toHaveReceivedCommand(PublishCommand);
526526

527-
// Any command was sent to SNS
527+
// at least one command was sent to SNS
528528
expect(snsMock).toHaveReceivedAnyCommand();
529529

530530
// two PublishCommands were sent to SNS
531531
expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
532532

533-
// a PublishCommand with Message "My message" was sent to SNS
534-
expect(snsMock).toHaveReceivedCommandWith(PublishCommand, {Message: 'My message'});
535-
536-
// the second command sent to SNS is a PublishCommand with Message "My message"
537-
expect(snsMock).toHaveReceivedNthCommandWith(2, PublishCommand, {Message: 'My message'});
538-
539-
// the second PublishCommand sent to SNS has Message "My message"
540-
expect(snsMock).toHaveReceivedNthSpecificCommandWith(2, PublishCommand, {Message: 'My message'});
533+
// a PublishCommand with Message "hello world" was sent to SNS
534+
expect(snsMock).toHaveReceivedCommandWith(
535+
PublishCommand, {Message: 'hello world'}
536+
);
537+
538+
// a PublishCommand with Message containing "hello" was sent to SNS
539+
expect(snsMock).toHaveReceivedCommandWith(
540+
PublishCommand, {Message: expect.stringContaining('hello')}
541+
);
542+
543+
// the second command sent to SNS was a PublishCommand with Message "hello world"
544+
expect(snsMock).toHaveReceivedNthCommandWith(
545+
2, PublishCommand, {Message: 'hello world'}
546+
);
547+
548+
// the second PublishCommand sent to SNS had Message "hello world"
549+
expect(snsMock).toHaveReceivedNthSpecificCommandWith(
550+
2, PublishCommand, {Message: 'hello world'}
551+
);
541552
```
542553

543554
Shorter aliases exist, like `toReceiveCommandTimes()`.

packages/aws-sdk-client-mock-jest/src/jestMatchers.ts

+78-47
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import {expect} from 'expect';
88

99
interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
1010
/**
11-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} exact number of {@link times}
11+
* Asserts the {@link AwsStub Client Mock} received given `Command` exact number of times.
1212
*
13-
* @param command aws-sdk command constructor
14-
* @param times
13+
* @example
14+
* ```js
15+
* expect(snsMock).toHaveReceivedCommandTimes(PublishCommand, 2);
16+
* ```
17+
*
18+
* @param command AWS SDK Command type
19+
* @param times Number of expected calls
1520
*/
1621
toHaveReceivedCommandTimes<TCmdInput extends object,
1722
TCmdOutput extends MetadataBearer>(
@@ -20,20 +25,46 @@ interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
2025
): R;
2126

2227
/**
23-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} at least one time
28+
* Asserts the {@link AwsStub Client Mock} received given `Command` at least one time.
29+
*
30+
* @example
31+
* ```js
32+
* expect(snsMock).toHaveReceivedCommandTimes(PublishCommand);
33+
* ```
2434
*
25-
* @param command aws-sdk command constructor
35+
* @param command AWS SDK Command type
2636
*/
2737
toHaveReceivedCommand<TCmdInput extends object,
2838
TCmdOutput extends MetadataBearer>(
2939
command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>,
3040
): R;
3141

3242
/**
33-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} at least one time with matching {@link input}
43+
* Asserts the {@link AwsStub Client Mock} received given `Command` at least one time with matching input.
44+
*
45+
* @example
46+
* ```js
47+
* expect(snsMock).toHaveReceivedCommandWith(
48+
* PublishCommand,
49+
* {
50+
* Message: 'hello world',
51+
* },
52+
* );
53+
* ```
3454
*
35-
* @param command aws-sdk command constructor
36-
* @param input
55+
* @example
56+
* With asymmetric matcher:
57+
* ```js
58+
* expect(snsMock).toHaveReceivedCommandWith(
59+
* PublishCommand,
60+
* {
61+
* Message: expect.stringContaining('hello'),
62+
* },
63+
* );
64+
* ```
65+
*
66+
* @param command AWS SDK Command type
67+
* @param input Partial input to match
3768
*/
3869
toHaveReceivedCommandWith<TCmdInput extends object,
3970
TCmdOutput extends MetadataBearer>(
@@ -42,12 +73,23 @@ interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
4273
): R;
4374

4475
/**
45-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} as defined {@link call} number
46-
* with matching {@link input}
76+
* Asserts the nth call to the {@link AwsStub Client Mock} was a given `Command` with matching input.
77+
*
78+
* @example
79+
* The second call to `SNSClient` was a `PublishCommand` with Message 'hello world':
80+
* ```js
81+
* expect(snsMock).toHaveReceivedNthCommandWith(
82+
* 2,
83+
* PublishCommand,
84+
* {
85+
* Message: 'hello world',
86+
* },
87+
* );
88+
* ```
4789
*
48-
* @param call call number to assert
49-
* @param command aws-sdk command constructor
50-
* @param input
90+
* @param call Call number
91+
* @param command AWS SDK Command type
92+
* @param input Partial input to match
5193
*/
5294
toHaveReceivedNthCommandWith<TCmdInput extends object,
5395
TCmdOutput extends MetadataBearer>(
@@ -57,12 +99,23 @@ interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
5799
): R;
58100

59101
/**
60-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} as defined specific {@link call}
61-
* number with matchin {@link input}
102+
* Asserts the nth `Command` of given type sent to the {@link AwsStub Client Mock} had matching input.
103+
*
104+
* @example
105+
* The second `PublishCommand` sent to `SNSClient` had Message 'hello world':
106+
* ```js
107+
* expect(snsMock).toHaveReceivedNthSpecificCommandWith(
108+
* 2,
109+
* PublishCommand,
110+
* {
111+
* Message: 'hello world',
112+
* },
113+
* );
114+
* ```
62115
*
63-
* @param call call number to assert
64-
* @param command aws-sdk command constructor
65-
* @param input
116+
* @param call Call number
117+
* @param command AWS SDK Command type
118+
* @param input Partial input to match
66119
*/
67120
toHaveReceivedNthSpecificCommandWith<TCmdInput extends object, TCmdOutput extends MetadataBearer>(
68121
call: number,
@@ -71,18 +124,14 @@ interface AwsSdkJestMockBaseMatchers<R> extends Record<string, Function> {
71124
): R;
72125

73126
/**
74-
* Asserts {@link AwsStub Aws Client Mock} received any command
127+
* Asserts {@link AwsStub Client Mock} was called at least once with any `Command`.
75128
*/
76129
toHaveReceivedAnyCommand(): R;
77130
}
78131

79132
interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
80133
/**
81-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} exact number of {@link times}
82-
*
83-
* @alias {@link AwsSdkJestMockBaseMatchers.toHaveReceivedCommandTimes}
84-
* @param command aws-sdk command constructor
85-
* @param times
134+
* @see toHaveReceivedCommandTimes
86135
*/
87136
toReceiveCommandTimes<TCmdInput extends object,
88137
TCmdOutput extends MetadataBearer>(
@@ -91,22 +140,15 @@ interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
91140
): R;
92141

93142
/**
94-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} at least one time
95-
*
96-
* @alias {@link AwsSdkJestMockBaseMatchers.toHaveReceivedCommand}
97-
* @param command aws-sdk command constructor
143+
* @see toHaveReceivedCommand
98144
*/
99145
toReceiveCommand<TCmdInput extends object,
100146
TCmdOutput extends MetadataBearer>(
101147
command: new (input: TCmdInput) => AwsCommand<TCmdInput, TCmdOutput>,
102148
): R;
103149

104150
/**
105-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} at least one time with matching {@link input}
106-
*
107-
* @alias {@link AwsSdkJestMockBaseMatchers.toHaveReceivedCommandWith}
108-
* @param command aws-sdk command constructor
109-
* @param input
151+
* @see toHaveReceivedCommandWith
110152
*/
111153
toReceiveCommandWith<TCmdInput extends object,
112154
TCmdOutput extends MetadataBearer>(
@@ -115,13 +157,7 @@ interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
115157
): R;
116158

117159
/**
118-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} as defined {@link call} number
119-
* with matching {@link input}
120-
*
121-
* @alias {@link AwsSdkJestMockBaseMatchers.toHaveReceivedNthCommandWith}
122-
* @param call call number to assert
123-
* @param command aws-sdk command constructor
124-
* @param input
160+
* @see toHaveReceivedNthCommandWith
125161
*/
126162
toReceiveNthCommandWith<TCmdInput extends object,
127163
TCmdOutput extends MetadataBearer>(
@@ -131,12 +167,7 @@ interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
131167
): R;
132168

133169
/**
134-
* Asserts {@link AwsStub Aws Client Mock} received a {@link command} as defined specific {@link call}
135-
* number with matchin {@link input}
136-
*
137-
* @param call call number to assert
138-
* @param command aws-sdk command constructor
139-
* @param input
170+
* @see toHaveReceivedNthSpecificCommandWith
140171
*/
141172
toReceiveNthSpecificCommandWith<TCmdInput extends object, TCmdOutput extends MetadataBearer>(
142173
call: number,
@@ -145,7 +176,7 @@ interface AwsSdkJestMockAliasMatchers<R> extends Record<string, Function> {
145176
): R;
146177

147178
/**
148-
* Asserts {@link AwsStub Aws Client Mock} received any command
179+
* @see toHaveReceivedAnyCommand
149180
*/
150181
toReceiveAnyCommand(): R;
151182
}

packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Calls:
147147
const sns = new SNSClient({});
148148
await sns.send(publishCmd1);
149149

150+
150151
expect(() => expect(snsMock).toHaveReceivedCommandWith(PublishCommand, {
151152
Message: expect.stringMatching(/message/), // eslint-disable-line @typescript-eslint/no-unsafe-assignment
152153
})).not.toThrow();

0 commit comments

Comments
 (0)