I build a custom node and want to unittest it using node-red-node-test-helper
.
I first make a custom flow in Node-RED and exported it. Then I follow the examples to turn that flow json object into a pure node array. Finally, I use helper routines to load the head and the tail node for input and output. But helper.getNode(headID)
and helper.getNode(tailID)
keep giving me null
.
I stepped into the getNode()
but found that my activeFlows
is empty.
I can't figure out why since I assume the helper can run a defined node list literal without a problem. Here is my test routine:
async function testFlow(nodeRuntime, helper, wiredNodes, inputMsg, expectation) {
// usage:
// - nodeRuntime: require(service/node/node.js)
// - wiredNodes: simplified node list from standard Node-RED flow json object
// - e.g., [
// {id: "my.node.id", type: "service_a", "wires": ["next.node.id", ...]},
// {id: "next.node.id", type: "service_b", "wires": ["3rd.node.id", ...]},
// ...
// ]
// - inputMsg: Node-RED message fed to the first node of the flow
// - e.g., { topic: "my.topic", payload: obj, miaservice: "service_name" }
// - expectation: assertion wrapper; can be one of:
// - (outMsg) => { expect(outMsg).to....(); }
// - (outMsg) => { expect(customCondition()).to...(); }
// e.g., if your flow has no output, but writes to a file, then simply test then file content and ignore the outMsg argument
let tail = wiredNodes.slice(-1)[0];
// lazy-append helper to user flow
let tailID = "flowtest.tail";
let helperName = 'helper';
let userAddedHelperAsTail = tail.type === helperName;
if ( !userAddedHelperAsTail ) {
// assume: tail has only one flow path,
// wire up help node as new tail
wiredNodes.slice(-1)[0].wires[0].push(tailID);
wiredNodes.push({id: tailID, type: helperName});
}
await helper.load(nodeRuntime, wiredNodes);
var tailNode = helper.getNode(tailID);
var headNode = helper.getNode(wiredNodes[0].id);
#
# expected headNode to be valid, but got null!
#
headNode.receive(inputMsg);
await asyncAssertFlowOutput(tailNode, expectation);
};
Using this routine, in the debugger I can see that my wiredNodes
are all valid according to helper's user guide: They all have id
, type
, wires
, and other custom fields.
Help is much appreciated!