node-red / node-red-node-test-helper

A test framework for Node-RED nodes
Apache License 2.0
57 stars 41 forks source link

Errors in function node code swallowed during testing -- causes test timeout #52

Open zachsitka opened 3 years ago

zachsitka commented 3 years ago

Errors in function node code are swallowed and cause timeout error in test instead of fail the test.

Since the tests are there to be able to help find and diagnose these issues, these errors would be more helpful if they were thrown up to the test code and handled there. This also will slow down any tests that are running, since they will hit timeout in order to fail when they could instead fail as soon as the error is hit.

Example test code below šŸ‘‡

const should = require("should");
const helper = require("node-red-node-test-helper");

const flow = [
    {
        "id": "2487e9dbea5d61b6",
        "type": "helper",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "wires": []
    },
    {
        "id": "f5903684.b7a698",
        "type": "function",
        "name": "inputs",
        // Here is where the error is hit in the function -- should be object undefined error
        "func": "msg.count = msg.property_does_not_exist.array.length;"+
                "return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "wires": [
            [
                "2487e9dbea5d61b6"
            ]
        ]
    }
]

const requiredNodes = [
    require("../../node_modules/@node-red/nodes/core/function/10-function")
]

helper.init(require.resolve("node-red"));

describe("error test", function() {
    before(function(done) {
        helper.startServer(done);
    });

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

    after(function(done) {
        helper.stopServer(done);
    });

    // This currently fails because you cannot test a function node that sets the msg.payload value for some reason
    it.only('should not time out', function(done) {
        helper.load(requiredNodes, flow, function() {
            try {
                var debugNode = helper.getNode("2487e9dbea5d61b6");
                var inputFunctionNode = helper.getNode("f5903684.b7a698");

                debugNode.on("input", function(msg) {
                    try {
                        should(1).equal(1);
                        done();
                    } catch (err) {
                        done(err);
                    }
                });

                inputFunctionNode.receive({ payload: 'test payload' });
            } catch (err) {
                done(err);
            }
        });
    });
});