1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Update Function catch tests to handle async receive

This commit is contained in:
Nick O'Leary 2019-08-21 11:39:34 +01:00
parent b734097d16
commit a17325f028
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 55 additions and 49 deletions

View File

@ -282,13 +282,13 @@ module.exports = function(RED) {
if (!errorMessage) { if (!errorMessage) {
errorMessage = err.toString(); errorMessage = err.toString();
} }
this.error(errorMessage, msg); done(errorMessage);
} }
else if (typeof err === "string") { else if (typeof err === "string") {
this.error(err, msg); done(err);
} }
else { else {
this.error(JSON.stringify(err), msg); done(JSON.stringify(err));
} }
} }
}); });

View File

@ -1412,21 +1412,23 @@ describe('function node', function() {
helper.load(functionNode, flow, function () { helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"}); n1.receive({payload: "foo", topic: "bar"});
try { setTimeout(function() {
helper.log().called.should.be.true(); try {
var logEvents = helper.log().args.filter(function (evt) { helper.log().called.should.be.true();
return evt[0].type == "function"; var logEvents = helper.log().args.filter(function (evt) {
}); return evt[0].type == "function";
logEvents.should.have.length(1); });
var msg = logEvents[0][0]; logEvents.should.have.length(1);
msg.should.have.property('level', helper.log().ERROR); var msg = logEvents[0][0];
msg.should.have.property('id', 'n1'); msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('type', 'function'); msg.should.have.property('id', 'n1');
msg.should.have.property('msg', 'small mistake'); msg.should.have.property('type', 'function');
done(); msg.should.have.property('msg', 'small mistake');
} catch (err) { done();
done(err); } catch (err) {
} done(err);
}
},50);
}); });
}); });
it('should catch thrown number', function (done) { it('should catch thrown number', function (done) {
@ -1434,43 +1436,47 @@ describe('function node', function() {
helper.load(functionNode, flow, function () { helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"}); n1.receive({payload: "foo", topic: "bar"});
try { setTimeout(function() {
helper.log().called.should.be.true(); try {
var logEvents = helper.log().args.filter(function (evt) { helper.log().called.should.be.true();
return evt[0].type == "function"; var logEvents = helper.log().args.filter(function (evt) {
}); return evt[0].type == "function";
logEvents.should.have.length(1); });
var msg = logEvents[0][0]; logEvents.should.have.length(1);
msg.should.have.property('level', helper.log().ERROR); var msg = logEvents[0][0];
msg.should.have.property('id', 'n1'); msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('type', 'function'); msg.should.have.property('id', 'n1');
msg.should.have.property('msg', '99'); msg.should.have.property('type', 'function');
done(); msg.should.have.property('msg', '99');
} catch (err) { done();
done(err); } catch (err) {
} done(err);
}
},50);
}); });
}); });
it('should catch thrown object (bad practise)', function (done) { it('should catch thrown object (bad practice)', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "throw {a:1};"}]; var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "throw {a:1};"}];
helper.load(functionNode, flow, function () { helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"}); n1.receive({payload: "foo", topic: "bar"});
try { setTimeout(function() {
helper.log().called.should.be.true(); try {
var logEvents = helper.log().args.filter(function (evt) { helper.log().called.should.be.true();
return evt[0].type == "function"; var logEvents = helper.log().args.filter(function (evt) {
}); return evt[0].type == "function";
logEvents.should.have.length(1); });
var msg = logEvents[0][0]; logEvents.should.have.length(1);
msg.should.have.property('level', helper.log().ERROR); var msg = logEvents[0][0];
msg.should.have.property('id', 'n1'); msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('type', 'function'); msg.should.have.property('id', 'n1');
msg.should.have.property('msg', '{"a":1}'); msg.should.have.property('type', 'function');
done(); msg.should.have.property('msg', '{"a":1}');
} catch (err) { done();
done(err); } catch (err) {
} done(err);
}
},50);
}); });
}); });
}); });