aws-sdk-vitest-mock 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -92
- package/index.cjs +18 -1
- package/index.d.ts +46 -1
- package/index.js +328 -221
- package/lib/matchers.d.ts +141 -0
- package/lib/mock-client.d.ts +436 -19
- package/lib/utils/paginator-helpers.d.ts +85 -0
- package/{matchers-C6AtmwWz.js → matchers-ClGOsQx8.js} +99 -0
- package/{matchers-Rq18z2C7.cjs → matchers-Dkkl4vtx.cjs} +1 -1
- package/package.json +11 -2
- package/vitest-setup.cjs +1 -1
- package/vitest-setup.js +1 -1
package/lib/matchers.d.ts
CHANGED
|
@@ -4,22 +4,163 @@ interface MatcherResult {
|
|
|
4
4
|
pass: boolean;
|
|
5
5
|
message: () => string;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Custom Vitest matchers for asserting AWS SDK command calls.
|
|
9
|
+
*
|
|
10
|
+
* @category Matchers
|
|
11
|
+
*
|
|
12
|
+
* @example Setup matchers in your test setup file
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { expect } from 'vitest';
|
|
15
|
+
* import { matchers } from 'aws-sdk-vitest-mock';
|
|
16
|
+
*
|
|
17
|
+
* expect.extend(matchers);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
7
20
|
export declare const matchers: {
|
|
21
|
+
/**
|
|
22
|
+
* Assert that a command was received at least once.
|
|
23
|
+
*
|
|
24
|
+
* @param received - The AWS client stub
|
|
25
|
+
* @param command - The command constructor to check for
|
|
26
|
+
* @returns Matcher result
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const s3Mock = mockClient(S3Client);
|
|
31
|
+
* const client = new S3Client({});
|
|
32
|
+
*
|
|
33
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
|
|
34
|
+
*
|
|
35
|
+
* expect(s3Mock).toHaveReceivedCommand(GetObjectCommand);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
8
38
|
toHaveReceivedCommand<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, command: CommandConstructor<TInput, TOutput>): MatcherResult;
|
|
39
|
+
/**
|
|
40
|
+
* Assert that a command was received exactly N times.
|
|
41
|
+
*
|
|
42
|
+
* @param received - The AWS client stub
|
|
43
|
+
* @param command - The command constructor to check for
|
|
44
|
+
* @param times - The exact number of times the command should have been received
|
|
45
|
+
* @returns Matcher result
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const s3Mock = mockClient(S3Client);
|
|
50
|
+
* const client = new S3Client({});
|
|
51
|
+
*
|
|
52
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file1.txt' }));
|
|
53
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file2.txt' }));
|
|
54
|
+
*
|
|
55
|
+
* expect(s3Mock).toHaveReceivedCommandTimes(GetObjectCommand, 2);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
9
58
|
toHaveReceivedCommandTimes<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, command: CommandConstructor<TInput, TOutput>, times: number): MatcherResult;
|
|
59
|
+
/**
|
|
60
|
+
* Assert that a command was received with specific input parameters.
|
|
61
|
+
*
|
|
62
|
+
* @param received - The AWS client stub
|
|
63
|
+
* @param command - The command constructor to check for
|
|
64
|
+
* @param input - The expected input parameters
|
|
65
|
+
* @returns Matcher result
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const s3Mock = mockClient(S3Client);
|
|
70
|
+
* const client = new S3Client({});
|
|
71
|
+
*
|
|
72
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
|
|
73
|
+
*
|
|
74
|
+
* expect(s3Mock).toHaveReceivedCommandWith(GetObjectCommand, {
|
|
75
|
+
* Bucket: 'test',
|
|
76
|
+
* Key: 'file.txt'
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
10
80
|
toHaveReceivedCommandWith<TInput extends object, TOutput extends MetadataBearer>(this: {
|
|
11
81
|
equals: (a: unknown, b: unknown) => boolean;
|
|
12
82
|
}, received: AwsClientStub, command: CommandConstructor<TInput, TOutput>, input: TInput): MatcherResult;
|
|
83
|
+
/**
|
|
84
|
+
* Assert that the Nth command call was a specific command with specific input.
|
|
85
|
+
*
|
|
86
|
+
* @param received - The AWS client stub
|
|
87
|
+
* @param n - The call number (1-indexed)
|
|
88
|
+
* @param command - The command constructor to check for
|
|
89
|
+
* @param input - The expected input parameters
|
|
90
|
+
* @returns Matcher result
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const s3Mock = mockClient(S3Client);
|
|
95
|
+
* const client = new S3Client({});
|
|
96
|
+
*
|
|
97
|
+
* await client.send(new PutObjectCommand({ Bucket: 'test', Key: 'file1.txt' }));
|
|
98
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file2.txt' }));
|
|
99
|
+
*
|
|
100
|
+
* expect(s3Mock).toHaveReceivedNthCommandWith(2, GetObjectCommand, {
|
|
101
|
+
* Bucket: 'test',
|
|
102
|
+
* Key: 'file2.txt'
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
13
106
|
toHaveReceivedNthCommandWith<TInput extends object, TOutput extends MetadataBearer>(this: {
|
|
14
107
|
equals: (a: unknown, b: unknown) => boolean;
|
|
15
108
|
}, received: AwsClientStub, n: number, command: CommandConstructor<TInput, TOutput>, input: TInput): MatcherResult;
|
|
109
|
+
/**
|
|
110
|
+
* Assert that no commands other than the expected ones were received.
|
|
111
|
+
*
|
|
112
|
+
* @param received - The AWS client stub
|
|
113
|
+
* @param expectedCommands - Array of command constructors that are allowed
|
|
114
|
+
* @returns Matcher result
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const s3Mock = mockClient(S3Client);
|
|
119
|
+
* const client = new S3Client({});
|
|
120
|
+
*
|
|
121
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
|
|
122
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'other.txt' }));
|
|
123
|
+
*
|
|
124
|
+
* expect(s3Mock).toHaveReceivedNoOtherCommands([GetObjectCommand]);
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
16
127
|
toHaveReceivedNoOtherCommands<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, expectedCommands?: CommandConstructor<TInput, TOutput>[]): MatcherResult;
|
|
17
128
|
};
|
|
129
|
+
/**
|
|
130
|
+
* TypeScript interface for AWS SDK Vitest matchers.
|
|
131
|
+
* This interface is used to extend Vitest's assertion types.
|
|
132
|
+
*
|
|
133
|
+
* @category Matchers
|
|
134
|
+
*/
|
|
18
135
|
export interface AwsSdkMatchers<R = unknown> {
|
|
136
|
+
/**
|
|
137
|
+
* Assert that a command was received at least once.
|
|
138
|
+
* @param command - The command constructor to check for
|
|
139
|
+
*/
|
|
19
140
|
toHaveReceivedCommand<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>): R;
|
|
141
|
+
/**
|
|
142
|
+
* Assert that a command was received exactly N times.
|
|
143
|
+
* @param command - The command constructor to check for
|
|
144
|
+
* @param times - The exact number of times
|
|
145
|
+
*/
|
|
20
146
|
toHaveReceivedCommandTimes<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>, times: number): R;
|
|
147
|
+
/**
|
|
148
|
+
* Assert that a command was received with specific input.
|
|
149
|
+
* @param command - The command constructor to check for
|
|
150
|
+
* @param input - The expected input parameters
|
|
151
|
+
*/
|
|
21
152
|
toHaveReceivedCommandWith<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>, input: TInput): R;
|
|
153
|
+
/**
|
|
154
|
+
* Assert that the Nth command call matches expectations.
|
|
155
|
+
* @param n - The call number (1-indexed)
|
|
156
|
+
* @param command - The command constructor to check for
|
|
157
|
+
* @param input - The expected input parameters
|
|
158
|
+
*/
|
|
22
159
|
toHaveReceivedNthCommandWith<TInput extends object, TOutput extends MetadataBearer>(n: number, command: CommandConstructor<TInput, TOutput>, input: TInput): R;
|
|
160
|
+
/**
|
|
161
|
+
* Assert that only expected commands were received.
|
|
162
|
+
* @param expectedCommands - Array of allowed command constructors
|
|
163
|
+
*/
|
|
23
164
|
toHaveReceivedNoOtherCommands<TInput extends object, TOutput extends MetadataBearer>(expectedCommands?: CommandConstructor<TInput, TOutput>[]): R;
|
|
24
165
|
}
|
|
25
166
|
export type { MatcherResult };
|
package/lib/mock-client.d.ts
CHANGED
|
@@ -23,57 +23,474 @@ type CommandHandler<TInput extends object = object, TOutput extends MetadataBear
|
|
|
23
23
|
interface MockOptions {
|
|
24
24
|
strict?: boolean;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Client stub for configuring and managing mock behaviors for an AWS SDK client.
|
|
28
|
+
*
|
|
29
|
+
* @category Core Functions
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const s3Mock = mockClient(S3Client);
|
|
34
|
+
*
|
|
35
|
+
* // Configure mock behavior
|
|
36
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'data' });
|
|
37
|
+
*
|
|
38
|
+
* // Mock works as configured
|
|
39
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
|
|
40
|
+
* expect(s3Mock.calls()).toHaveLength(1);
|
|
41
|
+
*
|
|
42
|
+
* // Reset clears call history but keeps mock configuration
|
|
43
|
+
* s3Mock.reset();
|
|
44
|
+
* expect(s3Mock.calls()).toHaveLength(0);
|
|
45
|
+
*
|
|
46
|
+
* // Mock still works after reset
|
|
47
|
+
* await client.send(new GetObjectCommand({ Bucket: 'test', Key: 'file.txt' }));
|
|
48
|
+
* expect(s3Mock.calls()).toHaveLength(1);
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
26
51
|
export interface AwsClientStub<TClient extends AnyClient = AnyClient> {
|
|
52
|
+
/**
|
|
53
|
+
* The client instance being mocked (undefined for class-level mocks).
|
|
54
|
+
* @readonly
|
|
55
|
+
*/
|
|
27
56
|
readonly client: TClient | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Configure mock behavior for a specific command.
|
|
59
|
+
*
|
|
60
|
+
* @param command - The AWS SDK command constructor to mock
|
|
61
|
+
* @param request - Optional partial input to match against (uses partial matching by default)
|
|
62
|
+
* @param options - Optional configuration for strict matching
|
|
63
|
+
* @returns A command stub for configuring mock responses
|
|
64
|
+
*
|
|
65
|
+
* @example Match any input
|
|
66
|
+
* ```typescript
|
|
67
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'data' });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @example Match specific input (partial)
|
|
71
|
+
* ```typescript
|
|
72
|
+
* s3Mock.on(GetObjectCommand, { Bucket: 'my-bucket' }).resolves({ Body: 'bucket data' });
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example Match exact input (strict)
|
|
76
|
+
* ```typescript
|
|
77
|
+
* s3Mock.on(GetObjectCommand, { Bucket: 'my-bucket', Key: 'file.txt' }, { strict: true })
|
|
78
|
+
* .resolves({ Body: 'exact match' });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
28
81
|
on: <TCtor extends AwsCommandConstructor>(command: TCtor, request?: Partial<CommandInputType<TCtor>>, options?: MockOptions) => AwsCommandStub<CommandInputType<TCtor>, CommandOutputType<TCtor>, TClient>;
|
|
82
|
+
/**
|
|
83
|
+
* Clear mock call history while preserving configured behaviors.
|
|
84
|
+
* Mock configurations remain active after reset, only the call history is cleared.
|
|
85
|
+
* Use this between tests when you want to reuse the same mock setup.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* afterEach(() => {
|
|
90
|
+
* s3Mock.reset();
|
|
91
|
+
* });
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
29
94
|
reset: () => void;
|
|
95
|
+
/**
|
|
96
|
+
* Restore the original client behavior and clear all mocks.
|
|
97
|
+
* After calling restore, the client will no longer be mocked.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* afterAll(() => {
|
|
102
|
+
* s3Mock.restore();
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
30
106
|
restore: () => void;
|
|
107
|
+
/**
|
|
108
|
+
* Get an array of all commands that were sent to the client.
|
|
109
|
+
*
|
|
110
|
+
* @returns Array of AWS SDK commands
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const calls = s3Mock.calls();
|
|
115
|
+
* console.log(calls.length); // Number of commands sent
|
|
116
|
+
* console.log(calls[0].input); // Input of first command
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
31
119
|
calls: () => AwsSdkCommand[];
|
|
32
120
|
/** @internal - For use by matchers only */
|
|
33
121
|
__rawCalls: () => ReturnType<Mock["mock"]["calls"]["slice"]>;
|
|
122
|
+
/**
|
|
123
|
+
* Enable debug logging to see detailed information about mock configuration and interactions.
|
|
124
|
+
* Logs both mock setup (when `.on()`, `.resolves()`, etc. are called) and command execution details.
|
|
125
|
+
* Useful for troubleshooting mock configurations and why commands aren't matching expected mocks.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* s3Mock.enableDebug();
|
|
130
|
+
* // Logs will appear for:
|
|
131
|
+
* // - Mock configuration (when .on(), .resolves(), etc. are called)
|
|
132
|
+
* // - Command execution (incoming commands and inputs)
|
|
133
|
+
* // - Mock matching results and reasons for failures
|
|
134
|
+
* // - Lifecycle events (reset, restore)
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
34
137
|
enableDebug: () => void;
|
|
138
|
+
/**
|
|
139
|
+
* Disable debug logging for mock configuration and interactions.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* s3Mock.disableDebug();
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
35
146
|
disableDebug: () => void;
|
|
36
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Command stub for configuring mock behaviors for a specific AWS SDK command.
|
|
150
|
+
* Provides a fluent API for setting up various mock responses and behaviors.
|
|
151
|
+
*
|
|
152
|
+
* @category Command Stub
|
|
153
|
+
*
|
|
154
|
+
* @example Basic usage
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const s3Mock = mockClient(S3Client);
|
|
157
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'data' });
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @example Chaining multiple behaviors
|
|
161
|
+
* ```typescript
|
|
162
|
+
* s3Mock.on(PutObjectCommand)
|
|
163
|
+
* .resolvesOnce({ ETag: '123' })
|
|
164
|
+
* .resolvesOnce({ ETag: '456' })
|
|
165
|
+
* .resolves({ ETag: 'default' });
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
37
168
|
export interface AwsCommandStub<TInput extends object, TOutput extends MetadataBearer, TClient extends AnyClient = AnyClient> {
|
|
38
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* Set a permanent mock response that will be used after all one-time handlers are consumed.
|
|
171
|
+
*
|
|
172
|
+
* @param output - Partial output object to return when the command is called
|
|
173
|
+
* @returns The command stub for chaining
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'file contents' });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
39
180
|
resolves: (output: Partial<TOutput>) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
40
|
-
/**
|
|
181
|
+
/**
|
|
182
|
+
* Set a permanent mock rejection that will be used after all one-time handlers are consumed.
|
|
183
|
+
*
|
|
184
|
+
* @param error - Error object or error message string
|
|
185
|
+
* @returns The command stub for chaining
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* s3Mock.on(GetObjectCommand).rejects(new Error('Access denied'));
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
41
192
|
rejects: (error: Error | string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
42
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* Set a permanent custom handler function that will be used after all one-time handlers are consumed.
|
|
195
|
+
*
|
|
196
|
+
* @param fn - Handler function that receives input and client instance
|
|
197
|
+
* @returns The command stub for chaining
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* s3Mock.on(GetObjectCommand).callsFake(async (input) => {
|
|
202
|
+
* return { Body: `Contents of ${input.Key}` };
|
|
203
|
+
* });
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
43
206
|
callsFake: (fn: CommandHandler<TInput, TOutput, TClient>) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
44
|
-
/**
|
|
207
|
+
/**
|
|
208
|
+
* Add a one-time mock response that will be consumed in order.
|
|
209
|
+
*
|
|
210
|
+
* @param output - Partial output object to return
|
|
211
|
+
* @returns The command stub for chaining
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* s3Mock.on(GetObjectCommand)
|
|
216
|
+
* .resolvesOnce({ Body: 'first call' })
|
|
217
|
+
* .resolvesOnce({ Body: 'second call' });
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
45
220
|
resolvesOnce: (output: Partial<TOutput>) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
46
|
-
/**
|
|
221
|
+
/**
|
|
222
|
+
* Add a one-time mock rejection that will be consumed in order.
|
|
223
|
+
*
|
|
224
|
+
* @param error - Error object or error message string
|
|
225
|
+
* @returns The command stub for chaining
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* s3Mock.on(GetObjectCommand)
|
|
230
|
+
* .rejectsOnce('First call fails')
|
|
231
|
+
* .resolves({ Body: 'second call succeeds' });
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
47
234
|
rejectsOnce: (error: Error | string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
48
|
-
/**
|
|
235
|
+
/**
|
|
236
|
+
* Add a one-time custom handler that will be consumed in order.
|
|
237
|
+
*
|
|
238
|
+
* @param fn - Handler function that receives input and client instance
|
|
239
|
+
* @returns The command stub for chaining
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* s3Mock.on(GetObjectCommand)
|
|
244
|
+
* .callsFakeOnce(async (input) => ({ Body: 'once' }))
|
|
245
|
+
* .resolves({ Body: 'permanent' });
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
49
248
|
callsFakeOnce: (fn: CommandHandler<TInput, TOutput, TClient>) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
50
|
-
/**
|
|
249
|
+
/**
|
|
250
|
+
* Set a permanent stream response for S3-like operations.
|
|
251
|
+
*
|
|
252
|
+
* @param data - String, Buffer, or Readable stream
|
|
253
|
+
* @returns The command stub for chaining
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* s3Mock.on(GetObjectCommand).resolvesStream('file contents');
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
51
260
|
resolvesStream: (data: StreamInput) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
52
|
-
/**
|
|
261
|
+
/**
|
|
262
|
+
* Set a one-time stream response for S3-like operations.
|
|
263
|
+
*
|
|
264
|
+
* @param data - String, Buffer, or Readable stream
|
|
265
|
+
* @returns The command stub for chaining
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* s3Mock.on(GetObjectCommand)
|
|
270
|
+
* .resolvesStreamOnce('first stream')
|
|
271
|
+
* .resolvesStream('default stream');
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
53
274
|
resolvesStreamOnce: (data: StreamInput) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
54
|
-
/**
|
|
275
|
+
/**
|
|
276
|
+
* Set a permanent mock response with a delay in milliseconds.
|
|
277
|
+
*
|
|
278
|
+
* @param output - Partial output object to return
|
|
279
|
+
* @param delayMs - Delay in milliseconds before resolving
|
|
280
|
+
* @returns The command stub for chaining
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* s3Mock.on(GetObjectCommand).resolvesWithDelay({ Body: 'data' }, 1000);
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
55
287
|
resolvesWithDelay: (output: Partial<TOutput>, delayMs: number) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
56
|
-
/**
|
|
288
|
+
/**
|
|
289
|
+
* Set a permanent mock rejection with a delay in milliseconds.
|
|
290
|
+
*
|
|
291
|
+
* @param error - Error object or error message string
|
|
292
|
+
* @param delayMs - Delay in milliseconds before rejecting
|
|
293
|
+
* @returns The command stub for chaining
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* s3Mock.on(GetObjectCommand).rejectsWithDelay('Timeout', 5000);
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
57
300
|
rejectsWithDelay: (error: Error | string, delayMs: number) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
58
|
-
/**
|
|
301
|
+
/**
|
|
302
|
+
* Reject with an S3 NoSuchKey error.
|
|
303
|
+
*
|
|
304
|
+
* @param key - Optional key name for error message
|
|
305
|
+
* @returns The command stub for chaining
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```typescript
|
|
309
|
+
* s3Mock.on(GetObjectCommand).rejectsWithNoSuchKey('missing-file.txt');
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
59
312
|
rejectsWithNoSuchKey: (key?: string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
60
|
-
/**
|
|
313
|
+
/**
|
|
314
|
+
* Reject with an S3 NoSuchBucket error.
|
|
315
|
+
*
|
|
316
|
+
* @param bucket - Optional bucket name for error message
|
|
317
|
+
* @returns The command stub for chaining
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```typescript
|
|
321
|
+
* s3Mock.on(GetObjectCommand).rejectsWithNoSuchBucket('my-bucket');
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
61
324
|
rejectsWithNoSuchBucket: (bucket?: string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
62
|
-
/**
|
|
325
|
+
/**
|
|
326
|
+
* Reject with an AccessDenied error.
|
|
327
|
+
*
|
|
328
|
+
* @param resource - Optional resource name for error message
|
|
329
|
+
* @returns The command stub for chaining
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* s3Mock.on(GetObjectCommand).rejectsWithAccessDenied('private-file.txt');
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
63
336
|
rejectsWithAccessDenied: (resource?: string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
64
|
-
/**
|
|
337
|
+
/**
|
|
338
|
+
* Reject with a DynamoDB ResourceNotFound error.
|
|
339
|
+
*
|
|
340
|
+
* @param resource - Optional resource name for error message
|
|
341
|
+
* @returns The command stub for chaining
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```typescript
|
|
345
|
+
* dynamoMock.on(GetItemCommand).rejectsWithResourceNotFound('MyTable');
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
65
348
|
rejectsWithResourceNotFound: (resource?: string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
66
|
-
/**
|
|
349
|
+
/**
|
|
350
|
+
* Reject with a DynamoDB ConditionalCheckFailed error.
|
|
351
|
+
*
|
|
352
|
+
* @returns The command stub for chaining
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```typescript
|
|
356
|
+
* dynamoMock.on(PutItemCommand).rejectsWithConditionalCheckFailed();
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
67
359
|
rejectsWithConditionalCheckFailed: () => AwsCommandStub<TInput, TOutput, TClient>;
|
|
68
|
-
/**
|
|
360
|
+
/**
|
|
361
|
+
* Reject with a Throttling error.
|
|
362
|
+
*
|
|
363
|
+
* @returns The command stub for chaining
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```typescript
|
|
367
|
+
* s3Mock.on(GetObjectCommand).rejectsWithThrottling();
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
69
370
|
rejectsWithThrottling: () => AwsCommandStub<TInput, TOutput, TClient>;
|
|
70
|
-
/**
|
|
371
|
+
/**
|
|
372
|
+
* Reject with an InternalServerError.
|
|
373
|
+
*
|
|
374
|
+
* @returns The command stub for chaining
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* s3Mock.on(GetObjectCommand).rejectsWithInternalServerError();
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
71
381
|
rejectsWithInternalServerError: () => AwsCommandStub<TInput, TOutput, TClient>;
|
|
72
|
-
/**
|
|
382
|
+
/**
|
|
383
|
+
* Set paginated responses for AWS pagination patterns.
|
|
384
|
+
*
|
|
385
|
+
* Tokens are automatically set to the last item of each page, which works for both
|
|
386
|
+
* DynamoDB (object tokens) and S3 (object tokens).
|
|
387
|
+
*
|
|
388
|
+
* @param items - Array of items to paginate (use marshalled items for DynamoDB)
|
|
389
|
+
* @param options - Pagination configuration options
|
|
390
|
+
* @returns The command stub for chaining
|
|
391
|
+
*
|
|
392
|
+
* @example DynamoDB pagination with marshalled data
|
|
393
|
+
* ```typescript
|
|
394
|
+
* import { marshall } from '@aws-sdk/util-dynamodb';
|
|
395
|
+
*
|
|
396
|
+
* const users = [
|
|
397
|
+
* { id: "user-1", name: "Alice" },
|
|
398
|
+
* { id: "user-2", name: "Bob" }
|
|
399
|
+
* ];
|
|
400
|
+
* const marshalledUsers = users.map(u => marshall(u));
|
|
401
|
+
*
|
|
402
|
+
* dynamoMock.on(ScanCommand).resolvesPaginated(marshalledUsers, {
|
|
403
|
+
* pageSize: 1,
|
|
404
|
+
* tokenKey: "LastEvaluatedKey",
|
|
405
|
+
* inputTokenKey: "ExclusiveStartKey"
|
|
406
|
+
* });
|
|
407
|
+
*
|
|
408
|
+
* // LastEvaluatedKey will be the marshalled last item (object, not string)
|
|
409
|
+
* const result = await client.send(new ScanCommand({ TableName: "Users" }));
|
|
410
|
+
* // result.LastEvaluatedKey = { id: { S: "user-1" }, name: { S: "Alice" } }
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @example S3 pagination
|
|
414
|
+
* ```typescript
|
|
415
|
+
* const objects = [
|
|
416
|
+
* { Key: 'file1.txt', Size: 100 },
|
|
417
|
+
* { Key: 'file2.txt', Size: 200 }
|
|
418
|
+
* ];
|
|
419
|
+
*
|
|
420
|
+
* s3Mock.on(ListObjectsV2Command).resolvesPaginated(objects, {
|
|
421
|
+
* pageSize: 1,
|
|
422
|
+
* itemsKey: "Contents",
|
|
423
|
+
* tokenKey: "NextContinuationToken",
|
|
424
|
+
* inputTokenKey: "ContinuationToken"
|
|
425
|
+
* });
|
|
426
|
+
*
|
|
427
|
+
* // NextContinuationToken will be the last object from the page
|
|
428
|
+
* const result = await client.send(new ListObjectsV2Command({ Bucket: "bucket" }));
|
|
429
|
+
* // result.NextContinuationToken = { Key: 'file1.txt', Size: 100 }
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
73
432
|
resolvesPaginated: <T = unknown>(items: T[], options?: PaginatorOptions) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
74
|
-
/**
|
|
433
|
+
/**
|
|
434
|
+
* Load response from a file. JSON files are parsed, others returned as strings.
|
|
435
|
+
*
|
|
436
|
+
* @param filePath - Path to the file to load
|
|
437
|
+
* @returns The command stub for chaining
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* s3Mock.on(GetObjectCommand).resolvesFromFile('./fixtures/response.json');
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
75
444
|
resolvesFromFile: (filePath: string) => AwsCommandStub<TInput, TOutput, TClient>;
|
|
76
445
|
}
|
|
446
|
+
/**
|
|
447
|
+
* Create a mock for an AWS SDK client class.
|
|
448
|
+
*
|
|
449
|
+
* Use this function when you want to mock all instances of a client class.
|
|
450
|
+
*
|
|
451
|
+
* @category Core Functions
|
|
452
|
+
*
|
|
453
|
+
* @param clientConstructor - The AWS SDK client class to mock
|
|
454
|
+
* @returns A client stub for configuring mock behaviors
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* ```typescript
|
|
458
|
+
* import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
|
|
459
|
+
* import { mockClient } from 'aws-sdk-vitest-mock';
|
|
460
|
+
*
|
|
461
|
+
* const s3Mock = mockClient(S3Client);
|
|
462
|
+
*
|
|
463
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'file contents' });
|
|
464
|
+
*
|
|
465
|
+
* const client = new S3Client({});
|
|
466
|
+
* const result = await client.send(new GetObjectCommand({ Bucket: 'my-bucket', Key: 'file.txt' }));
|
|
467
|
+
* // result.Body === 'file contents'
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
77
470
|
export declare const mockClient: <TClient extends AnyClient>(clientConstructor: ClientConstructor<TClient>) => AwsClientStub<TClient>;
|
|
471
|
+
/**
|
|
472
|
+
* Create a mock for a specific AWS SDK client instance.
|
|
473
|
+
*
|
|
474
|
+
* Use this function when you want to mock a single client instance.
|
|
475
|
+
*
|
|
476
|
+
* @category Core Functions
|
|
477
|
+
*
|
|
478
|
+
* @param clientInstance - The AWS SDK client instance to mock
|
|
479
|
+
* @returns A client stub for configuring mock behaviors
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* ```typescript
|
|
483
|
+
* import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
|
|
484
|
+
* import { mockClientInstance } from 'aws-sdk-vitest-mock';
|
|
485
|
+
*
|
|
486
|
+
* const client = new S3Client({});
|
|
487
|
+
* const s3Mock = mockClientInstance(client);
|
|
488
|
+
*
|
|
489
|
+
* s3Mock.on(GetObjectCommand).resolves({ Body: 'file contents' });
|
|
490
|
+
*
|
|
491
|
+
* const result = await client.send(new GetObjectCommand({ Bucket: 'my-bucket', Key: 'file.txt' }));
|
|
492
|
+
* // result.Body === 'file contents'
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
78
495
|
export declare const mockClientInstance: <TClient extends AnyClient>(clientInstance: TClient) => AwsClientStub<AnyClient>;
|
|
79
496
|
export {};
|