helper.getNode return null

Hi all,

I am trying to develop unit test for my node-red flows.
However i am facing issue with helper.getNode() function.

It returns null after passing node_id. These node id's are correctly retrieved using loadFlowFile() function and you can see the node-id's in output code.

import "mocha";
import { expect } from "chai";
import * as sinon from "sinon";

const requiredNodes = [
    require("../node_modules/@node-red/nodes/core/common/20-inject.js"),
    require("../node_modules/@node-red/nodes/core/common/21-debug.js"),
    require("../node_modules/@node-red/nodes/core/function/10-function.js"),
    require("../node_modules/@node-red/nodes/core/function/10-switch.js"),
    require("../node_modules/@node-red/nodes/core/function/89-delay.js"),
];



describe("Node-RED flow test", () => {
    const helper = require("node-red-node-test-helper");

    
    let inputNodeIds: string[] = [];
    let outputNodeIds: string[] = [];

    function loadFlowFile() {
        const flow: any[] = require("../src/flows.json");


        flow.forEach((node) => {
            console.log(" Node Is = ",node)
            if (node.type === "inject") {
                inputNodeIds[inputNodeIds.length] = node.id;
                node.type = "helper";
            }
            if (node.type === "debug") {
                outputNodeIds[outputNodeIds.length] = node.id;
                node.type = "helper";
            }
        });
        return flow;
    }

    before(() => {
        helper.init(require.resolve('node-red'));
    });

    beforeEach((done) => {
        helper.startServer(done);
    });

    afterEach((done) => {
        helper.unload().then(() => {
            helper.stopServer(done);
        });
    })

    function runFirstTest(done: Mocha.Done) {
        const flow = loadFlowFile();
        helper.load(requiredNodes, flow, () => {

            console.log("Input Node IDs = ",inputNodeIds)
            console.log("Output Node IDs = ",outputNodeIds)

            helper.getNode()

            const inputNode = helper.getNode(inputNodeIds[0]);
            const outputNode = helper.getNode(outputNodeIds[0]);

            console.log("Input Node = ",inputNode)
            console.log("Output Node  = ",outputNode)

            outputNode.on("input", (msg: any) => {
                try {
                    
                    expect(msg.payload).to.equal("payload contains 0")
                    done();
                } catch (e) {
                    done(e);
                }
            });

            inputNode.wires[0].forEach((wire: string) => {
                const node = helper.getNode(wire);
                console.log(" Node  = ",node)
                node.receive({ payload: 123450 });
            })
        });
    }

    it("should send 'payload contains 0' when the first digit is 0", (done) => {
        runFirstTest(done);
    });

The Output is

Input Node IDs =  []
Output Node IDs =  [
  '864ca23480dcda54',
  'a07686b6eb1759d0',
  'c4157b49eec5e27f',
  'f27933cd8c9c2013',
  '0fde90d316a166df',
  '58f74f3fde83e4d0',
  '439bbc55aa2305aa',
  '5eb780a53e7b079d',
  'e513449f80346d83'
]
Input Node =  null
Output Node  =  null
    1) should send 'payload contains 0' when the first digit is 0


  0 passing (2s)
  1 failing

  1) Node-RED flow test
       should send 'payload contains 0' when the first digit is 0:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\Abbad Zafar\Desktop\node-red\node-red-email\test\flow-test.ts)


Welcome @Zafar

Can you confirm whether you’re trying to develop flows or custom nodes for node red?

The package is for custom node development, not flows

Hi, @marcus-j-davies . Yes, i am trying to test the nodes for node-red

Ok, as your developing your own custom node modules - i have recategorised this post

I’m just about to head out - but a member of the forums may be around.

1 Like

@Zafar

I don't see where you are loading your custom node?
are you sure this is for your own custom nodes, as in you have developed nodes that users can install?

As I said, this package is for developers, developing node modules for Node RED, not writing flows.
Example: I don't see a require for your custom written node :man_shrugging:

I have moved this back out of Developing Nodes, unless I can be corrected, apologies if so.

Maybe this is what you are looking for?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.