-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from shelfio/feature/typescript-support
Added typescript support!
- Loading branch information
Showing
17 changed files
with
273 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib/ | ||
renovate.json | ||
tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.idea/ | ||
coverage/ | ||
node_modules/ | ||
lib/ | ||
temp | ||
yarn.lock | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
const {resolve} = require('path'); | ||
const preset = require('./lib'); | ||
|
||
module.exports = { | ||
globalSetup: resolve(__dirname, './setup.js'), | ||
globalTeardown: resolve(__dirname, './teardown.js'), | ||
testEnvironment: resolve(__dirname, './environment.js'), | ||
}; | ||
module.exports = preset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {resolve} from 'path'; | ||
|
||
export * from './types'; | ||
|
||
export default { | ||
globalSetup: resolve(__dirname, './setup.js'), | ||
globalTeardown: resolve(__dirname, './teardown.js'), | ||
testEnvironment: resolve(__dirname, './environment.js'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import type {ListTablesCommandOutput} from '@aws-sdk/client-dynamodb/dist-types/commands/ListTablesCommand'; | ||
import type {argValues} from 'dynamodb-local'; | ||
import DynamoDbLocal from 'dynamodb-local'; | ||
import {resolve} from 'path'; | ||
import cwd from 'cwd'; | ||
import {DynamoDB} from '@aws-sdk/client-dynamodb'; | ||
import type {CreateTableCommandInput} from '@aws-sdk/client-dynamodb'; | ||
import type {Config} from './types'; | ||
import waitForLocalhost from './utils/wait-for-localhost'; | ||
|
||
const debug = require('debug')('jest-dynamodb'); | ||
|
||
const DEFAULT_PORT = 8000; | ||
const DEFAULT_HOST = 'localhost'; | ||
const DEFAULT_OPTIONS: argValues[] = ['-sharedDb']; | ||
|
||
export default async function () { | ||
const { | ||
tables: newTables, | ||
clientConfig, | ||
installerConfig, | ||
port: port = DEFAULT_PORT, | ||
hostname: hostname = DEFAULT_HOST, | ||
options: options = DEFAULT_OPTIONS, | ||
} = await getConfig(); | ||
|
||
const dynamoDB = new DynamoDB({ | ||
endpoint: `http://${hostname}:${port}`, | ||
tls: false, | ||
region: 'local-env', | ||
credentials: { | ||
accessKeyId: 'fakeMyKeyId', | ||
secretAccessKey: 'fakeSecretAccessKey', | ||
}, | ||
...clientConfig, | ||
}); | ||
|
||
global.__DYNAMODB_CLIENT__ = dynamoDB; | ||
|
||
try { | ||
const promises: (Promise<ListTablesCommandOutput> | Promise<void>)[] = [ | ||
dynamoDB.listTables({}), | ||
]; | ||
|
||
if (!global.__DYNAMODB__) { | ||
promises.push(waitForLocalhost(port, hostname)); | ||
} | ||
|
||
const [TablesList] = await Promise.all(promises); | ||
const tableNames = TablesList?.TableNames; | ||
|
||
if (tableNames) { | ||
await deleteTables(dynamoDB, tableNames); | ||
} | ||
} 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, hostname); | ||
} | ||
} | ||
debug(`dynamodb-local is ready on port ${port}`); | ||
|
||
await createTables(dynamoDB, newTables); | ||
} | ||
|
||
function createTables(dynamoDB: DynamoDB, tables: CreateTableCommandInput[]) { | ||
return Promise.all(tables.map(table => dynamoDB.createTable(table))); | ||
} | ||
|
||
function deleteTables(dynamoDB: DynamoDB, tableNames: string[]) { | ||
return Promise.all(tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName}))); | ||
} | ||
|
||
async function getConfig(): Promise<Config> { | ||
const path = process.env.JEST_DYNAMODB_CONFIG || resolve(cwd(), 'jest-dynamodb-config.js'); | ||
const config = require(path); | ||
debug('config:', config); | ||
|
||
return typeof config === 'function' ? await config() : config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.