-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsetup.js
73 lines (59 loc) · 2.07 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const {resolve} = require('path');
const cwd = require('cwd');
const {DynamoDB} = require('@aws-sdk/client-dynamodb');
const DynamoDbLocal = require('dynamodb-local');
const debug = require('debug')('jest-dynamodb');
const waitForLocalhost = require('./wait-for-localhost');
const DEFAULT_PORT = 8000;
const DEFAULT_OPTIONS = ['-sharedDb'];
module.exports = async function () {
const config = require(process.env.JEST_DYNAMODB_CONFIG ||
resolve(cwd(), 'jest-dynamodb-config.js'));
debug('config:', config);
const {
tables: newTables,
clientConfig,
installerConfig,
port: port = DEFAULT_PORT,
options: options = DEFAULT_OPTIONS,
} = typeof config === 'function' ? await config() : config;
const dynamoDB = new DynamoDB({
endpoint: `http://localhost:${port}`,
tls: false,
region: 'local-env',
credentials: {
accessKeyId: 'fakeMyKeyId',
secretAccessKey: 'fakeSecretAccessKey',
},
...clientConfig,
});
global.__DYNAMODB_CLIENT__ = dynamoDB;
try {
const promises = [dynamoDB.listTables({})];
if (!global.__DYNAMODB__) {
promises.push(waitForLocalhost(port));
}
const [{TableNames: tableNames}] = await Promise.all(promises);
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);
}
if (!global.__DYNAMODB__) {
debug('spinning up a local ddb instance');
global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options);
debug(`dynamodb-local started on port ${port}`);
await waitForLocalhost(port);
}
}
debug(`dynamodb-local is ready on port ${port}`);
await createTables(dynamoDB, newTables);
};
function createTables(dynamoDB, tables) {
return Promise.all(tables.map(table => dynamoDB.createTable(table)));
}
function deleteTables(dynamoDB, tableNames) {
return Promise.all(tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName})));
}