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


#1

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

#2

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...


#3

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.


#4

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.


#5

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.


#6

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

#7

Try adding your credentials like this:

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

#8

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