Skip to content

Commit

Permalink
feat: use running server if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hirochachacha committed Feb 6, 2020
1 parent 4ef4564 commit 8bd3138
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
31 changes: 31 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,35 @@ jobs:
- yarn.lock
key: v1-dependencies-{{ checksum "package.json" }}

test_without_db:
docker:
- image: circleci/node:10-browsers

working_directory: ~/repo

steps:
- checkout
- run: yarn test

test_with_db:
docker:
- image: circleci/node:10-browsers
- image: circleci/dynamodb:oracle

working_directory: ~/repo

steps:
- checkout
- run: yarn test

workflows:
version: 2
build_and_test:
jobs:
- build
- test_with_db:
requires:
- build
- test_without_db:
requires:
- build
38 changes: 30 additions & 8 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {resolve} = require('path');
const cwd = require('cwd');
const DynamoDB = require('aws-sdk/clients/dynamodb');
const DynamoDbLocal = require('dynamodb-local');
const debug = require('debug')('jest-dynamodb');

// aws-sdk requires access and secret key to be able to call DDB
process.env.AWS_ACCESS_KEY_ID = 'access-key';
Expand All @@ -12,12 +13,13 @@ const DEFAULT_OPTIONS = ['-sharedDb'];

module.exports = async function() {
const config = require(resolve(cwd(), 'jest-dynamodb-config.js'));
const {tables, port: port = DEFAULT_PORT, options: options = DEFAULT_OPTIONS, clientConfig, installerConfig} =
typeof config === 'function' ? await config() : config;

if (installerConfig) {
DynamoDbLocal.configureInstaller(installerConfig);
}
const {
tables: newTables,
clientConfig,
installerConfig,
port: port = DEFAULT_PORT,
options: options = DEFAULT_OPTIONS
} = typeof config === 'function' ? await config() : config;

const dynamoDB = new DynamoDB({
endpoint: `localhost:${port}`,
Expand All @@ -26,11 +28,31 @@ module.exports = async function() {
...clientConfig
});

global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options);
global.__DYNAMODB_CLIENT__ = dynamoDB;

try {
const {TableNames: tableNames} = await dynamoDB.listTables().promise();
await deleteTables(dynamoDB, tableNames); // cleanup leftovers
} catch (err) {
// eslint-disable-next-line no-console
debug(`fallback to launch DB due to ${err}`);

if (installerConfig) {
DynamoDbLocal.configureInstaller(installerConfig);
}

await createTables(dynamoDB, tables);
global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options);
}

await createTables(dynamoDB, newTables);
};

async function createTables(dynamoDB, tables) {
return Promise.all(tables.map(table => dynamoDB.createTable(table).promise()));
}

async function deleteTables(dynamoDB, tableNames) {
return Promise.all(
tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName}).promise())
);
}
11 changes: 10 additions & 1 deletion teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@ const debug = require('debug')('jest-dynamodb');
module.exports = async function() {
// eslint-disable-next-line no-console
debug('Teardown DynamoDB');
await DynamoDbLocal.stopChild(global.__DYNAMODB__);

if (global.__DYNAMODB__) {
await DynamoDbLocal.stopChild(global.__DYNAMODB__);
} else {
const dynamoDB = global.__DYNAMODB_CLIENT__;
const {TableNames: tableNames} = await dynamoDB.listTables().promise();
await Promise.all(
tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName}).promise())
);
}
};

0 comments on commit 8bd3138

Please sign in to comment.