Skip to content

Commit 2db8852

Browse files
vladgolubevSimenB
authored andcommitted
Add DynamoDB usage example (#8319)
* docs: add dynamodb page to website sidebar * docs: add DynamoDB usage tutorial * docs: mention new dynamodb guide in CHANGELOG.md * style: apply prettier style suggestions
1 parent ac8c345 commit 2db8852

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- `[jest-cli]` Update `--forceExit` and "did not exit for one second" message colors ([#8329](https://github.com/facebook/jest/pull/8329))
77
- `[expect]` Improve report when matcher fails, part 16 ([#8306](https://github.com/facebook/jest/pull/8306))
88
- `[jest-runner]` Pass docblock pragmas to TestEnvironment constructor ([#8320](https://github.com/facebook/jest/pull/8320))
9+
- `[docs]` Add DynamoDB guide ([#8319](https://github.com/facebook/jest/pull/8319))
910

1011
### Fixes
1112

docs/DynamoDB.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: dynamodb
3+
title: Using with DynamoDB
4+
---
5+
6+
With the [Global Setup/Teardown](Configuration.md#globalsetup-string) and [Async Test Environment](Configuration.md#testenvironment-string) APIs, Jest can work smoothly with [DynamoDB](https://aws.amazon.com/dynamodb/).
7+
8+
## Use jest-dynamodb Preset
9+
10+
[Jest DynamoDB](https://github.com/shelfio/jest-dynamodb) provides all required configuration to run your tests using DynamoDB.
11+
12+
1. First install `@shelf/jest-dynamodb`
13+
14+
```
15+
yarn add @shelf/jest-dynamodb --dev
16+
```
17+
18+
2. Specify preset in your Jest configuration:
19+
20+
```json
21+
{
22+
"preset": "@shelf/jest-dynamodb"
23+
}
24+
```
25+
26+
3. Create `jest-dynamodb-config.js` and define DynamoDB tables
27+
28+
See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property)
29+
30+
```js
31+
module.exports = {
32+
tables: [
33+
{
34+
TableName: `files`,
35+
KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],
36+
AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],
37+
ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
38+
},
39+
// etc
40+
],
41+
};
42+
```
43+
44+
4. Configure DynamoDB client
45+
46+
```js
47+
const {DocumentClient} = require('aws-sdk/clients/dynamodb');
48+
49+
const isTest = process.env.JEST_WORKER_ID;
50+
const config = {
51+
convertEmptyValues: true,
52+
...(isTest && {
53+
endpoint: 'localhost:8000',
54+
sslEnabled: false,
55+
region: 'local-env',
56+
}),
57+
};
58+
59+
const ddb = new DocumentClient(config);
60+
```
61+
62+
5. Write tests
63+
64+
```js
65+
it('should insert item into table', async () => {
66+
await ddb
67+
.put({TableName: 'files', Item: {id: '1', hello: 'world'}})
68+
.promise();
69+
70+
const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise();
71+
72+
expect(Item).toEqual({
73+
id: '1',
74+
hello: 'world',
75+
});
76+
});
77+
```
78+
79+
There's no need to load any dependencies.
80+
81+
See [documentation](https://github.com/shelfio/jest-dynamodb) for details.

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"webpack",
2121
"puppeteer",
2222
"mongodb",
23+
"dynamodb",
2324
"tutorial-jquery",
2425
"watch-plugins",
2526
"migration-guide",

0 commit comments

Comments
 (0)