vitest-dynamodb-local
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

vitest-dynamodb-local

vitest-dynamodb-local is a fast DynamoDB mock for testing with vitest.

Installation

npm i vitest-dynamodb-local -D
# or
yarn add vitest-dynamodb-local -D
# or
pnpm add vitest-dynamodb-local -D

Usage

1. Set globalSetup in vitest.config.ts

// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-dynamodb-local"],
  },
});

2. Config file

In your project root, create a config file with the tables schemas, and an optional basePort to run dynamo-db-local on.

You can write the config file in either json, js, or cjs format.

In json:

{
  "tables": [
    {
      "TableName": "table",
      "KeySchema": [{ "AttributeName": "id", "KeyType": "HASH" }],
      "AttributeDefinitions": [{ "AttributeName": "id", "AttributeType": "S" }],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
      }
    }
  ],
  "basePort": 8000
}

In js or cjs:

module.exports = {
  // your configures
};

3. Update your source code

const client = new DynamoDBClient({
  ...yourConfig,
  ...(process.env.MOCK_DYNAMODB_ENDPOINT && {
    endpoint: process.env.MOCK_DYNAMODB_ENDPOINT,
    region: "local",
  }),
});

After all your tests, make sure you destroy your client. You can even do this by adding an afterAll in a setupFilesAfterEnv file.

afterAll(() => {
  client.destroy();
});

[Optional] Using fixtures

You can set some fixture data before each test:

vitest-dynamodb-local-config.json:

module.exports = {
  tables: [
    {
      // ...
      data: [{ id: "a", someattribute: "hello world" }],
    },
  ],
};

Debug errors

If you face any error in closing db, you can debug mode to see the error:

VITEST_DYNAMODB_LOCAL_DEBUG_CLOSING_DB=true npx vitest

License

MIT

Package Sidebar

Install

npm i vitest-dynamodb-local

Weekly Downloads

137

Version

0.2.0

License

MIT

Unpacked Size

17.7 kB

Total Files

7

Last publish

Collaborators

  • dgadelha