Socket
Socket
Sign inDemoInstall

vitest-dynamodb-lite

Package Overview
Dependencies
4
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

vitest-dynamodb-lite

fast dynamodb mock on local for vitest


Version published
Maintainers
1
Created

Readme

Source

vitest-dynamodb-lite

vitest-dynamodb-lite is a fast, concurrent-safe DynamoDB mock for testing with vitest.

concurrent-test

In this gif, 16 test files run concurrently without manually launching any dynamodb server. Each dynamodb server is launched when each test file is executed.

vitest-dynamodb-lite runs a local dynamodb server for each test case, so it is safe to run tests concurrently. These test cases in this gif perform PUT and GET an item for the same table name and the same key but different dynamodb servers concurrently.

vitest-dynamodb-lite uses dynalite instead of DynamoDB Local to run DynamoDB locally.

This repository was forked from jest-dynalite to use in vitest and added some performance improvements.

Features

  • Optionally clear tables between tests
  • Isolated tables between test runners
  • Ability to specify a config directory
  • No java requirement
  • Works with only @aws-sdk/client-dynamodb instead of aws-sdk

Installation

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

Usage

1. Set setupFiles in vitest.config.ts

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

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

2. Config file

In your project root, create a config file with the tables schemas, and an optional basePort to run dynalite 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",
  }),
});

process.env.MOCK_DYNAMODB_ENDPOINT is unique to each test runner.

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-lite-config.json:

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

License

MIT

Keywords

FAQs

Last updated on 14 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc