Mocha, npm test - Error: Timeout of 2000ms exceeded

I'm coming to grips with npm test (mocha) with my node: node-red-contrib-mytimeout (github dev-test branch). Since I'm just starting I'm learning about mocha and building tests. There's a lot that I don't understand. For instance

I have a test case that sends an "on" ( '{ "payload": "on" }' ) but I keep getting.

  1) mytimeout Node 2
       Should turn on:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/njc/dev/git/node-red-contrib-mytimeout/t/dev-test/test/basic2_spec.js)

The node mytimeout has gotten a bit complex but basically in it's default state if I send an "on" payload, it starts the time and it will send an "on" to the first output, 25 seconds later it send "warning" and 5 seconds "off".

https://github.com/linuxha/node-red-contrib-mytimeout/blob/feature/dev-test/test/basic2_spec.js.

  /*
  ** Main output
  ** n2: {"_msgid":"65d8f152.8e917","payload":"on","topic":"","timeout":30}
  ** Ticks output
  ** n3: {"payload":30,"state":1,"flag":"ticks > 0","_msgid":"5e5dd4bf.1be32c"}
  */
  it('Should turn on', function (done) {
    var flow = [
      { id: "n1", type: "mytimeout", name: nom, wires:[["n2"]] },
      { id: "n2", type: "helper" }
    ];
    helper.load(myNode, flow, function () {
      var n2 = helper.getNode("n2");
      var n1 = helper.getNode("n1");

      n2.on("input", function (msg) {
        console.log("NJC: msg.payload = " + msg.payload);
        console.log("NJC: msg = " + JSON.stringify(msg));
        msg.should.have.property('payload', 'on');
        done();
      });
      n1.receive({ payload: 'on' });
    });
  });

I gave the issue a lot of thought and tried to figure out why a timer wouldn't run. The only thing I came up with is that the timer's timeout wasn't set. I updated this to the n1 id in the flow from above and I can now run the test case.

  it('Should turn on', function (done) {
    var flow = [
      { id: "n1", type: "mytimeout", name: nom,
        outsafe: "on",
        outwarning: "warning",
        outunsafe: "off",
        warning: "5",
        timer: "30",
        debug: "0",
        wires:[["n2"]] },
      { id: "n2", type: "helper" }
    ];
    helper.load(myNode, flow, function () {
      var n2 = helper.getNode("n2");
      var n1 = helper.getNode("n1");

      n2.on("input", function (msg) {
        msg.should.have.property('payload', 'on');
        done();
      });
      n1.receive({ payload: 'on' });
    });
  });

I'm a bit at a loss why the default settings aren't set when run in the test. Will experiment further.

I've figured out why the default settings aren't getting set. They're in the html config. I'll add the settings to the test in the flows.

I'm now abusing the should library. I know everyone says keep it simple stupid but I'm not unit testing I'm doing functionality testing of the node. And this mytimeout black box is rather complex sending out multiple outputs at different intervals. I'm writing down the requirements, adding notes and rewriting my test cases for mocha/should and npm. This is a huge step up as I can now actually test the outputs of cmds (output 1) and ticks (output 2) and make sure they really are as expected. :slight_smile:

1 Like