From 03d9e28db9b4d047d35ef5700dec84cd25399766 Mon Sep 17 00:00:00 2001 From: Martin Rubli Date: Sat, 5 Sep 2020 12:04:29 +0800 Subject: [PATCH 1/3] examples: lower-case_spec.js: Allow proper assertion failure reporting (#45) The exceptions raised by assertion failures get swallowed by the Node-RED internals, so catch them explicitly and pass them to done() as suggested in https://github.com/node-red/node-red-node-test-helper/issues/45. --- examples/lower-case_spec.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/lower-case_spec.js b/examples/lower-case_spec.js index 39dc55e..5e32a30 100644 --- a/examples/lower-case_spec.js +++ b/examples/lower-case_spec.js @@ -13,8 +13,12 @@ describe('lower-case Node', function () { var flow = [{ id: "n1", type: "lower-case", name: "lower-case" }]; helper.load(lowerNode, flow, function () { var n1 = helper.getNode("n1"); - n1.should.have.property('name', 'lower-case'); - done(); + try { + n1.should.have.property('name', 'lower-case'); + done(); + } catch(err) { + done(err); + } }); }); @@ -22,8 +26,12 @@ describe('lower-case Node', function () { var flow = [{"id":"3912a37a.c3818c","type":"lower-case","z":"e316ac4b.c85a2","name":"lower-case","x":240,"y":320,"wires":[[]]}]; helper.load(lowerNode, flow, function () { var n1 = helper.getNode("3912a37a.c3818c"); - n1.should.have.property('name', 'lower-case'); - done(); + try { + n1.should.have.property('name', 'lower-case'); + done(); + } catch(err) { + done(err); + } }); }); @@ -36,8 +44,12 @@ describe('lower-case Node', function () { var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1"); n2.on("input", function (msg) { - msg.should.have.property('payload', 'uppercase'); - done(); + try { + msg.should.have.property('payload', 'uppercase'); + done(); + } catch(err) { + done(err); + } }); n1.receive({ payload: "UpperCase" }); }); From d2bd20cd419e39de3f6f40b4861c2a0b8361abab Mon Sep 17 00:00:00 2001 From: Martin Rubli Date: Sat, 5 Sep 2020 12:17:20 +0800 Subject: [PATCH 2/3] README.md: Update example unit test to report assertion failures Also see the previous commit and https://github.com/node-red/node-red-node-test-helper/issues/45. --- README.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 77e2aed..2b17b72 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,12 @@ describe('lower-case Node', function () { var flow = [{ id: "n1", type: "lower-case", name: "lower-case" }]; helper.load(lowerNode, flow, function () { var n1 = helper.getNode("n1"); - n1.should.have.property('name', 'lower-case'); - done(); + try { + n1.should.have.property('name', 'lower-case'); + done(); + } catch(err) { + done(err); + } }); }); @@ -87,8 +91,12 @@ describe('lower-case Node', function () { var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1"); n2.on("input", function (msg) { - msg.should.have.property('payload', 'uppercase'); - done(); + try { + msg.should.have.property('payload', 'uppercase'); + done(); + } catch(err) { + done(err); + } }); n1.receive({ payload: "UpperCase" }); }); @@ -100,6 +108,8 @@ In this example, we require `should` for assertions, this helper module, as well We then have a set of mocha unit tests. These tests check that the node loads correctly, and ensures it makes the payload string lower case as expected. +Note how the assertion failures are caught explicitly and passed to the `done()` call. Node-RED swallows exceptions that are raised in the flow, so we make sure the test framework is aware of them. Not doing so would simply lead to a test timeout because `done()` is never called in case of an assertion failure. + ## Initializing Helper To get started, we need to tell the helper where to find the node-red runtime. this is done by calling `helper.init(require.resolve('node-red'))` as shown. From 942b1f6ad8ad5c858817bf0241f9b3c1cc7594ce Mon Sep 17 00:00:00 2001 From: Martin Rubli Date: Sat, 5 Sep 2020 12:18:19 +0800 Subject: [PATCH 3/3] README.md: Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b17b72..755fd65 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Note how the assertion failures are caught explicitly and passed to the `done()` ## Initializing Helper -To get started, we need to tell the helper where to find the node-red runtime. this is done by calling `helper.init(require.resolve('node-red'))` as shown. +To get started, we need to tell the helper where to find the node-red runtime. This is done by calling `helper.init(require.resolve('node-red'))` as shown. The helper takes an optional `userSettings` parameter which is merged with the runtime defaults.