Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing fails in the case of custom node generated from subflow #57

Open
4 tasks done
kazuhitoyokoi opened this issue Nov 20, 2022 · 1 comment
Open
4 tasks done

Comments

@kazuhitoyokoi
Copy link
Member

kazuhitoyokoi commented Nov 20, 2022

I have tried to create test cases for custom nodes generated from a subflow but the test using helper module fails. In this issue, I wrote the example case of the error using a lower-case node created from subflow which contains a function node to use msg.payload.toLowerCase();.

What are the steps to reproduce?

Prepare the following files under the node-red-contrib-example-lower-case directory. And then, execute the npm install and npm test commands.

var helper = require("node-red-node-test-helper");
var lowerNode = require("../lower-case.js");

describe('lower-case Node', function () {

  afterEach(function () {
    helper.unload();
  });

  it('should be loaded', function (done) {
    var flow = [{ id: "n1", type: "lower-case", name: "test name" }];
    helper.load(lowerNode, flow, function () {
      var n1 = helper.getNode("n1");
      n1.should.have.property('name', 'test name');
      done();
    });
  });

  it('should make payload lower case', function (done) {
    var flow = [{ id: "n1", type: "lower-case", name: "test name",wires:[["n2"]] },
    { id: "n2", type: "helper" }];
    helper.load(lowerNode, flow, function () {
      var n2 = helper.getNode("n2");
      var n1 = helper.getNode("n1");
      n2.on("input", function (msg) {
        msg.should.have.property('payload', 'uppercase');
        done();
      });
      n1.receive({ payload: "UpperCase" });
    });
  });
});
  • package.json
{
  "name": "node-red-contrib-example-lower-case",
  "scripts": {
    "test": "mocha \"test/**/*_spec.js\""
  },
  "node-red": {
    "nodes": {
      "lower-case": "lower-case.js"
    }
  },
  "dependencies": {
    "mocha": "10.1.0",
    "node-red": "3.0.2",
    "node-red-node-test-helper": "0.3.0"
  }
}
  • lower-case.js
module.exports = function (RED) {
    const fs = require('fs');
    const path = require("path");
    const file = path.join(__dirname, "subflow.json");
    const text = fs.readFileSync(file);
    const flow = JSON.parse(text);
    RED.nodes.registerSubflow(flow);
}
  • subflow.json
{
    "id": "277d18ee2b9e83c9",
    "type": "subflow",
    "name": "lower-case",
    "info": "",
    "category": "",
    "in": [
        {
            "x": 60,
            "y": 80,
            "wires": [
                {
                    "id": "5b296b72cfed90ed"
                }
            ]
        }
    ],
    "out": [
        {
            "x": 340,
            "y": 80,
            "wires": [
                {
                    "id": "5b296b72cfed90ed",
                    "port": 0
                }
            ]
        }
    ],
    "env": [],
    "meta": {
        "module": "node-red-contrib-example-lower-case",
        "type": "lower-case",
        "version": "1.0.0",
        "license": "Apache-2.0"
    },
    "color": "#DDAA99",
    "flow": [
        {
            "id": "5b296b72cfed90ed",
            "type": "function",
            "z": "277d18ee2b9e83c9",
            "name": "function 1",
            "func": "msg.payload = msg.payload.toLowerCase();\nnode.send(msg);",
            "outputs": 1,
            "noerr": 0,
            "initialize": "",
            "finalize": "",
            "libs": [],
            "x": 200,
            "y": 80,
            "wires": [
                []
            ]
        },
        {
            "id": "a10f2fbc4615646b",
            "type": "subflow:277d18ee2b9e83c9",
            "z": "07d1ca735eae83bd",
            "name": "",
            "x": 410,
            "y": 120,
            "wires": [
                [
                    "8c055b9e31ec3702"
                ]
            ]
        }
    ]
}

What happens?

The npm test command outputs the timeout error.

$ npm test

> test
> mocha "test/**/*_spec.js"



  lower-case Node
    1) should be loaded
    2) should make payload lower case


  0 passing (4s)
  2 failing

  1) lower-case Node
       should be loaded:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/kazuhitoyokoi/Desktop/node-red-contrib-example-lower-case/test/lower-case_spec.js)
      at listOnTimeout (node:internal/timers:564:17)
      at process.processTimers (node:internal/timers:507:7)

  2) lower-case Node
       should make payload lower case:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/kazuhitoyokoi/Desktop/node-red-contrib-example-lower-case/test/lower-case_spec.js)
      at listOnTimeout (node:internal/timers:564:17)
      at process.processTimers (node:internal/timers:507:7)

What do you expect to happen?

The npm test command passes the test cases. The following is the output of a custom node created by general node development.

$ npm test

> test
> mocha "test/**/*_spec.js"



  lower-case Node
    ✔ should be loaded
    ✔ should make payload lower case


  2 passing (10ms)

Please tell us about your environment:

  • Node-RED version: v3.0.2
  • Node.js version: v18.7.0
  • npm version: v8.15.0
  • Platform/OS: macOS Venture 13.0.1, MacBook Air M1, 2020
@naoyaikeda
Copy link

I think what makes this project difficult to determine is the common error of timeout errors in a fairly wide range of cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants