@dweidenfeld/testcontainers
TypeScript icon, indicating that this package has built-in type declarations

1.2.0-3 • Public • Published

Testcontainers

Testcontainers is a NodeJS library that supports tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Build Status npm version npm version

Install

npm i -D testcontainers

Usage

Run your app with the DEBUG=testcontainers environment variable set to see debug output.

The following environment variables are supported:

Key Example value Behaviour
DOCKER_HOST tcp://docker:2375 Override the Docker host, useful for DIND in CI environments

Examples

Using a pre-built Docker image:

const redis = require("async-redis");
const { GenericContainer } = require("testcontainers");

(async () => {
  const container = await new GenericContainer("redis")
      .withExposedPorts(6379)
      .start();

  const redisClient = redis.createClient(
      container.getMappedPort(6379),
      container.getContainerIpAddress(),
  );
  await redisClient.quit();

  await container.stop();
})();

Building and using your own Docker image:

const path = require('path');
const { GenericContainer } = require("testcontainers");

(async () => {
  const buildContext = path.resolve(__dirname, "my-dir");
  
  const container = await GenericContainer.fromDockerfile(buildContext);
  
  const startedContainer = await container
      .withEnv("KEY", "VALUE")
      .withExposedPorts(8080)
      .start();

  await startedContainer.stop();
})();

Running commands inside running container

const { GenericContainer, Wait } = require("testcontainers");

(async () => {
  const container = await new GenericContainer("mysql")
      .withEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "true")
      .withWaitStrategy(Wait.forLogMessage("ready for connections"))
      .start();

  const { output, exitCode } = await container.exec(["mysql", "--execute", "show databases"]);

  console.log(exitCode, output);

  await container.stop();
})();

Wait Strategies

Ordinarily Testcontainers will wait for up to 60 seconds for the container's mapped network ports to start listening. If the default 60s timeout is not sufficient, it can be altered with the withStartupTimeout() method:

const { GenericContainer } = require("testcontainers");
const { Duration, TemporalUnit } = require("node-duration");

const container = await new GenericContainer("redis")
    .withExposedPorts(6379)
    .withStartupTimeout(new Duration(100, TemporalUnit.SECONDS))
    .start();

Log output Wait Strategy

In some situations a container's log output is a simple way to determine if it is ready or not. For example, we can wait for a Ready message in the container's logs as follows:

const { GenericContainer, Wait } = require("testcontainers");

const container = await new GenericContainer("redis")
    .withExposedPorts(6379)
    .withWaitStrategy(Wait.forLogMessage("Ready to accept connections"))
    .start();

Readme

Keywords

Package Sidebar

Install

npm i @dweidenfeld/testcontainers

Weekly Downloads

5

Version

1.2.0-3

License

MIT

Unpacked Size

50.7 kB

Total Files

43

Last publish

Collaborators

  • dweidenfeld