How can I unit test created UI Dashboard widget node?

Hi, :blush:

I have create new UI Dashboard widget node. But I have some problem about unit test.

In my new UI Dashboard widget node main function, there are require checked about node-red-dashboard. See below,

function UiWidgetThermometer(config) {
        let node = this;
        let done = null;
        try {
            if (ui === undefined) {
                ui = RED.require("node-red-dashboard")(RED);     <----------- THIS
            }
            RED.nodes.createNode(this, config);
            .....
            ......

Below code is my unit test, when I run it, there is an error occured (not pass). I had checked n1 object, result n1 equals 'null'.

const should = require("should");
const helper = require("node-red-node-test-helper");
const nodeUiWidgetThermometer = require("../nodes/ui_widget_thermometer.js");

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

describe('UI Widget Temperature Node', function () {

    beforeEach((done) => {
        helper.startServer(done);
    });

    afterEach((done) => {
        helper.unload();
        helper.stopServer(done);
    });

    it('should be loaded', (done) => {
        const flow = [
            {
                id: "n1",
                type: "ui_widget_thermometer",
                name: "test_node"
            }
        ];
        helper.load(nodeUiWidgetThermometer, flow, () => {
            const n1 = helper.getNode("n1");          <-------- CHECKED n1 = null 
            try {
                n1.should.have.property("name", "test_node");
                done();
            } catch (err) {
                done(err);
            }
        });
    });
});

But If I remove below require node-red-dashboard section in my UI widget code. And then I run unit test again, the result is pass.

if (ui === undefined) {
     ui = RED.require("node-red-dashboard")(RED);
}

So, how can I unit test created UI Dashboard widget node? :thinking:

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