Node-red-node-test issues, getting output from node

Trying to automate my testing, with "npm test" scripts, github and Travis-ci. Works fine for the most part, but I somehow cannot get the flow to work and catch output from the verisurenode, that is supposed to fetch data from verisure apis. I'm using mocha to intercept and answer the api calls. I can confirm that the node receives an incoming message, but troubleshooting after that is hard. Bit of newbie on the testing part...

it('should fetch status', function (done) {
    // setup intercepts
    nock(netScope)
      .get('/xbn/2/installation/search?email=' + verEmail)
      .replyWithFile(200, `${__dirname}/sites_reply.json`);
    nock(netScope)
      .get('/xbn/2/cookie')
      .replyWithFile(200, `${__dirname}/token_reply.xml`);
    nock(netScope)
      .get('/xbn/2/installation/123456789/overview')
      .replyWithFile(200, `${__dirname}/test_site.json`);
    // define flow to test
    var flow = [{ 'id': 'n1', 'type': 'VerisureAlarmNode', 'z': 'f1', 'name': 'Verisure Alarm', 'user': 'nc', 'x': 240, 'y': 240, 'wires': [ ['nh'] ] },
      { 'id': 'nc', 'type': 'VerisureConfig', 'z': 'f1', 'displayName': 'Verisure Site', 'siteName': 'Home', 'username': verEmail, 'password': verPassword },
      { id: 'nh', type: 'helper', 'z': 'f1' },
      { id: 'f1', type: 'tab', label: 'Test flow' }
    ];
    // console.log(helper.log());
    helper.load([sureNode, confNode], flow, function () {
      var n1 = helper.getNode('n1');
      var nc = helper.getNode('nc');
      var nh = helper.getNode('nh');
      nc.should.have.property('username', verEmail);
      n1.on('call:warn', function (msg) {
        console.log('warning N1: ' + msg);
      });
      nh.on('input', function (msg) {
        msg.payload.should.be('{"currentStatus":"DISARMED","changed":"false","date":"2018-11-08T06:54:28.000Z","name":"Kristian"}');
        console.log('Test message received NH: ' + msg);
        done();
      });
      n1.on('input', function (msg) {
        console.log('Test message received N1: ' + msg.payload);
      });
      n1.receive({ payload: 'test' });
      done();
    }); // should fetch status

No one with experience in using test-helper? Seems now my main issue that using .emit() doesnt actually make anything being sent on to the next node...

Hi @ksvan

Test helper is used extensively by the core nodes, and has evolved as the testing requirements of those nodes have evolved. But it isn't used much (yet) out side of that. So there is limited experience in the community for using it.

The best I can suggest is to follow the examples in the core node tests.

When you stay using .emit doesn't do anything... Can you elaborate? I don't see it being used in you code here.

You are calling done() before the test is finished, as the previous functions are asynchronous. You should probably move it to the last line of the last on-input function.

Thanks to both of you for pitching in. Truly believe this is something we should build more experience and share in the community.

Been trying lots of different angles on this. especially including having done() placed right:) Have been through lots of the core tests as well, and after tuning and learning, it seems my issue is that the config node. It doesnt seem to be configured right with credentials however I try it, and then my main node stops early due to lacking credentials. And for some reason I cannot seem to get spying on warn/debug/error to work like the examples, so bit hard to debug.

So this is one of most recent tries:

    var flow = [
    { 'id': 'n1', 'type': 'VerisureAlarmNode', 'z': 'f1', 'name': 'Verisure Alarm', 'user': 'nc', 'x': 240, 'y': 240, 'wires': [['nh']] },
    { 'id': 'nc', 'type': 'VerisureConfig', 'z': 'f1', 'displayName': 'Verisure Site', 'siteName': 'Home', 'credentials': { 'username': verEmail, 'password': verPassword } },
    // { id: 'nh', type: 'debug', 'z': 'f1', 'active': 'true', 'complete': 'payload', 'console': 'true', 'wires': '[]', 'tostatus': 'false', 'tosidebar': 'false' },
    { id: 'nh', type: 'helper', 'z': 'f1' },
    { id: 'f1', type: 'tab', label: 'Test flow' }
  ];

  it('should output alarmstatus', function (done) {
    helper.load([sureNode, confNode], flow, function () {
      var nh = helper.getNode('nh');
      var n1 = helper.getNode('n1');
      var nc = helper.getNode('nc');
      nc.credentials.username = verEmail;
      nc.credentials.username = verPassword;
      nh.on('input', function (msg) {
        console.log('!! test');
        msg.payload.should.be('{"currentStatus":"DISARMED","changed":"false","date":"2018-11-08T06:54:28.000Z","name":"Kristian"}');
        done();
      });
      n1.on('call:warn', call => {
        console.log('WARN' + call);
      });
      n1.on('call:error', call => {
        console.log('ERROR:' + call);
      });
      n1.receive('input', { payload: 'test' });
    });
  }); // IT end

Try adding your credentials like this:

var credentials = {nc: {'username': verEmail, 'password': verPassword}};
helper.load([sureNode, confNode], flow, credentials, function () {
2 Likes

Awsome, that seems to work. Now onto making Nock work in this test as well:)

Hi,

I can't figure this out. I'm trying to create a unit test using the test helper that passes a set of test credential values.

const test_credentials = { 'latitude': "51.501364", 'longitude': '-0.1440787' }
const flow = [{ id: "n1", type: "sun-events", name: "test name", wires: [["n2"]] }, { id: "n2", type: "helper" }];
helper.load(SunEventsNode, flow, test_credentials, function () {

When I try to access the credentials values they are undefined.

I found this post and changed the definition to

const test_credentials = { nc: {'latitude': "51.501364", 'longitude': '-0.1440787' } }

But I'm still getting undefined values. What does "nc" refer to in this example? I've been trawling through the node-red core source but it's like looking for a needle in a hasystack. Any suggestion how I can get this test working?

I ran my units tests in the Visual Studio javascript debugger and in a moment of inspiration I realised that the "nc" in the earlier example about was actually the name of the node in test flow. I changed the key in the javascript object to "n1" as per the names in my test flow and it now works perfectly.

const test_credentials = { n1: {latitude: "51.501364", longitude: "-0.1440787" } }

Thanks!