# vitest-environment-testcontainers

> Vitest testing environment with Testcontainers

Latest version **0.1.0** (published 2023-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install vitest-environment-testcontainers
pnpm add vitest-environment-testcontainers
yarn add vitest-environment-testcontainers
bun add vitest-environment-testcontainers
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-09-25 |
| First published | 2023-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Dexter Tan |
| Maintainers | dextertanyj |
| Keywords | vitest, vitest-environment, testing, integration, testcontainers, docker |

## Links

- npm: https://www.npmjs.com/package/vitest-environment-testcontainers
- Repository: https://github.com/dextertanyj/vitest-environment-testcontainers
- Issues: https://github.com/dextertanyj/vitest-environment-testcontainers/issues
- npm.io page: https://npm.io/package/vitest-environment-testcontainers

## Dependencies (1)

- [testcontainers](https://npm.io/package/testcontainers.md) ^10.2.1

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2023-09-25

## README

# vitest-environment-testcontainers

A [Vitest](https://vitest.dev/) environment with integrated support for [Testcontainers](https://testcontainers.com/).

## Features

- ⚙️ Setup and teardown containers automatically during Vitest runs.
- ✏️ Type-safe interface.
- 📖 Access container metadata during test runtime.

## Requirements

- [Docker](https://www.docker.com/)
- [Vitest](https://vitest.dev/)

## Quickstart

**1. Install `vitest-environment-testcontainers`.**

```shell
npm i -D vitest-environment-testcontainers
```

**2. Configure Vitest in `vitest.config.ts` to use the environment.**

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

export default defineConfig({
  test: {
    // ...
    environment: "testcontainers",
  },
});
```

_See [here](https://vitest.dev/guide/#configuring-vitest) for more information on how to configure Vitest._

**3. Specify the containers to launch.**

```ts
import { type EnvironmentOptions } from "vitest-environment-testcontainers";

const environmentOptions: EnvironmentOptions = {
  testcontainers: {
    containers: [
      {
        name: "database",
        image: "postgres:latest",
        ports: [5432],
        environment: {
          POSTGRES_USER: "root",
          POSTGRES_PASSWORD: "root",
          POSTGRES_DB: "test",
        },
        wait: {
          type: "PORT",
        },
      },
    ],
  },
};

export default defineConfig({
  test: {
    // ...
    environment: "testcontainers",
    environmentOptions,
  },
});
```

**4. Get information about the containers inside your tests.**

```ts
describe("Test", () => {
  const containers = globalThis.testcontainers.containers;

  // ...
});
```

The `containers` array contains objects of the following type:

```ts
{
  name: string;
  host: string;
  ports: Map<number, number>;
  configuration: ContainerConfiguration;
}
```

| Property        | Description                                                                          |
| --------------- | ------------------------------------------------------------------------------------ |
| `name`          | The name of the container as specified in the environment options.                   |
| `host`          | The hostname by which the container is accessible from.                              |
| `ports`         | A mapping of exposed container ports to their respective host ports.                 |
| `configuration` | The original configuration of the container as specified in the environment options. |

---
_Source: https://npm.io/package/vitest-environment-testcontainers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
