From e0f57bc7769ebd657ead566967073d4409820807 Mon Sep 17 00:00:00 2001 From: Valentin Beggi Date: Fri, 4 Feb 2022 15:56:11 +0100 Subject: [PATCH] feat: add getAllEvents method to EventBridge class --- README.md | 79 +++++++++++++++++++++----------------- src/helpers/eventBridge.ts | 34 +++++++++++++++- 2 files changed, 76 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index a77c847..e16b341 100644 --- a/README.md +++ b/README.md @@ -36,46 +36,46 @@ sls-test-tools is currently being actively maintained, yet is in alpha. Your fee ### EventBridge -``` - expect(eventBridgeEvents).toHaveEvent(); +```ts +expect(eventBridgeEvents).toHaveEvent(); - expect(eventBridgeEvents).toHaveEventWithSource("order.created"); +expect(eventBridgeEvents).toHaveEventWithSource("order.created"); ``` ### S3 Note: these async assertions require "await" -``` - await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME"); +```ts +await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME"); ``` -``` - await expect("BUCKET NAME").toExistAsS3Bucket(); +```ts +await expect("BUCKET NAME").toExistAsS3Bucket(); ``` -``` - await expect({ - bucketName: "BUCKET_NAME", - objectName: "FILE NAME", - }).toHaveContentTypeEqualTo("CONTENT_TYPE");; +```ts +await expect({ + bucketName: "BUCKET_NAME", + objectName: "FILE NAME", +}).toHaveContentTypeEqualTo("CONTENT_TYPE"); ``` where CONTENT_TYPE are [standards MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) -``` - await expect({ - bucketName: "BUCKET_NAME", - objectName: "FILE NAME", - }).toHaveContentEqualTo("CONTENT"); +```ts +await expect({ + bucketName: "BUCKET_NAME", + objectName: "FILE NAME", +}).toHaveContentEqualTo("CONTENT"); ``` ### Step Functions Note: these assertions also require "await" -``` - await expect("STATE_MACHINE_NAME").toHaveCompletedExecutionWithStatus("STATUS"); +```ts +await expect("STATE_MACHINE_NAME").toHaveCompletedExecutionWithStatus("STATUS"); ``` ## Helpers @@ -84,9 +84,9 @@ Note: these assertions also require "await" AWSClient - An AWS client with credentials set up -``` -getStackResources(stackName) - get information about a stack -getOptions() - get options for making requests to AWS +```ts +getStackResources(stackName); // get information about a stack +getOptions(); // get options for making requests to AWS ``` ### EventBridge @@ -95,17 +95,18 @@ An interface to the deployed EventBridge, allowing events to be injected and int #### Static -``` - EventBridge.build(busName) - create a EventBridge instance to allow events to be injected and intercepted +```ts +EventBridge.build(busName); // create a EventBridge instance to allow events to be injected and intercepted ``` #### Instance -``` - eventBridge.publishEvent(source, detailType, detail) - publish an event to the bus - eventBridge.getEvents() - get the events that have been sent to the bus - eventBridge.clear() - clear old messages - eventBridge.destroy() - remove infastructure used to track events +```ts +eventBridge.publishEvent(source, detailType, detail); // publish an event to the bus +eventBridge.getEvent(); // get the last event that has been sent to the bus +eventBridge.getAllEvents(); // get all the events that have been sent to the bus +eventBridge.clear(); // clear old messages +eventBridge.destroy(); // remove infastructure used to track events ``` ### Step Functions @@ -114,18 +115,26 @@ An interface to a deployed Step Function, with a function to execute a Step Func #### Static -``` - StepFunctions.build() // create a Step Functions Client for executing existing state machines +```ts +StepFunctions.build(); // create a Step Functions Client for executing existing state machines ``` #### Instance -``` - stepFunctions.runExecution(stateMachineName, input) // executes state machine until completion +```ts +stepFunctions.runExecution(stateMachineName, input); // executes state machine until completion ``` ## Running with `jest` +### Setup + +We recommend that you differentiate your unit tests from your integration tests by using two distinct Jest configurations: `jest.config.json` and `jest.integration.config.json`. In particular, in the configuration file of the integration tests, it is imperative to exclude any mocked AWS services by using: + +```json +modulePathIgnorePatterns: ["__mocks__"]; +``` + ### Arguments - When running tests with `jest` using `sls-test-tools` matchers there are certain parameters needed for `sls-test-tools` to make assertions. @@ -143,7 +152,7 @@ An interface to a deployed Step Function, with a function to execute a Step Func - To avoid issues we recommend `--runInBand` -``` +```ts import { AWSClient, EventBridge } from "sls-test-tools"; const lambda = new AWSClient.Lambda() @@ -173,7 +182,7 @@ describe("Integration Testing Event Bridge", () => { }; await lambda.invoke(params).promise(); - const eventBridgeEvents = await eventBridge.getEvents() + const eventBridgeEvents = await eventBridge.getEvent() expect(eventBridgeEvents).toHaveEvent(); expect(eventBridgeEvents).toHaveEventWithSource("order.created"); }); diff --git a/src/helpers/eventBridge.ts b/src/helpers/eventBridge.ts index 830e897..ffe0911 100644 --- a/src/helpers/eventBridge.ts +++ b/src/helpers/eventBridge.ts @@ -4,6 +4,12 @@ import { PromiseResult } from "aws-sdk/lib/request"; import { AWSClient, region } from "./general"; import { removeUndefinedMessages } from "./utils/removeUndefinedMessages"; +type EventBridgeMessage = { Body?: string }; + +type EventBridgeEvents = { + Messages?: EventBridgeMessage[]; +}; + export default class EventBridge { QueueUrl: string | undefined; eventBridgeClient: AWSEventBridge | undefined; @@ -122,12 +128,12 @@ export default class EventBridge { }) .promise(); - await this.getEvents(); // need to clear this manual published event from the SQS observer queue. + await this.getEvent(); // need to clear this manual published event from the SQS observer queue. return result; } - async getEvents(): Promise { + async getEvent(): Promise { if (this.QueueUrl === undefined) { throw new Error("QueueUrl is undefined"); } @@ -162,6 +168,30 @@ export default class EventBridge { return result; } + async getAllEvents(): Promise { + let allEventsFound = false; + const allEventBridgeEvents: EventBridgeEvents = {}; + + while (!allEventsFound) { + try { + const lastEventBridgeEvents = await this.getEvent(); + + if (!lastEventBridgeEvents || !lastEventBridgeEvents.Messages) { + return allEventBridgeEvents; + } + + allEventBridgeEvents.Messages = [ + ...(allEventBridgeEvents.Messages ?? []), + ...lastEventBridgeEvents.Messages, + ]; + } catch (e) { + allEventsFound = true; + } + } + + return allEventBridgeEvents; + } + async clear(): Promise { if (this.sqsClient === undefined) { throw new Error(