Hi,
recently I started writing unit tests for my Node-RED nodes. I'm using node-red-node-test-helper and already implemented a couple of tests which work just fine.
But now I got stuck implementing a test which involves sending a message from my node to the helper node. I just cannot get it working, the callback function for the "input" event is never called. But I'm sure that my node sends a message during the test. I verified this by instrumenting the code and saw that the send
function is called. Additionally I faked the test to pass and measured the coverage and also here I saw that the send
function was called.
The code of the node under test is available on GitHub:
This is my test code:
const sinon = require("sinon");
const helper = require("node-red-node-test-helper");
const configNode = require("../nodes/config.js");
const schedulerNode = require("../nodes/scheduler.js");
const chronos = require("../nodes/common/chronos.js");
const moment = require("moment");
describe("scheduler node", function()
{
context("node timers", function()
{
let clock = null;
before(function(done)
{
helper.startServer(done);
});
after(function(done)
{
helper.stopServer(done);
});
beforeEach(function()
{
clock = sinon.useFakeTimers();
sinon.stub(chronos, "getCurrentTime").returns(moment().utc());
});
afterEach(function()
{
helper.unload();
sinon.restore();
clock.restore();
});
it("should trigger at specified time", async function(done)
{
const flow = [{id: "sn1", type: "chronos-scheduler", name: "scheduler", config: "cn1", wires: [["hn1"]], schedule: [{trigger: {type: "time", value: "00:01", offset: 0, random: false}, output: {type: "msg", property: {name: "payload", type: "string", value: "test"}}}], outputs: 1},
{id: "hn1", type: "helper"},
{id: "cn1", type: "chronos-config", name: "config"}];
const credentials = {"cn1": {latitude: "50", longitude: "10"}};
await helper.load([schedulerNode, configNode], flow, credentials);
const hn1 = helper.getNode("hn1");
hn1.on("input", function(msg)
{
try
{
msg.should.have.property("payload", "test");
done();
}
catch(e)
{
done(e);
}
});
clock.tick(60000);
});
});
});
I always get this error from mocha as the done
function is never called:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Any idea what I'm doing wrong and how I can get this working?
Best regards,
Jens