From c8c80364c4c3b2f592f2f088bffe26c3f27f8518 Mon Sep 17 00:00:00 2001 From: Christopher Kruse Date: Tue, 23 Feb 2021 08:15:15 -0800 Subject: [PATCH] Allow jest watch / watchAll Uses an existence check at startup to avoid attempting to re-instantiate the dynamodb-local server if already defined. Also uses jest's [globalConfig argument at teardown](https://jestjs.io/docs/en/configuration#globalteardown-string) to detect watch state and avoid tearing down the server prematurely. --- setup.js | 4 +++- teardown.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/setup.js b/setup.js index 1e8ea8e..e1106cb 100644 --- a/setup.js +++ b/setup.js @@ -41,7 +41,9 @@ module.exports = async function () { DynamoDbLocal.configureInstaller(installerConfig); } - global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options); + if(!global.__DYNAMODB__) { + global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options); + } } await createTables(dynamoDB, newTables); diff --git a/teardown.js b/teardown.js index c956602..ca46414 100644 --- a/teardown.js +++ b/teardown.js @@ -1,12 +1,15 @@ const DynamoDbLocal = require('dynamodb-local'); const debug = require('debug')('jest-dynamodb'); -module.exports = async function () { +module.exports = async function (jestArgs) { // eslint-disable-next-line no-console debug('Teardown DynamoDB'); if (global.__DYNAMODB__) { - await DynamoDbLocal.stopChild(global.__DYNAMODB__); + const watching = jestArgs.watch || jestArgs.watchAll; + if (!watching) { + await DynamoDbLocal.stopChild(global.__DYNAMODB__); + } } else { const dynamoDB = global.__DYNAMODB_CLIENT__; const {TableNames: tableNames} = await dynamoDB.listTables().promise();