Unit Test is failing for custom node having flow Context and Ajax calls

Hi All,

My custom node sets data to flow context and also make Ajax calls to rest API's. Basic unit tests for initializing node and and receiving messages are passing but test for context and testing complete node run fails with timeout error. Execution of node stops particularly at line where it encounters flow context in a node. I am referencing change node for context unit test but it is not working for me. Below is my unit test which is failing:

const helper = require("node-red-node-test-helper");
const ingestionNode = require("../../dmax-ingestion/ingestion");
var Context = require("@node-red/runtime/lib/nodes/context");
helper.init(require.resolve('node-red'), {
    functionGlobalContext: { os: require('os') }
});

describe('ingestion Node', function () {
    const flow = [
        {
            "id": "9cf251bd159e1bf8",
            "type": "tab",
            "label": "Flow 1",
            "disabled": false,
            "info": "",
            "env": []
        },
        {
            id: 'ingestiontestNode',
            type: 'ingestion',
            name: 'test automated ingestion',
            ingestionType: 'clear',
            source: '162',
            sourceName: 'node-red-test/xyz',
            noOfRespToWaitFor: 1,
            noOfResponsesReceived: [],
            "wires": [["helpertestNode"]],
            nodeSend: "node.send(msg);"
        },
        { id: 'helpertestNode', type: 'helper' },
    ];
    function initContext(done) {
        Context.init({
            contextStorage: {
                memory0: {
                    module: "memory"
                },
                memory1: {
                    module: "memory"
                }
            }
        });
        Context.load().then(function () {
            done();
        });
    }
    beforeEach(function (done) {
        process.env.API_BASE_URL = "http://<serverip>5000/api";
        process.env.API_SSO_BASE_URL = "<domain name>/backend/api";
        process.env.API_USER = "appUser";
process.env.API_PASSWORD = "xyz";

        helper.startServer(done);
    });

    afterEach(function (done) {
        helper.unload().then(function () {
            return Context.clean({ allNodes: {} });
        }).then(function () {
            return Context.close();
        }).then(function () {
            RED.settings.nodeMessageBufferMaxLength = 0;
            helper.stopServer(done);
        });
    });
    it('should be able to set/get the value of flow context property', function (done) {
        helper.load(ingestionNode, flow, function () {
            var ingNd1 = helper.getNode("ingestiontestNode");
            var helper1 = helper.getNode("helperNode1");
            helper1.on("input", function (msg) {
                try {
                    console.log(ingNd1.context().flow.get("INGESTION_SOURCE_ID"));
                    ingNd1.context().flow.get("INGESTION_SOURCE_ID").should.equal('test value');
                    done();
                } catch (err) {
                    done(err);
                }
            });
            ingNd1.context().flow.set("INGESTION_SOURCE_ID", "test value");
            ingNd1.receive({ payload: "" });
        });
    });

    it('should run ingestion node in standalone mode', function (done) {
        helper.load(ingestionNode, flow, function () {
            var n1 = helper.getNode('ingestiontestNode');
            var n2 = helper.getNode('helpertestNode');

            n2.on('input', function (msg) {
                try {
                    msg.should.have.property('status', 'Success');
                    msg.should.have.property('topic', 'ingestion');
                    n1.status.should.have.value('Success');
                    done();
                } catch (err) {
                    done(err);
                }
            });

            n1.receive({ "_msgid": Math.random(), "topic": "ingestion" });
        });
    });
});

Both tests above are failing with timeout error. I am not sure about the root cause.
Error:

  1) ingestion Node
       should be able to set/get the value of flow context property:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\Node-Red\nodered-palette\test\custom-nodes\dmax-ingestion_spec.js)
      at listOnTimeout (node:internal/timers:557:17)
      at processTimers (node:internal/timers:500:7)

Any help is appreciated. Thanks

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.