aws-sdk-vitest-mock 0.2.0 → 0.3.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/index.cjs +1 -1
- package/index.js +38 -25
- package/lib/matchers.d.ts +14 -14
- package/lib/mock-client.d.ts +17 -16
- package/{matchers-_zpN1oru.js → matchers-C6AtmwWz.js} +3 -7
- package/{matchers-G36N2DNa.cjs → matchers-Rq18z2C7.cjs} +1 -1
- package/package.json +1 -1
- package/vitest-setup.cjs +1 -1
- package/vitest-setup.js +1 -1
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("vitest"),P=require("node:fs"),R=require("node:path"),N=require("node:stream"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("vitest"),P=require("node:fs"),R=require("node:path"),N=require("node:stream"),C=require("./matchers-Rq18z2C7.cjs");class d extends Error{constructor(e,s,c,n){super(e),this.name="AwsError",this.code=s,this.statusCode=c,this.retryable=n}}const W=t=>{const e=t?`The specified key does not exist. Key: ${t}`:"The specified key does not exist.";return new d(e,"NoSuchKey",404,!1)},M=t=>{const e=t?`The specified bucket does not exist. Bucket: ${t}`:"The specified bucket does not exist.";return new d(e,"NoSuchBucket",404,!1)},$=t=>{const e=t?`Access Denied for resource: ${t}`:"Access Denied";return new d(e,"AccessDenied",403,!1)},D=t=>{const e=t?`Requested resource not found: ${t}`:"Requested resource not found";return new d(e,"ResourceNotFoundException",400,!1)},I=()=>new d("The conditional request failed","ConditionalCheckFailedException",400,!1),O=()=>new d("Rate exceeded","Throttling",400,!0),T=()=>new d("We encountered an internal error. Please try again.","InternalServerError",500,!0);function k(){return{enabled:!1,log(t,e){this.enabled&&(e===void 0?console.log(`[aws-sdk-vitest-mock](Debug) ${t}`):console.log(`[aws-sdk-vitest-mock](Debug) ${t}`,e))}}}function y(t){t.enabled=!0}function v(t){t.enabled=!1}function F(t){const e=R.resolve(t),s=P.readFileSync(e,"utf8");return t.endsWith(".json")?JSON.parse(s):s}function L(t,e={}){const{pageSize:s=10,tokenKey:c="NextToken",itemsKey:n="Items"}=e;if(t.length===0)return[{[n]:[]}];const o=[];for(let r=0;r<t.length;r+=s){const u=t.slice(r,r+s),i=r+s<t.length,l={[n]:u};if(i){const a=l;a[c]=`token-${r+s}`}o.push(l)}return o}function A(){return typeof process<"u"&&process.versions?.node?"node":typeof process<"u"&&process.versions?.bun?"bun":"browser"}function K(t){const e=typeof t=="string"?Buffer.from(t,"utf8"):Buffer.from(t);return new N.Readable({read(){this.push(e),this.push(void 0)}})}function q(t){let e;return typeof t=="string"?e=new TextEncoder().encode(t):t instanceof Buffer?e=new Uint8Array(t):e=t,new ReadableStream({start(s){s.enqueue(e),s.close()}})}function h(t){const e=A();return e==="node"||e==="bun"?K(t):q(t)}function w(t,e){return Object.keys(e).every(s=>{const c=e[s],n=t[s];return c&&typeof c=="object"&&!Array.isArray(c)?typeof n!="object"||n===null?!1:w(n,c):n===c})}function j(t,e){if(t===e)return!0;if(typeof t!="object"||t===null||typeof e!="object"||e===null)return t===e;const s=Object.keys(t),c=Object.keys(e);return s.length!==c.length?!1:c.every(n=>{if(!Object.prototype.hasOwnProperty.call(t,n))return!1;const o=t,r=e,u=o[n],i=r[n];return typeof u=="object"&&u!==null&&typeof i=="object"&&i!==null?j(u,i):u===i})}function S(t){return async function(e){const s=()=>this;t.debugLogger.log(`Received command: ${e.constructor.name}`,e.input);const c=t.map.get(e.constructor);if(c){t.debugLogger.log(`Found ${c.length} mock(s) for ${e.constructor.name}`);const n=c.findIndex(o=>o.strict?o.matcher&&j(e.input,o.matcher):!o.matcher||w(e.input,o.matcher));if(n===-1)t.debugLogger.log(`No matching mock found for ${e.constructor.name}`,e.input);else{const o=c[n];if(!o)throw new Error(`Mock at index ${n} not found`);return t.debugLogger.log(`Using mock at index ${n} for ${e.constructor.name}`),o.once&&(c.splice(n,1),t.debugLogger.log(`Removed one-time mock for ${e.constructor.name}`)),o.handler(e.input,s())}}else t.debugLogger.log(`No mocks configured for ${e.constructor.name}`);throw new Error(`No mock configured for command: ${e.constructor.name}`)}}function x(t,e,s,c={}){const n=(r,u)=>{const i={matcher:s,handler:r,once:u,strict:!!c.strict},l=t.map.get(e)??[];if(u){const a=l.findIndex(f=>!f.once);a===-1?l.push(i):l.splice(a,0,i),t.map.set(e,l)}else{const a=l.filter(f=>f.once||JSON.stringify(f.matcher)!==JSON.stringify(s));a.push(i),t.map.set(e,a)}},o={resolves(r){return n(()=>Promise.resolve(r),!1),o},rejects(r){return n(()=>{const u=typeof r=="string"?new Error(r):r;return Promise.reject(u)},!1),o},callsFake(r){return n(r,!1),o},resolvesOnce(r){return n(()=>Promise.resolve(r),!0),o},rejectsOnce(r){return n(()=>{const u=typeof r=="string"?new Error(r):r;return Promise.reject(u)},!0),o},callsFakeOnce(r){return n(r,!0),o},resolvesStream(r){return n(()=>Promise.resolve({Body:h(r)}),!1),o},resolvesStreamOnce(r){return n(()=>Promise.resolve({Body:h(r)}),!0),o},resolvesWithDelay(r,u){const i=l=>{setTimeout(()=>l(r),u)};return n(()=>new Promise(i),!1),o},rejectsWithDelay(r,u){const i=typeof r=="string"?new Error(r):r,l=(a,f)=>{setTimeout(()=>f(i),u)};return n(()=>new Promise(l),!1),o},rejectsWithNoSuchKey(r){return n(()=>Promise.reject(W(r)),!1),o},rejectsWithNoSuchBucket(r){return n(()=>Promise.reject(M(r)),!1),o},rejectsWithAccessDenied(r){return n(()=>Promise.reject($(r)),!1),o},rejectsWithResourceNotFound(r){return n(()=>Promise.reject(D(r)),!1),o},rejectsWithConditionalCheckFailed(){return n(()=>Promise.reject(I()),!1),o},rejectsWithThrottling(){return n(()=>Promise.reject(O()),!1),o},rejectsWithInternalServerError(){return n(()=>Promise.reject(T()),!1),o},resolvesPaginated(r,u={}){const i=L(r,u);let l=0;return n(a=>{const f=u.tokenKey||"NextToken",p=a[f];if(p){const g=/token-(\d+)/.exec(p);if(g&&g[1]){const E=g[1];l=Math.floor(Number.parseInt(E,10)/(u.pageSize||10))}}else l=0;const m=i[l]||i[i.length-1]||i[0];if(!m)throw new Error("No paginated responses available");return l=Math.min(l+1,i.length-1),Promise.resolve(m)},!1),o},resolvesFromFile(r){return n(()=>{const u=F(r);return Promise.resolve(u)},!1),o}};return o}const B=t=>{const e={map:new WeakMap,debugLogger:k()},s=t.prototype,c=b.vi.spyOn(s,"send").mockImplementation(S(e));return{client:void 0,on:(o,r,u)=>x(e,o,r,u),reset:()=>{c.mockClear(),e.map=new WeakMap},restore:()=>{c.mockRestore(),e.map=new WeakMap},calls:()=>c.mock.calls.map(o=>o[0]),__rawCalls:()=>c.mock.calls,enableDebug:()=>{y(e.debugLogger)},disableDebug:()=>{v(e.debugLogger)}}},_=t=>{const e={map:new WeakMap,debugLogger:k()},s=b.vi.spyOn(t,"send").mockImplementation(S(e));return{client:t,on:(n,o,r)=>x(e,n,o,r),reset:()=>{s.mockClear(),e.map=new WeakMap},restore:()=>{s.mockRestore(),e.map=new WeakMap},calls:()=>s.mock.calls.map(n=>n[0]),__rawCalls:()=>s.mock.calls,enableDebug:()=>{y(e.debugLogger)},disableDebug:()=>{v(e.debugLogger)}}};exports.matchers=C.matchers;exports.mockClient=B;exports.mockClientInstance=_;
|
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { vi as b } from "vitest";
|
|
|
2
2
|
import { readFileSync as P } from "node:fs";
|
|
3
3
|
import R from "node:path";
|
|
4
4
|
import { Readable as N } from "node:stream";
|
|
5
|
-
import { m as X } from "./matchers-
|
|
5
|
+
import { m as X } from "./matchers-C6AtmwWz.js";
|
|
6
6
|
class d extends Error {
|
|
7
7
|
constructor(e, s, c, n) {
|
|
8
8
|
super(e), this.name = "AwsError", this.code = s, this.statusCode = c, this.retryable = n;
|
|
@@ -11,16 +11,16 @@ class d extends Error {
|
|
|
11
11
|
const W = (t) => {
|
|
12
12
|
const e = t ? `The specified key does not exist. Key: ${t}` : "The specified key does not exist.";
|
|
13
13
|
return new d(e, "NoSuchKey", 404, !1);
|
|
14
|
-
},
|
|
14
|
+
}, C = (t) => {
|
|
15
15
|
const e = t ? `The specified bucket does not exist. Bucket: ${t}` : "The specified bucket does not exist.";
|
|
16
16
|
return new d(e, "NoSuchBucket", 404, !1);
|
|
17
|
-
},
|
|
17
|
+
}, $ = (t) => {
|
|
18
18
|
const e = t ? `Access Denied for resource: ${t}` : "Access Denied";
|
|
19
19
|
return new d(e, "AccessDenied", 403, !1);
|
|
20
|
-
},
|
|
20
|
+
}, D = (t) => {
|
|
21
21
|
const e = t ? `Requested resource not found: ${t}` : "Requested resource not found";
|
|
22
22
|
return new d(e, "ResourceNotFoundException", 400, !1);
|
|
23
|
-
},
|
|
23
|
+
}, M = () => new d(
|
|
24
24
|
"The conditional request failed",
|
|
25
25
|
"ConditionalCheckFailedException",
|
|
26
26
|
400,
|
|
@@ -42,7 +42,7 @@ function k() {
|
|
|
42
42
|
function y(t) {
|
|
43
43
|
t.enabled = !0;
|
|
44
44
|
}
|
|
45
|
-
function
|
|
45
|
+
function w(t) {
|
|
46
46
|
t.enabled = !1;
|
|
47
47
|
}
|
|
48
48
|
function T(t) {
|
|
@@ -87,10 +87,10 @@ function h(t) {
|
|
|
87
87
|
const e = L();
|
|
88
88
|
return e === "node" || e === "bun" ? A(t) : K(t);
|
|
89
89
|
}
|
|
90
|
-
function
|
|
90
|
+
function v(t, e) {
|
|
91
91
|
return Object.keys(e).every((s) => {
|
|
92
92
|
const c = e[s], n = t[s];
|
|
93
|
-
return c && typeof c == "object" && !Array.isArray(c) ? typeof n != "object" || n === null ? !1 :
|
|
93
|
+
return c && typeof c == "object" && !Array.isArray(c) ? typeof n != "object" || n === null ? !1 : v(
|
|
94
94
|
n,
|
|
95
95
|
c
|
|
96
96
|
) : n === c;
|
|
@@ -121,7 +121,7 @@ function x(t) {
|
|
|
121
121
|
t.debugLogger.log(
|
|
122
122
|
`Found ${c.length} mock(s) for ${e.constructor.name}`
|
|
123
123
|
);
|
|
124
|
-
const n = c.findIndex((o) => o.strict ? o.matcher && j(e.input, o.matcher) : !o.matcher ||
|
|
124
|
+
const n = c.findIndex((o) => o.strict ? o.matcher && j(e.input, o.matcher) : !o.matcher || v(e.input, o.matcher));
|
|
125
125
|
if (n === -1)
|
|
126
126
|
t.debugLogger.log(
|
|
127
127
|
`No matching mock found for ${e.constructor.name}`,
|
|
@@ -135,7 +135,10 @@ function x(t) {
|
|
|
135
135
|
`Using mock at index ${n} for ${e.constructor.name}`
|
|
136
136
|
), o.once && (c.splice(n, 1), t.debugLogger.log(
|
|
137
137
|
`Removed one-time mock for ${e.constructor.name}`
|
|
138
|
-
)), o.handler(
|
|
138
|
+
)), o.handler(
|
|
139
|
+
e.input,
|
|
140
|
+
s()
|
|
141
|
+
);
|
|
139
142
|
}
|
|
140
143
|
} else
|
|
141
144
|
t.debugLogger.log(
|
|
@@ -153,9 +156,7 @@ function S(t, e, s, c = {}) {
|
|
|
153
156
|
handler: r,
|
|
154
157
|
once: u,
|
|
155
158
|
strict: !!c.strict
|
|
156
|
-
}, l = t.map.get(
|
|
157
|
-
e
|
|
158
|
-
) ?? [];
|
|
159
|
+
}, l = t.map.get(e) ?? [];
|
|
159
160
|
if (u) {
|
|
160
161
|
const a = l.findIndex((f) => !f.once);
|
|
161
162
|
a === -1 ? l.push(i) : l.splice(a, 0, i), t.map.set(
|
|
@@ -224,20 +225,20 @@ function S(t, e, s, c = {}) {
|
|
|
224
225
|
return n(() => Promise.reject(W(r)), !1), o;
|
|
225
226
|
},
|
|
226
227
|
rejectsWithNoSuchBucket(r) {
|
|
227
|
-
return n(() => Promise.reject(
|
|
228
|
+
return n(() => Promise.reject(C(r)), !1), o;
|
|
228
229
|
},
|
|
229
230
|
rejectsWithAccessDenied(r) {
|
|
230
|
-
return n(() => Promise.reject(
|
|
231
|
+
return n(() => Promise.reject($(r)), !1), o;
|
|
231
232
|
},
|
|
232
233
|
rejectsWithResourceNotFound(r) {
|
|
233
234
|
return n(
|
|
234
|
-
() => Promise.reject(
|
|
235
|
+
() => Promise.reject(D(r)),
|
|
235
236
|
!1
|
|
236
237
|
), o;
|
|
237
238
|
},
|
|
238
239
|
rejectsWithConditionalCheckFailed() {
|
|
239
240
|
return n(
|
|
240
|
-
() => Promise.reject(
|
|
241
|
+
() => Promise.reject(M()),
|
|
241
242
|
!1
|
|
242
243
|
), o;
|
|
243
244
|
},
|
|
@@ -281,26 +282,32 @@ function S(t, e, s, c = {}) {
|
|
|
281
282
|
};
|
|
282
283
|
return o;
|
|
283
284
|
}
|
|
284
|
-
const
|
|
285
|
+
const U = (t) => {
|
|
285
286
|
const e = {
|
|
286
287
|
map: /* @__PURE__ */ new WeakMap(),
|
|
287
288
|
debugLogger: k()
|
|
288
289
|
}, s = t.prototype, c = b.spyOn(s, "send").mockImplementation(x(e));
|
|
289
290
|
return {
|
|
290
291
|
client: void 0,
|
|
291
|
-
on: (o, r, u) => S(
|
|
292
|
+
on: (o, r, u) => S(
|
|
293
|
+
e,
|
|
294
|
+
o,
|
|
295
|
+
r,
|
|
296
|
+
u
|
|
297
|
+
),
|
|
292
298
|
reset: () => {
|
|
293
299
|
c.mockClear(), e.map = /* @__PURE__ */ new WeakMap();
|
|
294
300
|
},
|
|
295
301
|
restore: () => {
|
|
296
302
|
c.mockRestore(), e.map = /* @__PURE__ */ new WeakMap();
|
|
297
303
|
},
|
|
298
|
-
calls: () => c.mock.calls,
|
|
304
|
+
calls: () => c.mock.calls.map((o) => o[0]),
|
|
305
|
+
__rawCalls: () => c.mock.calls,
|
|
299
306
|
enableDebug: () => {
|
|
300
307
|
y(e.debugLogger);
|
|
301
308
|
},
|
|
302
309
|
disableDebug: () => {
|
|
303
|
-
|
|
310
|
+
w(e.debugLogger);
|
|
304
311
|
}
|
|
305
312
|
};
|
|
306
313
|
}, G = (t) => {
|
|
@@ -310,24 +317,30 @@ const _ = (t) => {
|
|
|
310
317
|
}, s = b.spyOn(t, "send").mockImplementation(x(e));
|
|
311
318
|
return {
|
|
312
319
|
client: t,
|
|
313
|
-
on: (n, o, r) => S(
|
|
320
|
+
on: (n, o, r) => S(
|
|
321
|
+
e,
|
|
322
|
+
n,
|
|
323
|
+
o,
|
|
324
|
+
r
|
|
325
|
+
),
|
|
314
326
|
reset: () => {
|
|
315
327
|
s.mockClear(), e.map = /* @__PURE__ */ new WeakMap();
|
|
316
328
|
},
|
|
317
329
|
restore: () => {
|
|
318
330
|
s.mockRestore(), e.map = /* @__PURE__ */ new WeakMap();
|
|
319
331
|
},
|
|
320
|
-
calls: () => s.mock.calls,
|
|
332
|
+
calls: () => s.mock.calls.map((n) => n[0]),
|
|
333
|
+
__rawCalls: () => s.mock.calls,
|
|
321
334
|
enableDebug: () => {
|
|
322
335
|
y(e.debugLogger);
|
|
323
336
|
},
|
|
324
337
|
disableDebug: () => {
|
|
325
|
-
|
|
338
|
+
w(e.debugLogger);
|
|
326
339
|
}
|
|
327
340
|
};
|
|
328
341
|
};
|
|
329
342
|
export {
|
|
330
343
|
X as matchers,
|
|
331
|
-
|
|
344
|
+
U as mockClient,
|
|
332
345
|
G as mockClientInstance
|
|
333
346
|
};
|
package/lib/matchers.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { MetadataBearer } from '@smithy/types';
|
|
2
|
+
import { AwsClientStub, CommandConstructor } from './mock-client.js';
|
|
3
3
|
interface MatcherResult {
|
|
4
4
|
pass: boolean;
|
|
5
5
|
message: () => string;
|
|
6
6
|
}
|
|
7
7
|
export declare const matchers: {
|
|
8
|
-
toHaveReceivedCommand(received: AwsClientStub, command: CommandConstructor): MatcherResult;
|
|
9
|
-
toHaveReceivedCommandTimes(received: AwsClientStub, command: CommandConstructor, times: number): MatcherResult;
|
|
10
|
-
toHaveReceivedCommandWith(this: {
|
|
8
|
+
toHaveReceivedCommand<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, command: CommandConstructor<TInput, TOutput>): MatcherResult;
|
|
9
|
+
toHaveReceivedCommandTimes<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, command: CommandConstructor<TInput, TOutput>, times: number): MatcherResult;
|
|
10
|
+
toHaveReceivedCommandWith<TInput extends object, TOutput extends MetadataBearer>(this: {
|
|
11
11
|
equals: (a: unknown, b: unknown) => boolean;
|
|
12
|
-
}, received: AwsClientStub, command: CommandConstructor, input:
|
|
13
|
-
toHaveReceivedNthCommandWith(this: {
|
|
12
|
+
}, received: AwsClientStub, command: CommandConstructor<TInput, TOutput>, input: TInput): MatcherResult;
|
|
13
|
+
toHaveReceivedNthCommandWith<TInput extends object, TOutput extends MetadataBearer>(this: {
|
|
14
14
|
equals: (a: unknown, b: unknown) => boolean;
|
|
15
|
-
}, received: AwsClientStub, n: number, command: CommandConstructor, input:
|
|
16
|
-
toHaveReceivedNoOtherCommands(received: AwsClientStub, expectedCommands?: CommandConstructor[]): MatcherResult;
|
|
15
|
+
}, received: AwsClientStub, n: number, command: CommandConstructor<TInput, TOutput>, input: TInput): MatcherResult;
|
|
16
|
+
toHaveReceivedNoOtherCommands<TInput extends object, TOutput extends MetadataBearer>(received: AwsClientStub, expectedCommands?: CommandConstructor<TInput, TOutput>[]): MatcherResult;
|
|
17
17
|
};
|
|
18
18
|
export interface AwsSdkMatchers<R = unknown> {
|
|
19
|
-
toHaveReceivedCommand(command: CommandConstructor): R;
|
|
20
|
-
toHaveReceivedCommandTimes(command: CommandConstructor, times: number): R;
|
|
21
|
-
toHaveReceivedCommandWith(command: CommandConstructor, input:
|
|
22
|
-
toHaveReceivedNthCommandWith(n: number, command: CommandConstructor, input:
|
|
23
|
-
toHaveReceivedNoOtherCommands(expectedCommands?: CommandConstructor[]): R;
|
|
19
|
+
toHaveReceivedCommand<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>): R;
|
|
20
|
+
toHaveReceivedCommandTimes<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>, times: number): R;
|
|
21
|
+
toHaveReceivedCommandWith<TInput extends object, TOutput extends MetadataBearer>(command: CommandConstructor<TInput, TOutput>, input: TInput): R;
|
|
22
|
+
toHaveReceivedNthCommandWith<TInput extends object, TOutput extends MetadataBearer>(n: number, command: CommandConstructor<TInput, TOutput>, input: TInput): R;
|
|
23
|
+
toHaveReceivedNoOtherCommands<TInput extends object, TOutput extends MetadataBearer>(expectedCommands?: CommandConstructor<TInput, TOutput>[]): R;
|
|
24
24
|
}
|
|
25
25
|
export type { MatcherResult };
|
|
26
26
|
declare module "vitest" {
|
package/lib/mock-client.d.ts
CHANGED
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
import { SmithyResolvedConfiguration } from '@smithy/smithy-client';
|
|
2
|
-
import {
|
|
1
|
+
import { Command as SmithyCommand, SmithyResolvedConfiguration } from '@smithy/smithy-client';
|
|
2
|
+
import { HttpHandlerOptions, MetadataBearer } from '@smithy/types';
|
|
3
3
|
import { Mock } from 'vitest';
|
|
4
4
|
import { PaginatorOptions } from './utils/paginator-helpers.js';
|
|
5
5
|
import { StreamInput } from './utils/stream-helpers.js';
|
|
6
|
-
export
|
|
7
|
-
$metadata?: unknown;
|
|
8
|
-
}
|
|
9
|
-
export interface StructuralCommand<TInput extends object, TOutput extends MetadataBearer> {
|
|
6
|
+
export type StructuralCommand<TInput extends object, TOutput extends MetadataBearer> = SmithyCommand<TInput, TOutput, any, any, any> | {
|
|
10
7
|
readonly input: TInput;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
8
|
+
readonly __awsSdkVitestMockOutput?: TOutput;
|
|
9
|
+
};
|
|
14
10
|
export type CommandConstructor<TInput extends object, TOutput extends MetadataBearer> = new (input: TInput) => StructuralCommand<TInput, TOutput>;
|
|
11
|
+
type AwsCommandConstructor = CommandConstructor<any, MetadataBearer>;
|
|
12
|
+
export type CommandInputType<TCtor extends AwsCommandConstructor> = ConstructorParameters<TCtor>[0];
|
|
13
|
+
export type CommandOutputType<TCtor extends AwsCommandConstructor> = InstanceType<TCtor> extends StructuralCommand<any, infer TOutput> ? TOutput : MetadataBearer;
|
|
15
14
|
export type AnyClient = {
|
|
16
|
-
send(command:
|
|
17
|
-
config: SmithyResolvedConfiguration<unknown>;
|
|
15
|
+
send(command: AwsSdkCommand): Promise<MetadataBearer>;
|
|
16
|
+
config: SmithyResolvedConfiguration<HttpHandlerOptions> | Record<string, unknown>;
|
|
18
17
|
};
|
|
19
|
-
export type
|
|
20
|
-
export type ClientConstructor<TClient extends AnyClient> = (abstract new (
|
|
18
|
+
export type AwsSdkCommand = StructuralCommand<object, MetadataBearer>;
|
|
19
|
+
export type ClientConstructor<TClient extends AnyClient> = (abstract new (...args: unknown[]) => TClient) | {
|
|
21
20
|
prototype: TClient;
|
|
22
21
|
};
|
|
23
|
-
type CommandHandler<TInput extends object = object, TOutput extends MetadataBearer = MetadataBearer, TClient extends AnyClient = AnyClient> = (input: TInput,
|
|
22
|
+
type CommandHandler<TInput extends object = object, TOutput extends MetadataBearer = MetadataBearer, TClient extends AnyClient = AnyClient> = (input: TInput, clientInstance: TClient | undefined) => Promise<Partial<TOutput>>;
|
|
24
23
|
interface MockOptions {
|
|
25
24
|
strict?: boolean;
|
|
26
25
|
}
|
|
27
26
|
export interface AwsClientStub<TClient extends AnyClient = AnyClient> {
|
|
28
27
|
readonly client: TClient | undefined;
|
|
29
|
-
on: <
|
|
28
|
+
on: <TCtor extends AwsCommandConstructor>(command: TCtor, request?: Partial<CommandInputType<TCtor>>, options?: MockOptions) => AwsCommandStub<CommandInputType<TCtor>, CommandOutputType<TCtor>, TClient>;
|
|
30
29
|
reset: () => void;
|
|
31
30
|
restore: () => void;
|
|
32
|
-
calls: () =>
|
|
31
|
+
calls: () => AwsSdkCommand[];
|
|
32
|
+
/** @internal - For use by matchers only */
|
|
33
|
+
__rawCalls: () => ReturnType<Mock["mock"]["calls"]["slice"]>;
|
|
33
34
|
enableDebug: () => void;
|
|
34
35
|
disableDebug: () => void;
|
|
35
36
|
}
|
|
@@ -8,7 +8,7 @@ const e = {
|
|
|
8
8
|
gray: (t) => `\x1B[90m${t}\x1B[39m`,
|
|
9
9
|
bold: (t) => `\x1B[1m${t}\x1B[22m`
|
|
10
10
|
}, $ = (t) => {
|
|
11
|
-
const c = t.
|
|
11
|
+
const c = t.__rawCalls();
|
|
12
12
|
return Array.isArray(c) ? c.filter(
|
|
13
13
|
(a) => Array.isArray(a) && a.length > 0
|
|
14
14
|
) : [];
|
|
@@ -37,9 +37,7 @@ const e = {
|
|
|
37
37
|
toHaveReceivedCommandWith(t, c, a) {
|
|
38
38
|
const r = $(t).filter(
|
|
39
39
|
(n) => n[0] instanceof c
|
|
40
|
-
), m = r.some(
|
|
41
|
-
(n) => this.equals(n[0].input, a)
|
|
42
|
-
), o = c.name;
|
|
40
|
+
), m = r.some((n) => this.equals(n[0].input, a)), o = c.name;
|
|
43
41
|
return {
|
|
44
42
|
pass: m,
|
|
45
43
|
message: () => {
|
|
@@ -59,9 +57,7 @@ ${u}
|
|
|
59
57
|
|
|
60
58
|
But ${i}`;
|
|
61
59
|
}
|
|
62
|
-
const n = r.map(
|
|
63
|
-
(s) => s[0].input
|
|
64
|
-
), d = e.red(o), g = e.cyan(JSON.stringify(a, void 0, 2)), l = e.gray("But received"), h = e.yellow(o), p = e.gray("with"), S = e.yellow(
|
|
60
|
+
const n = r.map((s) => s[0].input), d = e.red(o), g = e.cyan(JSON.stringify(a, void 0, 2)), l = e.gray("But received"), h = e.yellow(o), p = e.gray("with"), S = e.yellow(
|
|
65
61
|
n.map((s) => JSON.stringify(s, void 0, 2)).join(`
|
|
66
62
|
|
|
67
63
|
`)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";const e={red:t=>`\x1B[31m${t}\x1B[39m`,green:t=>`\x1B[32m${t}\x1B[39m`,yellow:t=>`\x1B[33m${t}\x1B[39m`,blue:t=>`\x1B[34m${t}\x1B[39m`,magenta:t=>`\x1B[35m${t}\x1B[39m`,cyan:t=>`\x1B[36m${t}\x1B[39m`,gray:t=>`\x1B[90m${t}\x1B[39m`,bold:t=>`\x1B[1m${t}\x1B[22m`},$=t=>{const c=t.
|
|
1
|
+
"use strict";const e={red:t=>`\x1B[31m${t}\x1B[39m`,green:t=>`\x1B[32m${t}\x1B[39m`,yellow:t=>`\x1B[33m${t}\x1B[39m`,blue:t=>`\x1B[34m${t}\x1B[39m`,magenta:t=>`\x1B[35m${t}\x1B[39m`,cyan:t=>`\x1B[36m${t}\x1B[39m`,gray:t=>`\x1B[90m${t}\x1B[39m`,bold:t=>`\x1B[1m${t}\x1B[22m`},$=t=>{const c=t.__rawCalls();return Array.isArray(c)?c.filter(a=>Array.isArray(a)&&a.length>0):[]},f={toHaveReceivedCommand(t,c){const a=$(t),r=a.some(o=>o[0]instanceof c),m=c.name;return{pass:r,message:()=>{if(r)return`Expected AWS SDK mock not to have received command ${e.red(m)}`;const o=a.map(n=>n[0].constructor?.name??"Unknown");return o.length===0?`Expected AWS SDK mock to have received command ${e.red(m)}, but ${e.gray("no commands were received")}`:`Expected AWS SDK mock to have received command ${e.red(m)}, but received: ${e.yellow(o.join(", "))}`}}},toHaveReceivedCommandTimes(t,c,a){const r=$(t).filter(n=>n[0]instanceof c),m=r.length===a,o=c.name;return{pass:m,message:()=>m?`Expected AWS SDK mock not to have received command ${o} ${a} times`:`Expected AWS SDK mock to have received command ${o} ${a} times, but received ${r.length} times`}},toHaveReceivedCommandWith(t,c,a){const r=$(t).filter(n=>n[0]instanceof c),m=r.some(n=>this.equals(n[0].input,a)),o=c.name;return{pass:m,message:()=>{if(m){const s=e.red(o),u=e.cyan(JSON.stringify(a,void 0,2));return`Expected AWS SDK mock not to have received command ${s} with input:
|
|
2
2
|
${u}`}if(r.length===0){const s=e.red(o),u=e.cyan(JSON.stringify(a,void 0,2)),i=e.gray(`${o} was never called`);return`Expected AWS SDK mock to have received command ${s} with input:
|
|
3
3
|
${u}
|
|
4
4
|
|
package/package.json
CHANGED
package/vitest-setup.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("vitest"),t=require("./matchers-
|
|
1
|
+
"use strict";const e=require("vitest"),t=require("./matchers-Rq18z2C7.cjs");e.expect.extend(t.matchers);
|
package/vitest-setup.js
CHANGED