11const { resolve} = require ( 'path' ) ;
22const cwd = require ( 'cwd' ) ;
3- const DynamoDB = require ( 'aws-sdk/clients/ dynamodb' ) ;
3+ const { DynamoDB} = require ( '@ aws-sdk/client- dynamodb' ) ;
44const DynamoDbLocal = require ( 'dynamodb-local' ) ;
55const debug = require ( 'debug' ) ( 'jest-dynamodb' ) ;
6+ const waitForLocalhost = require ( './wait-for-localhost' ) ;
67
78// aws-sdk requires access and secret key to be able to call DDB
89process . env . AWS_ACCESS_KEY_ID = 'access-key' ;
@@ -22,17 +23,21 @@ module.exports = async function () {
2223 } = typeof config === 'function' ? await config ( ) : config ;
2324
2425 const dynamoDB = new DynamoDB ( {
25- endpoint : `localhost:${ port } ` ,
26- sslEnabled : false ,
26+ endpoint : `http:// localhost:${ port } ` ,
27+ tls : false ,
2728 region : 'local-env' ,
29+ credentials : {
30+ accessKeyId : 'fakeMyKeyId' ,
31+ secretAccessKey : 'fakeSecretAccessKey'
32+ } ,
2833 ...clientConfig
2934 } ) ;
3035
3136 global . __DYNAMODB_CLIENT__ = dynamoDB ;
3237
3338 try {
3439 const { TableNames : tableNames } = await Promise . race ( [
35- dynamoDB . listTables ( ) . promise ( ) ,
40+ dynamoDB . listTables ( { } ) ,
3641 new Promise ( resolve => setTimeout ( resolve , 1000 ) )
3742 ] ) ;
3843 await deleteTables ( dynamoDB , tableNames ) ; // cleanup leftovers
@@ -45,15 +50,19 @@ module.exports = async function () {
4550 }
4651
4752 if ( ! global . __DYNAMODB__ ) {
53+ debug ( 'spinning up a local ddb instance' ) ;
54+
4855 global . __DYNAMODB__ = await DynamoDbLocal . launch ( port , null , options ) ;
56+
57+ await waitForLocalhost ( { port : DEFAULT_PORT , useGet : true , path : '/' } ) ;
4958 }
5059 }
5160
5261 await createTables ( dynamoDB , newTables ) ;
5362} ;
5463
5564async function createTables ( dynamoDB , tables ) {
56- return Promise . all ( tables . map ( table => dynamoDB . createTable ( table ) . promise ( ) ) ) ;
65+ return Promise . all ( tables . map ( table => dynamoDB . createTable ( table ) ) ) ;
5766}
5867
5968async function deleteTables ( dynamoDB , tableNames ) {
0 commit comments