百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jest-dynamodb

> 测试质量
开源

DynamoDB 本地服务器的预设

198 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

DynamoDB 本地服务器的预设

jest-dynamodb

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

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

2. Create jest-dynamodb-config.js

2.1 Properties

tables
  • Type: object[]
  • Required: true

Array of createTable params.

  • Create Table API.
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'].

  • dynamodb-local
  • DynamoDB Local Usage Notes
clientConfig
  • Type: object
  • Required: false

Constructor params of DynamoDB client.

  • Constructor Property
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

javascript
/**
 * @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" ):

…

3.1 Configure DynamoDB client (from aws-sdk v2)

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

javascript
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

javascript
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

  • dynamodb-local
  • DynamoDB Local Usage Notes
  • Create Table API

Used by

  • dynamodb-parallel-scan
  • @nasa-gcn/dynamodb-autoincrement

See Also

  • jest-mongodb

Publish

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

License

MIT © Shelf

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptdynamodbjestjest-environmentjest-preset

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.