npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gosupersimple/jest-testcontainers

v3.0.0

Published

Jest preset for starting docker containers that stay up whilist your tests run.

Downloads

6

Readme

💡 This is a fork of unmaintained Trendyol/jest-testcontainers

jest-testcontainers

Jest preset for running docker containers with your tests. Primary purpose is to make it possible to use any database in integration tests. Since it uses docker images, custom database images with different plugins/configurations can be used in the integration tests. Using testcontainers-node under the hood. Inspired by @shelf/jest-mongodb.

npm version

Usage

Install

Docker should be installed on your system. If running inside a CI pipeline, see FAQ.md.

yarn add --dev @gosupersimple/jest-testcontainers

Edit Jest Config

On your jest.config.js add the project as the preset.

module.exports = {
  preset: '@gosupersimple/jest-testcontainers'
};

Declare Containers To Run

Create a file called jest-testcontainers-config.js and put it to root of your project, where you run npm test. If you would like to put this file somewhere else, you can use the JEST_TESTCONTAINERS_CONFIG_PATH environment variable to define a relative or absolute path. A sample configuration file;

module.exports = {
  redis: {
    image: 'redis',
    tag: 'alpine3.12',
    ports: [6379],
    env: {
      EXAMPLE: 'env',
    },
    wait: {
      type: 'text',
      text: 'Ready to accept connections'
    }
  },
//   more: {
//     image: 'any-docker-image', // postgresql, mongodb, neo4j etc.
//     ports: [1234, 4567], // ports to make accessible in tests
//   },
};

Connect To Containers

Every containers IP and Port info is registered to global variables to be used by your tests.

const redis = require('redis');
const { promisify } = require('util');

describe('testcontainers example suite', () => {
  let redisClient;

  beforeAll(() => {
    const redisConnectionURI = `redis://${global.__TESTCONTAINERS_REDIS_IP__}:${global.__TESTCONTAINERS_REDIS_PORT_6379__}`;
    redisClient = redis.createClient(redisConnectionURI);

    // if you have declared multiple containers, they will be available to access as well. e.g.
    // `global.__TESTCONTAINERS_${CONFIG_KEY}_IP__`
    // `global.__TESTCONTAINERS_${CONFIG_KEY}_PORT_${CONFIG_PORT}__`
  });

  afterAll(() => {
    redisClient.quit();
  });

  it('write should be ok', async () => {
    // Arrange
    const setAsync = promisify(redisClient.set).bind(redisClient);

    // Act
    const setResult = await setAsync('test', 73);

    // Assert
    expect(setResult).toEqual('OK');
  });
});

Documentation

Detailed documentation of the jest-testcontainers-config.js can be found at DOC.md.

Watch mode support

Starting with version 2.0.0 containers will not be stopped if Jest is started in watch mode. This greatly improves development productivity if your containers have a slow startup time (ex. Elasticsearch). This comes with the price that you have to be mindful that containers will be reused between watch test executions and you need to do proper cleanup in your after hooks.

If you want to disable this behavior you can set the JEST_TESTCONTAINERS_RESTART_ON_WATCH environment variable.

Wondering what will happen when those containers are not stopped when Jest is exited - testcontainer's ryuk will take care of them.

License

This project is licensed under the MIT License