Socket
Socket
Sign inDemoInstall

node-red-contrib-mock-node

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-red-contrib-mock-node

Crude facility to mock NodeRED nodes for unit testing


Version published
Weekly downloads
2
Maintainers
1
Install size
11.5 kB
Created
Weekly downloads
 

Readme

Source

node-red-contrib-mock-node

Crude way of mocking node-red nodes so you can unit test them easily (ish).

Installation

$ npm install node-red-contrib-mock-node --save-dev

Example

Consider the following example node:

module.exports = function (RED) {
    'use strict';

    RED.nodes.registerType('example-node-red-node', function (config) {

        RED.nodes.createNode(this, config);
        var node = this;

        node.context().set('test', config.testValue);
        node.context().flow.set('test', config.testValue + 1);
        node.context().global.set('test', config.testValue + 2);

        node.on('input', function (msg) {
            node.send(msg);
            node.status({
                fill: 'green',
                shape: 'dot',
                text: 'hello'
            });
        });
    });
};

We can test it like so:

var nodeRedModule = require('./example-node-red-node.js');
var mock = require('node-red-contrib-mock-node');

it('should test properly', function () {

    var node = mock(nodeRedModule, {
        testValue: 3
    }, {
        username: 'uname',
        password: 'pword'
    }, function(module, node) {
       // Do stuff with the module and node before the node's registerType callback is invoked.
    });

    assert.strictEqual(node.credentials.username, 'uname');
    assert.strictEqual(node.credentials.password, 'pword');

    assert.strictEqual(node.context().get('test'), 3);
    assert.strictEqual(node.context().flow.get('test'), 4);
    assert.strictEqual(node.context().global.get('test'), 5);

    var msg = {topic: 'topic', payload: 'payload'};
    node.emit('input', msg);
    assert.strictEqual(node.sent(0), msg);

    assert.strictEqual(node.status().fill, 'green');
    assert.strictEqual(node.status().shape, 'dot');
    assert.strictEqual(node.status().text, 'hello');
});

The key part is the arguments to the function mock:

  1. This the the node red node you want to test, loaded via require.
  2. This is the config that would be passed to the node if you have pressed deploy in the node red ui.
  3. Optional - These are the credentials (http://nodered.org/docs/creating-nodes/credentials).
  4. Optional - A callback that is invoked before the node's registerType callback is invoked (see example above).

Keywords

FAQs

Last updated on 19 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc