@node-red-tools/test-helpers
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

test-helpers

Collection of test helper functions for Node-Red flows

npm (scoped) Node.js CI

Usage

import { setup, teardown } from '@node-red-tools/test-helpers';
import amqp from 'amqplib';
import path from 'path';

before(async function() {
    this.context = await setup({
        flow: {
            userDir: process.cwd(),
        },
        containers: [
            {
                image: 'redis',
                name: 'test-redis',
                ports: [
                    {
                        host: 6379,
                        container: 6379,
                    },
                ],
            },
            {
                image: 'rabbitmq',
                name: 'test-rabbitmq',
                ports: [
                    {
                        host: 5672,
                        container: 5672,
                    },
                ],
            },
        ],
        values: {
            rabbitmq: async () => {
                const conn = await amqp.connect('amqp://127.0.0.1');

                return [conn, () => conn.close()];
            },
        },
    });

    global.rabbitmq = ctx.values.rabbitmq;
});

after(async function() {
    await teardown(this.context);
});

Using readiness probes

import { setup, teardown, probes } from '@node-red-tools/test-helpers';
import axios from 'axios';
import path from 'path';

before(() => {
    this.context = setup({
        flow: {
            path: path.resolve('../', __dirname),
            readinessProbe: {
                failureThreshold: 3,
                fn: probes.http({
                    method: 'GET',
                    path: '/my-endpoint',
                }),
            },
        },
        containers: [
            {
                image: 'redis',
                name: 'test-redis',
                ports: [
                    {
                        host: 6379,
                        container: 6379,
                    },
                ],
            },
            {
                image: 'rabbitmq',
                name: 'test-rabbitmq',
                ports: [
                    {
                        host: 5672,
                        container: 5672,
                    },
                ],
            },
        ],
    });
});

after(() => {
    teardown(this.context);
});

API

Docker

Start

import { docker } from '@node-red-tools/test-helpers';

await docker.start({
    name: 'my_nginx',
    image: 'nginx',
    ports: [
        {
            container: 80,
            host: 8888,
        },
    ],
    stdout: process.stdout,
    stderr: process.stderr,
});

Start all

import { docker } from '@node-red-tools/test-helpers';

await docker.startAll([
    {
        name: 'my_nginx_1',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8888,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
    {
        name: 'my_nginx_2',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8889,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
]);

Stop

import { docker } from '@node-red-tools/test-helpers';

const terminate = await docker.start({
    name: 'my_nginx',
    image: 'nginx',
    ports: [
        {
            container: 80,
            host: 8888,
        },
    ],
    stdout: process.stdout,
    stderr: process.stderr,
});

await terminate();

or

import { docker } from '@node-red-tools/test-helpers';

await docker.start({
    name: 'my_nginx',
    image: 'nginx',
    ports: [
        {
            container: 80,
            host: 8888,
        },
    ],
    stdout: process.stdout,
    stderr: process.stderr,
});

const id = await docker.findID('my_nginx');

await docker.stop(id);

Stop all

import { docker } from '@node-red-tools/test-helpers';

const terminateAll = await docker.startAll([
    {
        name: 'my_nginx_1',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8888,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
    {
        name: 'my_nginx_2',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8889,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
]);

docker.stopAll(terminateAll);

or

import { docker } from '@node-red-tools/test-helpers';

await docker.startAll([
    {
        name: 'my_nginx_1',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8888,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
    {
        name: 'my_nginx_2',
        image: 'nginx',
        ports: [
            {
                container: 80,
                host: 8889,
            },
        ],
        stdout: process.stdout,
        stderr: process.stderr,
    },
]);

const ids = await Promise.all([
    docker.findID('my_nginx_1'),
    docker.findID('my_nginx_2'),
]);

await docker.stopAll(ids);

Flow

Start

import { flow } from '@node-red-tools/test-helpers';

await flow.start({
    userDir: process.cwd(),
});

Stop

import { flow } from '@node-red-tools/test-helpers';

const termination = await flow.start({
    userDir: process.cwd(),
});

await termination();

Readme

Keywords

Package Sidebar

Install

npm i @node-red-tools/test-helpers

Weekly Downloads

1

Version

0.6.0

License

MIT

Unpacked Size

109 kB

Total Files

42

Last publish

Collaborators

  • j9080
  • ziflex