Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Appearance

Vault TestContainers

ATs.ED package to help you easily test your code using the power ofTestContainers with HashiCorp Vault.

Note: This package doesnot depend on@tsed/platform-http and can be used with any test framework.

✨ Features

  • 🚀 Easily spin up a Vault server in a Docker container for your tests
  • 🛑 Automatically stop the Vault server after your tests
  • 🔄 Reset the Vault server state between tests
  • 🔐 Pre-configured with dev mode and root token for easy testing

📦 Installation

Install the package with your favorite package manager:

sh
npm install --save-dev @tsedio/testcontainers-vault
sh
yarn add --dev @tsedio/testcontainers-vault
sh
pnpm add --dev @tsedio/testcontainers-vault
sh
bun add --dev @tsedio/testcontainers-vault

WARNING

See our documentation page for instructions oninstalling premium plugins.

⚙️ Configuration

Set up a global test lifecycle to manage your Vault container.

🧪 Vitest

Add a global setup in yourvitest.config.ts:

ts
import {defineConfig}from "vitest/config";export default defineConfig({  test: {    globalSetup: [import.meta.resolve("@tsed/testcontainers-vault/vitest/setup")]  }});

🧪 Jest

AddglobalSetup andglobalTeardown to your Jest config:

ts
// jest.config.jsmodule.exports = {  globalSetup: ["jest.setup.js"],  globalTeardown: ["jest.teardown.js"]};

Create the following files:

ts
// jest.setup.jsimport {TestContainersVault}from "@tsedio/testcontainers-vault";module.exports = async ()=> {  await TestContainersVault.startContainer();};// jest.teardown.jsimport {TestContainersVault}from "@tsedio/testcontainers-vault";module.exports = async ()=> {  await TestContainersVault.stopContainer();};

🛠️ Usage

Set up a Vault connection in your project like this:

ts
import {withOptions}from "@tsed/config";import {DITest}from "@tsed/di";import {VaultConfigSource}from "@tsedio/config-vault";import {TestContainersVault}from "@tsedio/testcontainers-vault";describe("Integration test", ()=> {  beforeEach(async ()=> {    return DITest.create({      extends: [        withOptions(VaultConfigSource, {          name:"vault",          ...TestContainersVault.getVaultOptions(),          secretPath:"secret/data/tsed-test"        })      ]    });  });  afterEach(()=> DITest.reset());  it("should store and retrieve secrets",async ()=> {    const configs = inject<CONFIG_SOURCES>(CONFIG_SOURCES);    const instance = configs.vaultas VaultConfigSource;    await instance.set("hello","world");    const result = await instance.getAll();    expect(result).toEqual({      hello:"world"    });  });});

💡 Tips

  • 🧹 UseTestContainersVault.reset() to clear all secrets in the Vault server between tests.
  • 🔐 The default root token isdev-token and the server runs in dev mode.
  • 🌐 You can useTestContainersVault.getClient() to get a pre-configured node-vault client.

📚 Resources


[8]ページ先頭

©2009-2025 Movatter.jp