From bb3f1729b5ed5dbebdcfcb7aac3d527cb0198db6 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:07:40 +0300 Subject: [PATCH 1/4] docs: add dynamodb page to website sidebar --- website/sidebars.json | 1 + 1 file changed, 1 insertion(+) diff --git a/website/sidebars.json b/website/sidebars.json index c8fb58c869e7..de65d7d26007 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -20,6 +20,7 @@ "webpack", "puppeteer", "mongodb", + "dynamodb", "tutorial-jquery", "watch-plugins", "migration-guide", From 3f4ca84bca70ed51fed50bb7559e70091d8dddd6 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:11:40 +0300 Subject: [PATCH 2/4] docs: add DynamoDB usage tutorial --- docs/DynamoDB.md | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 docs/DynamoDB.md diff --git a/docs/DynamoDB.md b/docs/DynamoDB.md new file mode 100644 index 000000000000..8faf18dd646a --- /dev/null +++ b/docs/DynamoDB.md @@ -0,0 +1,75 @@ +--- +id: dynamodb +title: Using with DynamoDB +--- + +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/). + +## Use jest-dynamodb Preset + +[Jest DynamoDB](https://github.com/shelfio/jest-dynamodb) provides all required configuration to run your tests using DynamoDB. + +1. First install `@shelf/jest-dynamodb` + +``` +yarn add @shelf/jest-dynamodb --dev +``` + +2. Specify preset in your Jest configuration: + +```json +{ + "preset": "@shelf/jest-dynamodb" +} +``` + +3. Create `jest-dynamodb-config.js` and define DynamoDB tables + +See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property) + +```js +module.exports = { + tables: [ + { + TableName: `files`, + KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}], + AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}], + ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1} + } + // etc + ] +}; +``` + +4. Configure DynamoDB client + +```js +const {DocumentClient} = require('aws-sdk/clients/dynamodb'); + +const isTest = process.env.JEST_WORKER_ID; +const config = { + convertEmptyValues: true, + ...(isTest && {endpoint: 'localhost:8000', sslEnabled: false, region: 'local-env'}) +}; + +const ddb = new DocumentClient(config); +``` + +5. Write tests + +```js +it('should insert item into table', async () => { + await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}}).promise(); + + const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise(); + + expect(Item).toEqual({ + id: '1', + hello: 'world' + }); +}); +``` + +There's no need to load any dependencies. + +See [documentation](https://github.com/shelfio/jest-dynamodb) for details. From 4659b757b5b67cbda47bbce222819b00f32791e7 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:15:55 +0300 Subject: [PATCH 3/4] docs: mention new dynamodb guide in CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e23216d3f0a3..a543b0fdccb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[expect]` Improve report when matcher fails, part 15 ([#8281](https://github.com/facebook/jest/pull/8281)) +- `[docs]` Add DynamoDB guide ([#8319](https://github.com/facebook/jest/pull/8319)) ### Fixes From 9b02b4c8641ceace4d268a53e87c0d134ee7d2a8 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:25:33 +0300 Subject: [PATCH 4/4] style: apply prettier style suggestions --- docs/DynamoDB.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/DynamoDB.md b/docs/DynamoDB.md index 8faf18dd646a..ed3d36febae9 100644 --- a/docs/DynamoDB.md +++ b/docs/DynamoDB.md @@ -34,10 +34,10 @@ module.exports = { TableName: `files`, KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}], AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}], - ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1} - } + ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1}, + }, // etc - ] + ], }; ``` @@ -49,7 +49,11 @@ const {DocumentClient} = require('aws-sdk/clients/dynamodb'); const isTest = process.env.JEST_WORKER_ID; const config = { convertEmptyValues: true, - ...(isTest && {endpoint: 'localhost:8000', sslEnabled: false, region: 'local-env'}) + ...(isTest && { + endpoint: 'localhost:8000', + sslEnabled: false, + region: 'local-env', + }), }; const ddb = new DocumentClient(config); @@ -59,13 +63,15 @@ const ddb = new DocumentClient(config); ```js it('should insert item into table', async () => { - await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}}).promise(); + await ddb + .put({TableName: 'files', Item: {id: '1', hello: 'world'}}) + .promise(); const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise(); expect(Item).toEqual({ id: '1', - hello: 'world' + hello: 'world', }); }); ```