serverless-spy
Version:
CDK-based library for writing elegant integration tests on AWS serverless architecture and an additional web console to monitor events in real time.
158 lines (157 loc) • 8.57 kB
TypeScript
import { DynamoDBSpyEvent } from "../common/spyEvents/DynamoDBSpyEvent.js";
import { EventBridgeRuleSpyEvent } from "../common/spyEvents/EventBridgeRuleSpyEvent.js";
import { EventBridgeSpyEvent } from "../common/spyEvents/EventBridgeSpyEvent.js";
import { FunctionContext } from "../common/spyEvents/FunctionContext.js";
import { FunctionConsole } from "../common/spyEvents/FunctionConsole.js";
import { FunctionConsoleSpyEvent } from "../common/spyEvents/FunctionConsoleSpyEvent.js";
import { FunctionErrorSpyEvent } from "../common/spyEvents/FunctionErrorSpyEvent.js";
import { FunctionRequestSpyEvent } from "../common/spyEvents/FunctionRequestSpyEvent.js";
import { FunctionResponseSpyEvent } from "../common/spyEvents/FunctionResponseSpyEvent.js";
import { S3SpyEvent } from "../common/spyEvents/S3SpyEvent.js";
import { SnsSubscriptionSpyEvent } from "../common/spyEvents/SnsSubscriptionSpyEvent.js";
import { SnsTopicSpyEvent } from "../common/spyEvents/SnsTopicSpyEvent.js";
import { SqsSpyEvent } from "../common/spyEvents/SqsSpyEvent.js";
import { PrettifyForDisplay } from "./PrettifyForDisplay.js";
import { RecursivePartial } from "./RecursivePartial.js";
//#region listener/SpyHandlers.ts.d.ts
type MyJestMatchers = ReturnType<typeof expect>;
type MyJestMatchersWitOnlyFunctions = Pick<MyJestMatchers, { [P in keyof MyJestMatchers]: MyJestMatchers[P] extends ((...args: any) => any) ? P : never }[keyof MyJestMatchers]>;
type JestExpectWithSpyMethods<TSpyEvent, TSpyHandler> = Omit<{ [K in keyof MyJestMatchersWitOnlyFunctions]: (...args: Parameters<MyJestMatchersWitOnlyFunctions[K]>) => TSpyHandler }, 'toContainEqual' | 'toEqual' | 'toMatchObject' | 'toStrictEqual'> & {
/**
* Used when you want to check that an item is in a list.
* For testing the items in the list, this matcher recursively checks the
* equality of all fields, rather than checking for object identity.
*
* Optionally, you can provide a type for the expected value via a generic.
* This is particularly useful for ensuring expected objects have the right structure.
*/
toContainEqual(expected: TSpyEvent): TSpyHandler;
/**
* Used when you want to check that two objects have the same value.
* This matcher recursively checks the equality of all fields, rather than checking for object identity.
*
* Optionally, you can provide a type for the expected value via a generic.
* This is particularly useful for ensuring expected objects have the right structure.
*/
toEqual(expected: TSpyEvent): TSpyHandler;
/**
* Used to check that a JavaScript object matches a subset of the properties of an object
*
* Optionally, you can provide an object to use as Generic type for the expected value.
* This ensures that the matching object matches the structure of the provided object-like type.
*
* @example
*
* type House = {
* bath: boolean;
* bedrooms: number;
* kitchen: {
* amenities: string[];
* area: number;
* wallColor: string;
* }
* };
*
* expect(desiredHouse).toMatchObject<House>({...standardHouse, kitchen: {area: 20}}) // wherein standardHouse is some base object of type House
*/
toMatchObject(expected: PrettifyForDisplay<RecursivePartial<TSpyEvent>>): TSpyHandler;
/**
* Use to test that objects have the same types as well as structure.
*
* Optionally, you can provide a type for the expected value via a generic.
* This is particularly useful for ensuring expected objects have the right structure.
*/
toStrictEqual(expected: TSpyEvent): TSpyHandler;
};
interface DynamoDBSpyHandler<TData = any> extends JestExpectWithSpyMethods<DynamoDBSpyEvent<TData>, DynamoDBSpyHandler<TData>> {
getData: () => PrettifyForDisplay<DynamoDBSpyEvent<TData>>;
}
interface EventBridgeRuleSpyHandler<TData = any> extends JestExpectWithSpyMethods<EventBridgeRuleSpyEvent<TData>, EventBridgeRuleSpyHandler<TData>> {
getData: () => PrettifyForDisplay<EventBridgeRuleSpyEvent<TData>>;
}
interface EventBridgeSpyHandler<TData = any> extends JestExpectWithSpyMethods<EventBridgeSpyEvent<TData>, EventBridgeSpyHandler<TData>> {
getData: () => PrettifyForDisplay<EventBridgeSpyEvent<TData>>;
}
interface FunctionBaseSpyHandler<TData = any> extends JestExpectWithSpyMethods<FunctionRequestSpyEvent<TData>, FunctionRequestSpyHandler<TData>> {
followedByError: (param?: {
condition?: (event: {
spyEventType: 'FunctionError';
request: TData;
context: FunctionContext;
error: any;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionBaseSpyHandler<TData> & FunctionErrorSpyHandler<TData> & JestExpectWithSpyMethods<FunctionErrorSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
followedByConsole: (param?: {
condition?: (event: {
spyEventType: 'FunctionConsole';
request: TData;
context: FunctionContext;
console: FunctionConsole;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionBaseSpyHandler<TData> & FunctionConsoleSpyHandler<TData> & JestExpectWithSpyMethods<FunctionConsoleSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
followedByResponse: <TDataResponse = any>(param?: {
condition?: (event: {
spyEventType: 'FunctionResponse';
request: TData;
context: FunctionContext;
response: TDataResponse;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionResponseSpyHandler<TData> & JestExpectWithSpyMethods<FunctionRequestSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
}
interface FunctionRequestSpyHandler<TData = any> extends FunctionBaseSpyHandler<TData> {
getData: () => PrettifyForDisplay<FunctionRequestSpyEvent<TData>>;
}
interface FunctionConsoleSpyHandler<TData = any> extends JestExpectWithSpyMethods<FunctionConsoleSpyEvent<TData>, FunctionConsoleSpyHandler<TData>> {
getData: () => PrettifyForDisplay<FunctionConsoleSpyEvent<TData>>;
followedByError: (param?: {
condition?: (event: {
spyEventType: 'FunctionError';
request: TData;
context: FunctionContext;
error: any;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionBaseSpyHandler<TData> & FunctionErrorSpyHandler<TData> & JestExpectWithSpyMethods<FunctionErrorSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
followedByConsole: (param?: {
condition?: (event: {
spyEventType: 'FunctionConsole';
request: TData;
context: FunctionContext;
console: FunctionConsole;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionBaseSpyHandler<TData> & FunctionConsoleSpyHandler<TData> & JestExpectWithSpyMethods<FunctionConsoleSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
followedByResponse: <TDataResponse = any>(param?: {
condition?: (event: {
spyEventType: 'FunctionResponse';
request: TData;
context: FunctionContext;
response: TDataResponse;
}) => boolean;
timoutMs?: number;
}) => Promise<FunctionResponseSpyHandler<TData> & JestExpectWithSpyMethods<FunctionRequestSpyEvent<TData>, FunctionRequestSpyHandler<TData>>>;
}
interface FunctionErrorSpyHandler<TData = any> extends JestExpectWithSpyMethods<FunctionErrorSpyEvent<TData>, FunctionErrorSpyHandler<TData>> {
getData: () => PrettifyForDisplay<FunctionErrorSpyEvent<TData>>;
}
interface FunctionResponseSpyHandler<TData = any> extends JestExpectWithSpyMethods<FunctionResponseSpyEvent<TData>, FunctionResponseSpyHandler<TData>> {
getData: () => PrettifyForDisplay<FunctionResponseSpyEvent<TData>>;
}
interface S3SpyHandler extends JestExpectWithSpyMethods<S3SpyEvent, S3SpyHandler> {
getData: () => S3SpyEvent;
}
interface SnsSubscriptionSpyHandler<TData = any> extends JestExpectWithSpyMethods<SnsSubscriptionSpyEvent<TData>, SnsSubscriptionSpyHandler<TData>> {
getData: () => PrettifyForDisplay<SnsSubscriptionSpyEvent<TData>>;
}
interface SnsTopicSpyHandler<TData = any> extends JestExpectWithSpyMethods<SnsTopicSpyEvent<TData>, SnsTopicSpyHandler<TData>> {
getData: () => PrettifyForDisplay<SnsTopicSpyEvent<TData>>;
}
interface SqsSpyHandler<TData = any> extends JestExpectWithSpyMethods<SqsSpyEvent<TData>, SqsSpyHandler<TData>> {
getData: () => PrettifyForDisplay<SqsSpyEvent<TData>>;
}
//#endregion
export { DynamoDBSpyHandler, EventBridgeRuleSpyHandler, EventBridgeSpyHandler, FunctionBaseSpyHandler, FunctionConsoleSpyHandler, FunctionErrorSpyHandler, FunctionRequestSpyHandler, FunctionResponseSpyHandler, S3SpyHandler, SnsSubscriptionSpyHandler, SnsTopicSpyHandler, SqsSpyHandler };
//# sourceMappingURL=SpyHandlers.ts.d.ts.map