1.2.0-3 • Published 5 years ago

@dweidenfeld/testcontainers v1.2.0-3

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

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:

KeyExample valueBehaviour
DOCKER_HOSTtcp://docker:2375Override 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();