{"id":13485123,"url":"https://github.com/shelfio/jest-dynamodb","last_synced_at":"2025-06-10T08:09:00.120Z","repository":{"id":34645122,"uuid":"181049859","full_name":"shelfio/jest-dynamodb","owner":"shelfio","description":"Jest preset for DynamoDB local server","archived":false,"fork":false,"pushed_at":"2025-05-27T21:33:45.000Z","size":323,"stargazers_count":190,"open_issues_count":44,"forks_count":37,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-05-29T08:08:23.937Z","etag":null,"topics":["dynamodb","jest","jest-environment","jest-preset","nodejs","npm-package","tests"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shelfio.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-04-12T17:01:03.000Z","updated_at":"2025-05-27T06:17:20.000Z","dependencies_parsed_at":"2023-12-21T01:01:53.437Z","dependency_job_id":"fead6e2c-e368-4af8-9fc6-7580b76c66a7","html_url":"https://github.com/shelfio/jest-dynamodb","commit_stats":{"total_commits":431,"total_committers":37,"mean_commits":11.64864864864865,"dds":0.6403712296983759,"last_synced_commit":"874d250012ac3f378574091ec76ad47b263c811b"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-dynamodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-dynamodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-dynamodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-dynamodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shelfio","download_url":"https://codeload.github.com/shelfio/jest-dynamodb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shelfio%2Fjest-dynamodb/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258180613,"owners_count":22661185,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamodb","jest","jest-environment","jest-preset","nodejs","npm-package","tests"],"created_at":"2024-07-31T17:01:46.831Z","updated_at":"2025-06-10T08:09:00.094Z","avatar_url":"https://github.com/shelfio.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Packages","JavaScript"],"sub_categories":["Presets"],"readme":"# jest-dynamodb [![CircleCI](https://circleci.com/gh/shelfio/jest-dynamodb/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/jest-dynamodb/tree/master) ![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) [![npm (scoped)](https://img.shields.io/npm/v/@shelf/jest-dynamodb.svg)](https://www.npmjs.com/package/@shelf/jest-dynamodb)\n\n\u003e Jest preset to run DynamoDB Local\n\n## Usage\n\n### 0. Install\n\n```\n$ yarn add @shelf/jest-dynamodb --dev\n```\n\nMake sure `java` runtime available for running [DynamoDBLocal.jar](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html)\n\n### 1. Create `jest.config.js`\n\n```js\nmodule.exports = {\n  preset: '@shelf/jest-dynamodb',\n};\n```\n\n### 2. Create `jest-dynamodb-config.js`\n\n#### 2.1 Properties\n\n##### tables\n\n- Type: `object[]`\n- Required: `true`\n\nArray of createTable params.\n\n- [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property).\n\n##### port\n\n- Type: `number`\n- Required: `false`\n\nPort number. The default port number is `8000`.\n\n##### hostname\n\n- Type: `string`\n- Required: `false`\n\nHostname. The default hostname is `localhost`.\n\n##### options\n\n- Type: `string[]`\n- Required: `false`\n\nAdditional arguments for dynamodb-local. The default value is `['-sharedDb']`.\n\n- [dynamodb-local](https://github.com/rynop/dynamodb-local)\n- [DynamoDB Local Usage Notes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html)\n\n##### clientConfig\n\n- Type: `object`\n- Required: `false`\n\nConstructor params of DynamoDB client.\n\n- [Constructor Property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#constructor-property)\n\n##### installerConfig\n\n- Type: `{installPath?: string, downloadUrl?: string}`\n- Required: `false`\n\n- `installPath` defines the location where dynamodb-local is installed or will be installed.\n- `downloadUrl` defines the url of dynamodb-local package.\n\nThe default value is defined at https://github.com/rynop/dynamodb-local/blob/2e6c1cb2edde4de0dc51a71c193c510b939d4352/index.js#L16-L19\n\n#### 2.2 Examples\n\nYou can set up tables as an object:\n \u003e Whole list of config properties can be found [here](https://github.com/shelfio/jest-dynamodb/blob/6c64dbd4ee5a68230469ea14cbfb814470521197/src/types.ts#L80-L87)\n```js\n/**\n * @type {import('@shelf/jest-dynamodb/lib').Config}')}\n */\nconst config = {\n  tables: [\n    {\n      TableName: `files`,\n      KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],\n      AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],\n      ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},\n    },\n    // etc\n  ],\n  port: 8000,\n};\nmodule.exports = config;\n```\n\nOr as an async function (particularly useful when resolving DynamoDB setup dynamically from `serverless.yml`):\n\n```js\nmodule.exports = async () =\u003e {\n  const serverless = new (require('serverless'))();\n  // If using monorepo where DynamoDB serverless.yml is in another directory\n  // const serverless = new (require('serverless'))({ servicePath: '../../../core/data' });\n\n  await serverless.init();\n  const service = await serverless.variables.populateService();\n  const resources = service.resources.filter(r =\u003e Object.keys(r).includes('Resources'))[0];\n\n  const tables = Object.keys(resources)\n    .map(name =\u003e resources[name])\n    .filter(r =\u003e r.Type === 'AWS::DynamoDB::Table')\n    .map(r =\u003e r.Properties);\n\n  return {\n    tables,\n    port: 8000,\n  };\n};\n```\n\nOr read table definitions from a CloudFormation template (example handles a !Sub on TableName, i.e. TableName: !Sub \"\\${env}-users\" ):\n\n```js\nconst yaml = require('js-yaml');\nconst fs = require('fs');\nconst {CLOUDFORMATION_SCHEMA} = require('cloudformation-js-yaml-schema');\n\nmodule.exports = async () =\u003e {\n  const cf = yaml.load(fs.readFileSync('../cf-templates/example-stack.yaml', 'utf8'), {\n    schema: CLOUDFORMATION_SCHEMA,\n  });\n  var tables = [];\n  Object.keys(cf.Resources).forEach(item =\u003e {\n    tables.push(cf.Resources[item]);\n  });\n\n  tables = tables\n    .filter(r =\u003e r.Type === 'AWS::DynamoDB::Table')\n    .map(r =\u003e {\n      let table = r.Properties;\n      if (typeof r.TableName === 'object') {\n        table.TableName = table.TableName.data.replace('${env}', 'test');\n      }\n      delete table.TimeToLiveSpecification; //errors on dynamo-local\n      return table;\n    });\n\n  return {\n    tables,\n    port: 8000,\n  };\n};\n```\n\n### 3.1 Configure DynamoDB client (from aws-sdk v2)\n\n```js\nconst {DocumentClient} = require('aws-sdk/clients/dynamodb');\n\nconst isTest = process.env.JEST_WORKER_ID;\nconst config = {\n  convertEmptyValues: true,\n  ...(isTest \u0026\u0026 {\n    endpoint: 'localhost:8000',\n    sslEnabled: false,\n    region: 'local-env',\n    credentials: {\n      accessKeyId: 'fakeMyKeyId',\n      secretAccessKey: 'fakeSecretAccessKey',\n    },\n  }),\n};\n\nconst ddb = new DocumentClient(config);\n```\n\n### 3.2 Configure DynamoDB client (from aws-sdk v3)\n\n```js\nconst {DynamoDB} = require('@aws-sdk/client-dynamodb');\nconst {DynamoDBDocument} = require('@aws-sdk/lib-dynamodb');\n\nconst isTest = process.env.JEST_WORKER_ID;\n\nconst ddb = DynamoDBDocument.from(\n  new DynamoDB({\n    ...(isTest \u0026\u0026 {\n      endpoint: 'http://localhost:8000',\n      region: 'local-env',\n      credentials: {\n        accessKeyId: 'fakeMyKeyId',\n        secretAccessKey: 'fakeSecretAccessKey',\n      },\n    }),\n  }),\n  {\n    marshallOptions: {\n      convertEmptyValues: true,\n    },\n  }\n);\n```\n\n### 4. PROFIT! Write tests\n\n```js\nit('should insert item into table', async () =\u003e {\n  await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}}).promise();\n\n  const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise();\n\n  expect(Item).toEqual({\n    id: '1',\n    hello: 'world',\n  });\n});\n```\n\n## Monorepo Support\n\nBy default the `jest-dynamodb-config.js` is read from `cwd` directory, but this might not be suitable for monorepos with nested [jest projects](https://jestjs.io/docs/configuration#projects-arraystring--projectconfig) with nested `jest.config.*` files nested in subdirectories.\n\nIf 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.\n\n### Example Using `JEST_DYNAMODB_CONFIG` in nested project\n\n```\n// src/nested/project/jest.config.js\nconst path = require('path');\n\n// Define path of project level config - extension not required as file will be imported via `require(process.env.JEST_DYNAMODB_CONFIG)`\nprocess.env.JEST_DYNAMODB_CONFIG = path.resolve(__dirname, './jest-dynamodb-config');\n\nmodule.exports = {\n  preset: '@shelf/jest-dynamodb'\n  displayName: 'nested-project',\n};\n```\n\n## Troubleshooting\n\n\u003cdetails\u003e\n\u003csummary\u003eUnknownError: Not Found\u003c/summary\u003e\n\nPerhaps something is using your port specified in `jest-dynamodb-config.js`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003ecom.almworks.sqlite4java.Internal log WARNING: [sqlite] cannot open DB[1]:\u003c/summary\u003e\n\nSee https://www.josephso.dev/using-jest-dynamodb-in-apple-silicon-platform-workaround/#community-build\n\n\u003c/details\u003e\n\n## Alternatives\n\n- [jest-dynalite](https://github.com/freshollie/jest-dynalite) - a much lighter version which spins up an instance for each runner \u0026 doesn't depend on Java\n\n## Read\n\n- [dynamodb-local](https://github.com/rynop/dynamodb-local)\n- [DynamoDB Local Usage Notes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html)\n- [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property)\n\n## Used by\n\n- [dynamodb-parallel-scan](https://github.com/shelfio/dynamodb-parallel-scan)\n- [@nasa-gcn/dynamodb-autoincrement](https://github.com/nasa-gcn/dynamodb-autoincrement)\n\n## See Also\n\n- [jest-mongodb](https://github.com/shelfio/jest-mongodb)\n\n## Publish\n\n```sh\n$ git checkout master\n$ yarn version\n$ yarn publish\n$ git push origin master --tags\n```\n\n## License\n\nMIT © [Shelf](https://shelf.io)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshelfio%2Fjest-dynamodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshelfio%2Fjest-dynamodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshelfio%2Fjest-dynamodb/lists"}