node-red-node-test-helper icon indicating copy to clipboard operation
node-red-node-test-helper copied to clipboard

.send spy common across all nodes

Open joepavitt opened this issue 2 years ago • 0 comments

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 complete node with a scope set the node id of the node we care about
  • Wire a helper-node (Helper 2) after the complete node as we can't monitor on('input') for `complete nodes.
  • Define a msgSent = false in the scope of the test
  • Add an on('input') for Helper 2 and within it, set the variable msgSent to true
  • Add an on('input') for the helper node after the complete.
    • Inside this event handler, set a timeout (e.g. 50ms) to ensure that Helper 2 handles logic first, then check that msgSent is true/false, depending on the desired outcome of the test

joepavitt avatar Sep 08 '23 12:09 joepavitt