Skip to content

Commit 8df76a8

Browse files
authored
Merge pull request #27 from erezrokah/chai_support
chai support
2 parents f65a2c3 + b481c85 commit 8df76a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2217
-424
lines changed

README.md

Lines changed: 23 additions & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# jest-e2e-serverless
1+
# AWS Testing Library
22

33
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4-
[![CircleCI](https://circleci.com/gh/erezrokah/jest-e2e-serverless.svg?style=svg)](https://circleci.com/gh/erezrokah/jest-e2e-serverless)
4+
[![CircleCI](https://circleci.com/gh/erezrokah/aws-testing-library.svg?style=svg)](https://circleci.com/gh/erezrokah/aws-testing-library)
55

6-
> Note: This library is still at POC level, if you're missing any capability please open an issue :)
6+
> Note: If you're missing any capability please open an issue/feature request :)
77
88
## Prerequisites
99

@@ -20,250 +20,36 @@ If you plan to use the [deploy](#deploy) utility function please install and con
2020
Install with [yarn](https://github.com/yarnpkg/yarn)
2121

2222
```bash
23-
yarn add jest-e2e-serverless --dev
23+
yarn add aws-testing-library --dev
2424
```
2525

2626
or [npm](https://www.npmjs.com/)
2727

2828
```bash
29-
npm install jest-e2e-serverless --save-dev
29+
npm install aws-testing-library --save-dev
3030
```
3131

32-
### Setup
32+
## Setup
3333

34-
The simplest setup is to use jest's `setupTestFrameworkScriptFile` config.
34+
- [Chai](src/chai/README.md)
35+
- [Jest](src/jest/README.md)
3536

36-
Make sure your `package.json` includes the following:
37-
38-
```json
39-
// package.json
40-
"jest": {
41-
"setupTestFrameworkScriptFile": "./node_modules/jest-e2e-serverless/lib/index.js",
42-
},
43-
```
44-
45-
#### Usage with TypeScript
46-
47-
When using `jest-e2e-serverless` with [TypeScript](http://typescriptlang.org/) and [ts-jest](https://github.com/kulshekhar/ts-jest), you'll need to add a `setupFrameworks.ts` file to your app that explicitly imports `jest-e2e-serverless`, and point the `setupTestFrameworkScriptFile` field in your `package.json` file towards it:
48-
49-
```typescript
50-
// src/setupFrameworks.ts
51-
import 'jest-e2e-serverless';
52-
```
53-
54-
```json
55-
// package.json
56-
"jest": {
57-
"setupTestFrameworkScriptFile": "./src/setupFrameworks.ts",
58-
},
59-
```
60-
61-
### Assertions
62-
63-
> Notes
64-
>
65-
> - The matchers use `aws-sdk` under the hood, thus they are all asynchronous and require using `async/await`
66-
67-
- [toHaveItem()](#tohaveitem)
68-
- [toHaveObject()](#tohaveobject)
69-
- [toHaveLog()](#tohavelog)
70-
- [toBeAtState()](#tobeatstate)
71-
- [toHaveState()](#tohavestate)
72-
- [toReturnResponse()](#toreturnresponse)
73-
- [toHaveRecord()](#tohaverecord)
74-
- [toHaveMessage()](#tohavemessage)
75-
76-
#### `toHaveItem()`
77-
78-
Asserts existence/equality of a DynamoDb item
79-
80-
```js
81-
expect.assertions(1); // makes sure the assertion was called
82-
await expect({
83-
region: 'us-east-1',
84-
table: 'dynamo-db-table',
85-
timeout: 0 /* optional (defaults to 2500) */,
86-
pollEvery: 0 /* optional (defaults to 500) */,
87-
}).toHaveItem(
88-
{
89-
id: 'itemId',
90-
} /* dynamoDb key object (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#getItem-property) */,
91-
{
92-
id: 'itemId',
93-
createdAt: new Date().getTime(),
94-
text: 'some content',
95-
} /* optional, if exists will check equality in addition to existence */,
96-
true /* optional, strict mode comparison, defaults to true */,
97-
);
98-
```
99-
100-
[See complete example](https://github.com/erezrokah/serverless-monorepo-app/blob/master/services/db-service/e2e/db.test.ts)
101-
102-
#### `toHaveObject()`
103-
104-
Asserts existence/equality of a S3 object
105-
106-
```js
107-
expect.assertions(1); // makes sure the assertion was called
108-
await expect({
109-
region: 'us-east-1',
110-
bucket: 's3-bucket',
111-
timeout: 0 /* optional (defaults to 2500) */,
112-
pollEvery: 0 /* optional (defaults to 500) */,
113-
}).toHaveObject(
114-
'someFileInTheBucket' /* a string representing the object key in the bucket */,
115-
Buffer.from(
116-
'a buffer of the file content',
117-
) /* optional, if exists will check equality in addition to existence */,
118-
);
119-
```
120-
121-
[See complete example](https://github.com/erezrokah/serverless-monorepo-app/blob/master/services/file-service/e2e/handler.test.ts)
122-
123-
#### `toHaveLog()`
124-
125-
Asserts log message of a lambda function
126-
127-
```js
128-
expect.assertions(1); // makes sure the assertion was called
129-
await expect({
130-
region: 'us-east-1',
131-
function: 'functionName',
132-
timeout: 0 /* optional (defaults to 2500) */,
133-
pollEvery: 0 /* optional (defaults to 500) */,
134-
}).toHaveLog(
135-
'some message written to log by the lambda' /* a pattern to match against log messages */,
136-
);
137-
```
138-
139-
[See complete example](https://github.com/erezrokah/hello-retail/blob/master/e2eTests/src/sendUserLogin.test.ts)
140-
141-
#### `toBeAtState()`
142-
143-
Asserts a state machine current state
144-
145-
```js
146-
expect.assertions(1); // makes sure the assertion was called
147-
await expect({
148-
pollEvery: 5000 /* optional (defaults to 500) */,
149-
region: 'us-east-1',
150-
stateMachineArn: 'stateMachineArn',
151-
timeout: 30 * 1000 /* optional (defaults to 2500) */,
152-
}).toBeAtState('ExpectedState');
153-
```
154-
155-
[See complete example](https://github.com/erezrokah/hello-retail/blob/master/e2eTests/src/newProduct.test.ts#L73)
156-
157-
#### `toHaveState()`
158-
159-
Asserts that a state machine has been at a state
160-
161-
```js
162-
expect.assertions(1); // makes sure the assertion was called
163-
await expect({
164-
pollEvery: 5000 /* optional (defaults to 500) */,
165-
region: 'us-east-1',
166-
stateMachineArn: 'stateMachineArn',
167-
timeout: 30 * 1000 /* optional (defaults to 2500) */,
168-
}).toHaveState('ExpectedState');
169-
```
170-
171-
[See complete example](https://github.com/erezrokah/hello-retail/blob/master/e2eTests/src/stateMachine.test.ts#L97)
172-
173-
#### `toReturnResponse()`
174-
175-
Asserts that an api returns a specific response
176-
177-
```js
178-
expect.assertions(1); // makes sure the assertion was called
179-
await expect({
180-
url: 'https://api-id.execute-api.us-east-1.amazonaws.com/dev/api/private',
181-
method: 'POST',
182-
params: { urlParam: 'value' } /* optional URL parameters */,
183-
data: { bodyParam: 'value' } /* optional body parameters */,
184-
headers: { Authorization: 'Bearer token_value' } /* optional headers */,
185-
}).toReturnResponse({
186-
data: {
187-
message: 'Unauthorized',
188-
},
189-
statusCode: 401,
190-
});
191-
```
192-
193-
[See complete example](https://github.com/erezrokah/serverless-monorepo-app/blob/master/services/api-service/e2e/privateEndpoint.test.ts#L8)
194-
195-
#### `toHaveRecord()`
196-
197-
Asserts existence/equality of a Kinesis record
198-
199-
```js
200-
expect.assertions(1); // makes sure the assertion was called
201-
await expect({
202-
region: 'us-east-1',
203-
stream: 'kinesis-stream',
204-
timeout: 0 /* optional (defaults to 10000) */,
205-
pollEvery: 0 /* optional (defaults to 500) */,
206-
}).toHaveRecord(
207-
item => item.id === 'someId' /* predicate to match with the stream data */,
208-
);
209-
```
210-
211-
[See complete example](https://github.com/erezrokah/serverless-monorepo-app/blob/master/services/kinesis-service/e2e/handler.test.ts)
212-
213-
#### `toHaveMessage()`
214-
215-
Asserts existence/equality of a message in an SQS queue
216-
217-
```js
218-
const {
219-
subscribeToTopic,
220-
unsubscribeFromTopic,
221-
} = require('jest-e2e-serverless/lib/utils/sqs');
222-
223-
let [subscriptionArn, queueUrl] = ['', ''];
224-
try {
225-
// create an SQS queue and subscribe to SNS topic
226-
({ subscriptionArn, queueUrl } = await subscribeToTopic(region, topicArn));
227-
228-
// run some code that will publish a message to the SNS topic
229-
someCodeThatResultsInPublishingAMessage();
230-
231-
expect.assertions(1); // makes sure the assertion was called
232-
await expect({
233-
region,
234-
queueUrl,
235-
timeout: 10000 /* optional (defaults to 2500) */,
236-
pollEvery: 2500 /* optional (defaults to 500) */,
237-
}).toHaveMessage(
238-
/* predicate to match with the messages in the queue */
239-
message =>
240-
message.Subject === 'Some Subject' && message.Message === 'Some Message',
241-
);
242-
} finally {
243-
// unsubscribe from SNS topic and delete SQS queue
244-
await unsubscribeFromTopic(region, subscriptionArn, queueUrl);
245-
}
246-
```
247-
248-
[See complete example](https://github.com/erezrokah/serverless-monitoring-app/blob/master/services/monitoring-service/e2e/checkEndpointStepFunction.test.ts)
249-
250-
### Utils
37+
## Utils
25138

25239
- [invoke()](#invoke)
25340
- [clearAllItems()](#clearallitems)
25441
- [writeItems()](#writeitems)
25542
- [clearAllObjects()](#clearallobjects)
25643
- [deleteAllLogs()](#deletealllogs)
25744
- [stopRunningExecutions()](#stoprunningexecutions)
258-
- [getResponse()](#getresponse)
25945
- [deploy()](#deploy)
26046

261-
#### `invoke()`
47+
### `invoke()`
26248

26349
Invokes a lambda function
26450

26551
```typescript
266-
const { invoke } = require('jest-e2e-serverless/lib/utils/lambda');
52+
const { invoke } = require('aws-testing-library/lib/utils/lambda');
26753

26854
const result = await invoke(
26955
'us-east-1',
@@ -274,34 +60,34 @@ const result = await invoke(
27460
);
27561
```
27662

277-
#### `clearAllItems()`
63+
### `clearAllItems()`
27864

27965
Clear all items in a DynamoDb table
28066

28167
```typescript
282-
const { clearAllItems } = require('jest-e2e-serverless/lib/utils/dynamoDb');
68+
const { clearAllItems } = require('aws-testing-library/lib/utils/dynamoDb');
28369

28470
await clearAllItems('us-east-1', 'dynamo-db-table');
28571
```
28672

287-
#### `writeItems()`
73+
### `writeItems()`
28874

28975
Write items to a DynamoDb table
29076

29177
```typescript
292-
const { writeItems } = require('jest-e2e-serverless/lib/utils/dynamoDb');
78+
const { writeItems } = require('aws-testing-library/lib/utils/dynamoDb');
29379

29480
const items = require('./seed.json');
29581

29682
await writeItems('us-east-1', 'dynamo-db-table', items);
29783
```
29884

299-
#### `clearAllObjects()`
85+
### `clearAllObjects()`
30086

30187
Clear all objects in a s3 bucket
30288

30389
```typescript
304-
const { clearAllObjects } = require('jest-e2e-serverless/lib/utils/s3');
90+
const { clearAllObjects } = require('aws-testing-library/lib/utils/s3');
30591

30692
await clearAllObjects(
30793
'us-east-1',
@@ -310,50 +96,34 @@ await clearAllObjects(
31096
);
31197
```
31298

313-
#### `deleteAllLogs()`
99+
### `deleteAllLogs()`
314100

315101
Clear all log streams for a lambda function
316102

317103
```typescript
318-
const { deleteAllLogs } = require('jest-e2e-serverless/lib/utils/cloudwatch');
104+
const { deleteAllLogs } = require('aws-testing-library/lib/utils/cloudwatch');
319105

320106
await deleteAllLogs('us-east-1', 'lambda-function-name');
321107
```
322108

323-
#### `stopRunningExecutions()`
109+
### `stopRunningExecutions()`
324110

325111
Stop all running executions for a state machine
326112

327113
```typescript
328114
const {
329115
stopRunningExecutions,
330-
} = require('jest-e2e-serverless/lib/utils/stepFunctions');
116+
} = require('aws-testing-library/lib/utils/stepFunctions');
331117

332118
await stopRunningExecutions('us-east-1', 'state-machine-arn');
333119
```
334120

335-
#### `getResponse()`
336-
337-
Send a request to an api and get a response
338-
339-
```typescript
340-
const { getResponse } = require('jest-e2e-serverless/lib/utils/api');
341-
342-
const result = await getResponse(
343-
url: 'https://api-id.execute-api.us-east-1.amazonaws.com/dev/api/private',
344-
method: 'POST',
345-
params: { urlParam: 'value' } /* optional URL parameters */,
346-
data: { bodyParam: 'value' } /* optional body parameters */,
347-
headers: { Authorization: 'Bearer token_value' } /* optional headers */,
348-
);
349-
```
350-
351-
#### `deploy()`
121+
### `deploy()`
352122

353123
Deploys the current service using [Serverless framework](https://serverless.com/)
354124

355125
```typescript
356-
const { deploy } = require('jest-e2e-serverless/lib/utils/serverless');
126+
const { deploy } = require('aws-testing-library/lib/utils/serverless');
357127

358128
await deploy('dev' /* optional - deployment stage */);
359129
```

0 commit comments

Comments
 (0)