node-red-node-test-helper
node-red-node-test-helper copied to clipboard
.send spy common across all nodes
Problem Summary
I'm trying to write tests for the new Dashboard 2.0. In doing so, I have a passthru option, which when disabled, does not send a message on to any connected nodes.
As such, I needed to write a test that check the sinon.spy() on node.send() did not run. However, it does.
Upon investigation with @knolleary, we discovered that all nodes in the helper share a single spy on the respective .send(), this means that any node sending a message would trigger this spy, thus making it impossible to register whether or not the node.send() was not run for a given node we cared about.
Temporary Workaround
In order to circumnavigate the issue, we have used the following pattern utilising setTimeouts and.a helper-node:
- Wire the node we want to watch to a
helper-node(Helper 1) - Add a
completenode with ascopeset the node id of the node we care about - Wire a
helper-node(Helper 2) after thecompletenode as we can't monitoron('input')for `complete nodes. - Define a
msgSent = falsein the scope of the test - Add an
on('input')for Helper 2 and within it, set the variablemsgSenttotrue - Add an
on('input')for thehelpernode after thecomplete.- Inside this event handler, set a timeout (e.g. 50ms) to ensure that Helper 2 handles logic first, then check that
msgSentistrue/false, depending on the desired outcome of the test
- Inside this event handler, set a timeout (e.g. 50ms) to ensure that Helper 2 handles logic first, then check that