Unit testing can't load node in my it block?

hi guys,

I've node-red installed globally and from my custom node project i've installed

npm install node-red-node-test-helper node-red --save-dev

test fail when i run npm test from my custom node project.

 should be loaded:
     TypeError: Cannot read property 'should' of null

this is my test file

var should = require("should");
var helper = require("node-red-node-test-helper");
var mycsnode = require("../mycustom");

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


describe('mycsnode Node', function () {

    beforeEach(function (done) {
        helper.startServer(done);
    });
  
    afterEach(function (done) {
        helper.unload().then(function() {
            helper.stopServer(done);
        });
    });

    it('should be loaded', function (done) {
        var flow = [{ id: "n1", type: "mycsnode", name: "test name" }];

        helper.load(mycsnode, flow, function () {
          var n1 = helper.getNode("n1");
          console.log('n1---', n1);
          try {
            n1.should.have.property('name', 'test name');
            done();
          } catch(err) {
            done(err);
          }
        });
      });


});

what does this line ↑ show in console?

hi thanks for your reply.
this line show n1--- null

thats why your test fails.

Can you confirm that your custom node ../mycustom actually runs in a node-red install?

if it does, then add one to your flow & export it to clip board & use that as your n1 flow value.

You could also add (inside the try, 1st line) should(n).be.Object(); to be more specific

1 Like

test pass when i export my config to clip board as my n1 value.
thank you guy

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