Catalog npm
Back to packageRELEASE DETAILS

4.0.1-pre.3

npm

@0xflick/jest-dynamodb

Run your tests using Jest & DynamoDB local

dynamodbdynamodb localjestjest environmentjest preset
Published21 Apr 2025
Direct dependencies22
Transitive dependenciesNot calculated

README

Current package documentation

jest-dynamodb CircleCI npm (scoped)

Jest preset to run DynamoDB Local

Usage

0. Install

$ yarn add @shelf/jest-dynamodb --dev

Make sure java runtime available for running DynamoDBLocal.jar

1. Create jest.config.js

module.exports = {
  preset: '@shelf/jest-dynamodb',
};

2. Create jest-dynamodb-config.js

2.1 Properties

tables
  • Type: object[]
  • Required: true

Array of createTable params.

port
  • Type: number
  • Required: false

Port number. The default port number is 8000.

hostname
  • Type: string
  • Required: false

Hostname. The default hostname is localhost.

options
  • Type: string[]
  • Required: false

Additional arguments for dynamodb-local. The default value is ['-sharedDb'].

clientConfig
  • Type: object
  • Required: false

Constructor params of DynamoDB client.

installerConfig
  • 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

2.2 Examples

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):

module.exports = async () => {
  const serverless = new (require('serverless'))();
  // If using monorepo where DynamoDB serverless.yml is in another directory
  // const serverless = new (require('serverless'))({ servicePath: '../../../core/data' });

  await serverless.init();
  const service = await serverless.variables.populateService();
  const resources = service.resources.filter(r => Object.keys(r).includes('Resources'))[0];

  const tables = Object.keys(resources)
    .map(name => resources[name])
    .filter(r => r.Type === 'AWS::DynamoDB::Table')
    .map(r => r.Properties);

  return {
    tables,
    port: 8000,
  };
};

Or read table definitions from a CloudFormation template (example handles a !Sub on TableName, i.e. TableName: !Sub "${env}-users" ):

const yaml = require('js-yaml');
const fs = require('fs');
const {CLOUDFORMATION_SCHEMA} = require('cloudformation-js-yaml-schema');

module.exports = async () => {
  const cf = yaml.load(fs.readFileSync('../cf-templates/example-stack.yaml', 'utf8'), {
    schema: CLOUDFORMATION_SCHEMA,
  });
  var tables = [];
  Object.keys(cf.Resources).forEach(item => {
    tables.push(cf.Resources[item]);
  });

  tables = tables
    .filter(r => r.Type === 'AWS::DynamoDB::Table')
    .map(r => {
      let table = r.Properties;
      if (typeof r.TableName === 'object') {
        table.TableName = table.TableName.data.replace('${env}', 'test');
      }
      delete table.TimeToLiveSpecification; //errors on dynamo-local
      return table;
    });

  return {
    tables,
    port: 8000,
  };
};

3.1 Configure DynamoDB client (from aws-sdk v2)

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);

3.2 Configure DynamoDB client (from aws-sdk v3)

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,
    },
  }
);

4. PROFIT! Write tests

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',
  });
});

Monorepo Support

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.

Example Using 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',
};

Troubleshooting

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

Alternatives

  • jest-dynalite - a much lighter version which spins up an instance for each runner & doesn't depend on Java

Read

Used by

See Also

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 22

Native requirements
22 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@aws-sdk/client-dynamodb
Environment
{
  "local_name": "@aws-sdk/client-dynamodb",
  "alias": false,
  "peer_metadata": null
}
^3.624.0runtime
@aws-sdk/lib-dynamodb
Environment
{
  "local_name": "@aws-sdk/lib-dynamodb",
  "alias": false,
  "peer_metadata": null
}
^3.624.0runtime
@aws-sdk/util-dynamodb
Environment
{
  "local_name": "@aws-sdk/util-dynamodb",
  "alias": false,
  "peer_metadata": null
}
^3.624.0runtime
cwd
Environment
{
  "local_name": "cwd",
  "alias": false,
  "peer_metadata": null
}
0.10.0runtime
debug
Environment
{
  "local_name": "debug",
  "alias": false,
  "peer_metadata": null
}
4.3.4runtime
dynamodb-local
Environment
{
  "local_name": "dynamodb-local",
  "alias": false,
  "peer_metadata": null
}
0.0.34runtime
@babel/cli
Environment
{
  "local_name": "@babel/cli",
  "alias": false,
  "peer_metadata": null
}
7.27.0development
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
7.26.10development
@shelf/babel-config
Environment
{
  "local_name": "@shelf/babel-config",
  "alias": false,
  "peer_metadata": null
}
1.2.0development
@shelf/eslint-config
Environment
{
  "local_name": "@shelf/eslint-config",
  "alias": false,
  "peer_metadata": null
}
4.2.1development
@shelf/prettier-config
Environment
{
  "local_name": "@shelf/prettier-config",
  "alias": false,
  "peer_metadata": null
}
1.0.0development
@shelf/tsconfig
Environment
{
  "local_name": "@shelf/tsconfig",
  "alias": false,
  "peer_metadata": null
}
0.1.0development
@types/aws-sdk
Environment
{
  "local_name": "@types/aws-sdk",
  "alias": false,
  "peer_metadata": null
}
2.7.4development
@types/cwd
Environment
{
  "local_name": "@types/cwd",
  "alias": false,
  "peer_metadata": null
}
^0.10.2development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
29.5.14development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
22development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
9.24.0development
husky
Environment
{
  "local_name": "husky",
  "alias": false,
  "peer_metadata": null
}
8.0.3development
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
29.7.0development
lint-staged
Environment
{
  "local_name": "lint-staged",
  "alias": false,
  "peer_metadata": null
}
13.3.0development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
3.5.3development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
5.8.3development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "f1ebe9c49b553fe36cfb4ce754c69cd8c9f99804",
  "integrity": "sha512-guPMnHXXUtH86mfNqR1RzmKpEJztMVAaPQ72D3/LXJYWBKK3jaTu0cRPCcd0pmL/OZNrpyZD0HUuMSIPLVQpwg==",
  "tarball": "https://registry.npmjs.org/@0xflick/jest-dynamodb/-/jest-dynamodb-4.0.1-pre.3.tgz",
  "fileCount": 29,
  "unpackedSize": 22597,
  "signatures": [
    {
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U",
      "sig": "MEYCIQCyE9WqxGYy5FbzRw3M60BiGBroXCHgp8Vu8uHxW7s8twIhAKFQsevi6j7mfwF+9R63T6zcGPe/DrBCf9Dkh8jzgEit"
    }
  ]
}

Repository

PENDING
shelfio/jest-dynamodb

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 23:36 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:9f7cd6c1f45f43893be21ddad332b489e75c20cf2e837740254c7b4c05306106

Other collected versions

4.0.1-pre.218 AprView all collected versions →