Skip to content

Commit

Permalink
feat: add getAllEvents method to EventBridge class
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbeggi committed Feb 4, 2022
1 parent fab9ab0 commit d05a205
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
67 changes: 38 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,7 +84,7 @@ Note: these assertions also require "await"

AWSClient - An AWS client with credentials set up

```
```ts
getStackResources(stackName) - get information about a stack
getOptions() - get options for making requests to AWS
```
Expand All @@ -95,15 +95,16 @@ An interface to the deployed EventBridge, allowing events to be injected and int

#### Static

```
```ts
EventBridge.build(busName) - create a EventBridge instance to allow events to be injected and intercepted
```

#### Instance

```
```ts
eventBridge.publishEvent(source, detailType, detail) - publish an event to the bus
eventBridge.getEvents() - get the events that have been sent 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
```
Expand All @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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");
});
Expand Down
34 changes: 32 additions & 2 deletions src/helpers/eventBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SQS.ReceiveMessageResult | undefined> {
async getEvent(): Promise<SQS.ReceiveMessageResult | undefined> {
if (this.QueueUrl === undefined) {
throw new Error("QueueUrl is undefined");
}
Expand Down Expand Up @@ -162,6 +168,30 @@ export default class EventBridge {
return result;
}

async getAllEvents(): Promise<EventBridgeEvents> {
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<any> {
if (this.sqsClient === undefined) {
throw new Error(
Expand Down

0 comments on commit d05a205

Please sign in to comment.