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) {
errorMessage = err.toString();
}
this.error(errorMessage, msg);
done(errorMessage);
}
else if (typeof err === "string") {
this.error(err, msg);
done(err);
}
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 () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', 'small mistake');
done();
} catch (err) {
done(err);
}
setTimeout(function() {
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', 'small mistake');
done();
} catch (err) {
done(err);
}
},50);
});
});
it('should catch thrown number', function (done) {
@ -1434,43 +1436,47 @@ describe('function node', function() {
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', '99');
done();
} catch (err) {
done(err);
}
setTimeout(function() {
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', '99');
done();
} 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};"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', '{"a":1}');
done();
} catch (err) {
done(err);
}
setTimeout(function() {
try {
helper.log().called.should.be.true();
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', '{"a":1}');
done();
} catch (err) {
done(err);
}
},50);
});
});
});