How can I initial credential in my created node for node-red-node-test-helper?

Hi all,

I have some problem about initial credential in my created node for node-red-node-test-helper.

i try to follow node-red-node-test-helper help, node-red/node-red-node-test-helper: A test framework for Node-RED nodes (github.com). But it had remain error.

The error is from my checking in .js file in my created node. If it has not input the credentials, my created node status return -1 value.

2022-05-16_152048

Below code is the credentials in node registerType in html file ...

....
credentials: {
    token: {
        type: "text",
        required: true
    }
},
....

Below is the unit test

I am not sure, I initiate correct credentials value or not? or Where is my mistake in my code? :thinking:

Thank you in advance.

You need to include the credentials in the flow itself:

const flow = [
   {
      id: "n1",
      type: "node-line-notify",
      ...
      credentials: {
         token: "test"
      }
   }
]

Thank you very much for you help. :blush:

But the error has remain.

Below is my full unit test file.

const should = require("should");
const helper = require("node-red-node-test-helper");
const nodeLineNotify = require("../nodes/node-line-notify/node-line-notify.js");

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

describe('Line Notify Node', function () {

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

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

    it('should be loaded', (done) => {
        const flow = [{ id: "n1", type: "node-line-notify", name: "node-line-notify", useExternalData: true }];
        helper.load(nodeLineNotify, flow, () => {
            const n1 = helper.getNode("n1");
            try {
                n1.should.have.property("name", "node-line-notify");
                done();
            } catch (err) {
                done(err);
            }
        });
    });

    it("should make payload without token", (done) => {
        const flow = [
            {
                id: "n1",
                type: "node-line-notify",
                name: "node-line-notify",
                message: "test",
                useExternalData: false
            }
        ];
        helper.load(nodeLineNotify, flow, () => {
            const n1 = helper.getNode("n1");
            n1.on("call:error", (err) => {
                should.equal(err.lastArg.payload, "node-line-notify.errors.notFoundToken");
                should.equal(err.lastArg.status, -1);
                done();
            });
            n1.receive({ payload: "test" });
        });
    });

    it("error token", (done) => {
        const flow = [
            {
                id: "n1",
                type: "node-line-notify",
                name: "node-line-notify",
                message: "test",
                useExternalData: false,
                useImageUrl: false,
                useImageFile: false,
                useSticker: false,
                credentials: {
                    token: "test"
                }
            }
        ];
        helper.load(nodeLineNotify, flow, () => {
            const n1 = helper.getNode("n1");
            n1.on("call:error", (err) => {
                console.log(err);
                should.equal(err.lastArg.status, 401);
                should.equal(err.lastArg.payload, "Invalid access token");
                done();
            });
            n1.receive({ payload: "test" });
        });
    });
});

I use console.log for display 'err' object. It does not have values credential object. (I am not sure, can I use console.log for debugging)

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