node-red / node-red-node-test-helper

A test framework for Node-RED nodes
Apache License 2.0
57 stars 41 forks source link

Helper wrongly strips off all but the first message in its input message array #60

Closed kakyoism closed 1 year ago

kakyoism commented 1 year ago

Repro

Expected

Observed

Question

Is this by design? How do I properly test a node with multiple output ports?

knolleary commented 1 year ago

Please do not cross-post questions to both the forum and the issue list.

The test should pass: n2 should receive a message array of the exact layout as the message array sent from n1.

That is not how Node-RED works. Node-RED delivers each message as a separate input event on the receiving node.

You are also mixing up "sending multiple messages to one output" and "sending messages on each output".

If you call node.send([msg1, msg2, msg3]) then Node-RED will send msg1 on the first output, msg2 on the second output and msg3 on the third output.

If you call node.send([ [msg1, msg2, msg3] ]), then Node-RED will send all three messages on the first output - but they are still sent as individual input events.

In you code, you are doing the first of these two - sending a message to each output. But you test flow only has one output connected -the n1.wires property only has one element, so it only has one output.

If you want to test multiple output ports, then you'd need to create a helper node connected to each of the outputs:

let flow = [
  { id: 'n1', type: 'myNode', wires: [['output1'], ['output2'], ['output3']] },
  { id: 'output1', type: 'helper', wires: [[]] },
  { id: 'output2', type: 'helper', wires: [[]] },
  { id: 'output3', type: 'helper', wires: [[]] }
];

Your test then has to be a bit smarter about what it does inside the input event handlers - as you now have to wait for all three handlers to fire before ending the test.