Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
agwhi committed May 14, 2021
1 parent 6c569b2 commit 38efe5b
Showing 1 changed file with 74 additions and 50 deletions.
124 changes: 74 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,119 @@
<div align="center">
<h1>sls-test-tools</h1>

Custom Jest Assertions for Serverless Projects

Custom Jest Assertions for Serverless Projects
</div>

<hr />

`sls-test-tools` provides a range of utilities, setup, teardown and assertions to make it easier to write effective and high quality integration tests for Serverless Architectures on AWS using `TypeScript` & `jest`.


## Installation

With npm:

```sh
npm install --save-dev sls-test-tools
```

With yarn:

```sh
yarn add -D sls-test-tools
```

## Maintenance

sls-test-tools is currently being actively maintained. If you find a problem with the tool, let us know and we'll solve it as quickly as possible.
## Usage

- Copy the `test-lib.js` file, along with the `package.json` to your project.
## Assertions:

## Assertion Examples:
### EventBridge

## Running with `jest`
```
expect(eventBridgeEvents).toHaveEvent();

expect(eventBridgeEvents).toHaveEventWithSource("order.created");
```

## Helpers

### General

```
AWSClient - An AWS client with credentials set up

getStackResources(stackName) - get information about a stack
getOptions() - get options for making requests to AWS
```

### EventBridge

#### Static

```
EventBridge.build(busName) - create a EventBridge instance with helper functionns
```

- `yarn jest '-profile=default' '-stack=my-service-dev' --runInBand`
#### 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
```

## Running with `jest`

- `yarn jest '-profile=default' '-stack=my-service-dev' --runInBand`

```
async function callEndPoint(url, data, cookieToken) {
const requestOptions = {
...(await getOptions()),
data: data,
url,
};
if (cookieToken) {
requestOptions.headers.cookie = cookieToken;
} else {
requestOptions.headers.cookie = null;
}
return axios(requestOptions);
}

describe("Integration Testing: My Service", () => {

describe("Integration Testing Event Bridge", () => {
beforeAll(async () => {
const stacks = await getStackResources(stackName);
const endpointOutput = stacks.Stacks[0].Outputs.find(
(output) => output.OutputKey === "ServiceEndpoint"
).OutputValue;
const endpointMatches = endpointOutput.match(
/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/
);
endpoint = endpointMatches[0];
axios.defaults.validateStatus = () => true; // prevent error codes from throwing errors
eventBridge = await EventBridge.build("event-bridge")
});

it("creates API Gateway Endpoint and URLs", async () => {
expect(endpoint).toBeDefined();
afterAll(async () => {
await eventBridge.destroy()
});

it("returns 403 forbidden without valid api key", async () => {
expect(endpoint).toBeDefined();
const resp = await axios.post(endpoint);
expect(resp.status).toBe(403);
});
it("lambda publish event correctly", async () => {
const event = {
body: JSON.stringify({
filename: filename,
}),
};

it("returns a 200 with a correct token and object object", async () => {
expect(endpoint).toBeDefined();
const resp = await callEndPoint(endpoint, data);
expect(resp.status).toBe(200);
expect(resp.data.success).toBe(true);
// Invoke Lambda Function
const params = {
FunctionName: "event-bridge-example-dev-service1",
Payload: JSON.stringify(event),
};
await lambda.invoke(params).promise();

const eventBridgeEvents = await eventBridge.getEvents()
expect(eventBridgeEvents).toHaveEvent();
expect(eventBridgeEvents).toHaveEventWithSource("order.created");
});

it("populates the correct data in the bucket", async () => {
it("pdf is saved when an order is created", async () => {
await eventBridge
.publishEvent("order.created", "example", JSON.stringify({ filename: filename }));

await sleep(5000); // wait 5 seconds to allow event to pass

const params = {
Bucket: "bucket-name",
Key: "key",
Bucket: "example-dev-thumbnails-bucket",
Key: filename,
};

// Assert that file was added to the S3 bucket
const obj = await s3.getObject(params).promise();
expect(obj.ContentType).toBe("text/html");
expect(obj.Body.toString()).toMatchSnapshot();
});
expect(obj.ContentType).toBe("application/pdf");
});

```

Expand All @@ -104,7 +128,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="https://medium.com/serverless-transformation"><img src="https://avatars1.githubusercontent.com/u/11080984?v=4" width="100px;" alt=""/><br /><sub><b>Ben Ellerby</b></sub></a><br /><a href="https://github.com/BenEllerby/sls-test-tools/commits?author=BenEllerby" title="Code">💻</a> <a href="#content-BenEllerby" title="Content">🖋</a> <a href="https://github.com/BenEllerby/sls-test-tools/commits?author=BenEllerby" title="Documentation">📖</a> <a href="#ideas-BenEllerby" title="Ideas, Planning, & Feedback">🤔</a> <a href="#design-BenEllerby" title="Design">🎨</a> <a href="#talk-BenEllerby" title="Talks">📢</a> <a href="https://github.com/BenEllerby/sls-test-tools/pulls?q=is%3Apr+reviewed-by%3ABenEllerby" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://medium.com/serverless-transformation"><img src="https://avatars.githubusercontent.com/hamilton-s" width="100px;" alt=""/><br /><sub><b>Sarah Hamilton</b></sub></a><br /><a href="https://github.com/BenEllerby/sls-test-tools/commits?author=hamilton-s" title="Code">💻</a> <a href="#content-hamilton-s" title="Content">🖋</a> <a href="https://github.com/BenEllerby/sls-test-tools/commits?author=hamilton-s" title="Documentation">📖</a> <a href="#ideas-hamilton-s" title="Ideas, Planning, & Feedback">🤔</a></td>

<td align="center"><a href="https://github.com/agwhi"><img src="https://avatars.githubusercontent.com/agwhi" width="100px;" alt=""/><br /><sub><b>Alex White</b></sub></a><br /><a href="https://github.com/BenEllerby/sls-test-tools/commits?author=agwhi" title="Code">💻</a><a href="https://github.com/BenEllerby/sls-test-tools/commits?author=agwhi" title="Documentation">📖</a></td>
</tr>
</table>

Expand Down

0 comments on commit 38efe5b

Please sign in to comment.