From d46531def82ea60a16e330f38b9f830cab566d86 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sat, 2 Nov 2019 11:27:08 +0000 Subject: [PATCH] add unit tests for encode Object changes. --- test/unit/@node-red/util/lib/util_spec.js | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/unit/@node-red/util/lib/util_spec.js b/test/unit/@node-red/util/lib/util_spec.js index 17304e811..603f4524c 100644 --- a/test/unit/@node-red/util/lib/util_spec.js +++ b/test/unit/@node-red/util/lib/util_spec.js @@ -772,6 +772,57 @@ describe("@node-red/util/util", function() { var resultJson = JSON.parse(result.msg); resultJson.socket.should.eql('[internal]'); }); + it('object which fails to serialise', function(done) { + var msg = { + msg: { + obj:{ + cantserialise:{ + message:'this will not be displayed', + toJSON: function(val) { + throw 'this exception should have been caught'; + return 'should not display because we threw first'; + }, + }, + canserialise:{ + message:'this should be displayed', + } + }, + } + }; + var result = util.encodeObject(msg); + result.format.should.eql("error"); + var success = (result.msg.indexOf('cantserialise') > 0); + success &= (result.msg.indexOf('this exception should have been caught') > 0); + success &= (result.msg.indexOf('canserialise') > 0); + success.should.eql(1); + done(); + }); + it('object which fails to serialise - different error type', function(done) { + var msg = { + msg: { + obj:{ + cantserialise:{ + message:'this will not be displayed', + toJSON: function(val) { + throw new Error('this exception should have been caught'); + return 'should not display because we threw first'; + }, + }, + canserialise:{ + message:'this should be displayed', + } + }, + } + }; + var result = util.encodeObject(msg); + result.format.should.eql("error"); + var success = (result.msg.indexOf('cantserialise') > 0); + success &= (result.msg.indexOf('this exception should have been caught') > 0); + success &= (result.msg.indexOf('canserialise') > 0); + success.should.eql(1); + done(); + }); + }); }); });