Jest preset for DynamoDB local server
Jest preset to run DynamoDB Local
$ yarn add @shelf/jest-dynamodb --devMake sure java runtime available for running DynamoDBLocal.jar
jest.config.jsmodule.exports = {
preset: '@shelf/jest-dynamodb',
};jest-dynamodb-config.jsobject[]trueArray of createTable params.
numberfalsePort number. The default port number is 8000.
stringfalseHostname. The default hostname is localhost.
string[]falseAdditional arguments for dynamodb-local. The default value is ['-sharedDb'].
objectfalseConstructor params of DynamoDB client.
Type: {installPath?: string, downloadUrl?: string}
Required: false
installPath defines the location where dynamodb-local is installed or will be installed.
downloadUrl defines the url of dynamodb-local package.
The default value is defined at https://github.com/rynop/dynamodb-local/blob/2e6c1cb2edde4de0dc51a71c193c510b939d4352/index.js#L16-L19
You can set up tables as an object:
Whole list of config properties can be found here
/**
* @type {import('@shelf/jest-dynamodb/lib').Config}')}
*/
const config = {
tables: [
{
TableName: `files`,
KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],
AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],
ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
},
// etc
],
port: 8000,
};
module.exports = config;Or as an async function (particularly useful when resolving DynamoDB setup dynamically from serverless.yml):
…Or read table definitions from a CloudFormation template (example handles a !Sub on TableName, i.e. TableName: !Sub "${env}-users" ):
…const {DocumentClient} = require('aws-sdk/clients/dynamodb');
const isTest = process.env.JEST_WORKER_ID;
const config = {
convertEmptyValues: true,
...(isTest && {
endpoint: 'localhost:8000',
sslEnabled: false,
region: 'local-env',
credentials: {
accessKeyId: 'fakeMyKeyId',
secretAccessKey: 'fakeSecretAccessKey',
},
}),
};
const ddb = new DocumentClient(config);const {DynamoDB} = require('@aws-sdk/client-dynamodb');
const {DynamoDBDocument} = require('@aws-sdk/lib-dynamodb');
const isTest = process.env.JEST_WORKER_ID;
const ddb = DynamoDBDocument.from(
new DynamoDB({
...(isTest && {
endpoint: 'http://localhost:8000',
region: 'local-env',
credentials: {
accessKeyId: 'fakeMyKeyId',
secretAccessKey: 'fakeSecretAccessKey',
},
}),
}),
{
marshallOptions: {
convertEmptyValues: true,
},
}
);it('should insert item into table', async () => {
await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}}).promise();
const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise();
expect(Item).toEqual({
id: '1',
hello: 'world',
});
});By default the jest-dynamodb-config.js is read from cwd directory, but this might not be suitable for monorepos with nested jest projects with nested jest.config.* files nested in subdirectories.
If your jest-dynamodb-config.js file is not located at {cwd}/jest-dynamodb-config.js or you are using nested jest projects, you can define the environment variable JEST_DYNAMODB_CONFIG with the absolute path of the respective jest-dynamodb-config.js file.
JEST_DYNAMODB_CONFIG in nested project// src/nested/project/jest.config.js
const path = require('path');
// Define path of project level config - extension not required as file will be imported via `require(process.env.JEST_DYNAMODB_CONFIG)`
process.env.JEST_DYNAMODB_CONFIG = path.resolve(__dirname, './jest-dynamodb-config');
module.exports = {
preset: '@shelf/jest-dynamodb'
displayName: 'nested-project',
};UnknownError: Not Found
Perhaps something is using your port specified in jest-dynamodb-config.js.
com.almworks.sqlite4java.Internal log WARNING: [sqlite] cannot open DB[1]:
See https://www.josephso.dev/using-jest-dynamodb-in-apple-silicon-platform-workaround/#community-build
$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tagsMIT © Shelf
No open issues yet, or sync has not completed.