From c309c82eb1d68b4a004567b9ced09961de746d65 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 21 Dec 2019 12:18:52 +0100 Subject: [PATCH] Add a basic automated test --- README.md | 33 ++++++++++++++++++++++++++++++--- index.js | 8 ++++---- package.json | 7 ++++++- test-node.html | 32 ++++++++++++++++++++++++++++++++ test-node.js | 26 ++++++++++++++++++++++++++ test.js | 21 +++++++++++++++++++++ 6 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 test-node.html create mode 100644 test-node.js create mode 100644 test.js diff --git a/README.md b/README.md index dbd6620..8411330 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,13 @@ See an example of use in [*node-red-contrib-json-multi-schema*](https://github.c ## Usage -Add an `index.js` file to your Node-RED node, next to a `package.json` that has a structure like `{ "node-red": {"node-type": "node-type.js"} }`: +Add an entry file `index.js` (or another name) to your Node-RED node, next to a `package.json` that contains a structure like `{ "node-red": {"node-type": "node-type.js"} }`: ```js const RED = require('node-red-contrib-mock-cli'); const noderedNode = RED.load(require.main); if (noderedNode) { + //noderedNode.* //Access to the Node-RED node instance RED.run(); } else { console.error('Error loading Node-RED node!'); @@ -40,6 +41,20 @@ Properties of configuration nodes can be specified using a *dot* such as: node ./index.js node-type --server.url='"https://example.net/"' --server.username='"Alice"' ``` +### Example +Replace `test.js` by the name of your entry file (e.g. `index.js`), and `test-node` by the name of the type of your node. + +```sh +printf '{"payload":3} \n {"payload":7}' | node ./test.js test-node --multiplyBy='5' +``` + +Outputs: + +``` +{"payload":15} +{"payload":35} +``` + ### JSON in Node-RED format The command expects JSON messages with a Node-RED structure `{"payload":"Example"}` from standard input, one line per message. @@ -56,8 +71,20 @@ The command outputs JSON messages with a Node-RED structure to standard output, See some examples of inputs and outputs in [*node-red-contrib-json-multi-schema*](https://github.com/alexandrainst/node-red-contrib-json-multi-schema#wiringpiping-all-modules-together). +### Standard outputs + +Only the Node-RED JSON payload is emitted on standard-output (STDOUT), while all other messages and errors are on the standard-error (STDERR). + + +### Advanced usage +It is possible to catch the node events { `debug`, `error`, `log` } to override the default behaviour (which is to write to standard-error): + +```js +noderedNode.on('debug', msg => console.warn('Caught event: ' + msg)); +``` + ## Limitations -This module does not have the ambition of exposing the full Node-RED functionality, but instead focuses on simple cases, providing a tiny and efficient layer without dependency. -So for unit testing, and if requiring the full Node-RED is fine, then check the official [node-red-node-test-helper](https://github.com/node-red/node-red-node-test-helper) instead of this module. +This module does not have the ambition of exposing the full Node-RED functionality, but instead focuses on simple cases, providing a tiny and efficient layer without any dependency. +So for more advanced unit testing, and if requiring the full Node-RED is fine, then check the official [node-red-node-test-helper](https://github.com/node-red/node-red-node-test-helper) instead of this module. diff --git a/index.js b/index.js index c72fc4c..26cc1d4 100644 --- a/index.js +++ b/index.js @@ -150,7 +150,7 @@ const RED = { console.log(JSON.stringify(msg)); nbAwaited--; if (done && nbAwaited <= 0) { - console.error('==== Done. ===='); + console.warn('==== Done. ===='); //process.exit(0); } }); @@ -183,17 +183,17 @@ const RED = { }); rl.on('error', () => { - console.error('==== STDIN error ===='); + console.warn('==== STDIN error ===='); done = true; }); rl.on('pause', () => { - console.error('==== STDIN paused ===='); + console.warn('==== STDIN paused ===='); done = true; }); rl.on('close', () => { - console.error('==== STDIN closed ===='); + console.warn('==== STDIN closed ===='); done = true; }); }, diff --git a/package.json b/package.json index 57f27ca..39a9ef6 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,11 @@ "nodered", "Node-RED" ], + "node-red": { + "nodes": { + "test-node": "test-node.js" + } + }, "author": { "name": "Alexandre Alapetite", "url": "https://alexandra.dk/alexandre.alapetite" @@ -32,6 +37,6 @@ "start": "node ./index.js", "lint": "node ./node_modules/.bin/jshint *.js", "pretest": "npm run-script lint", - "test": "node -v" + "test": "echo '{\"payload\":3}' | node ./test.js test-node --multiplyBy='5' | grep -q '{\"payload\":15}' && echo 'OK'" } } diff --git a/test-node.html b/test-node.html new file mode 100644 index 0000000..0b1f20a --- /dev/null +++ b/test-node.html @@ -0,0 +1,32 @@ + + + + + diff --git a/test-node.js b/test-node.js new file mode 100644 index 0000000..cf1ea47 --- /dev/null +++ b/test-node.js @@ -0,0 +1,26 @@ +/* jshint esversion:8, node:true, strict:true */ +/** + * Node-RED test node. + */ + +module.exports = RED => { + "use strict"; + + function TestNode(config) { + RED.nodes.createNode(this, config); + const node = this; + const multiplyBy = config.multiplyBy; + + node.on('input', async msg => { + node.debug('test-node just received a message'); + try { + msg.payload *= multiplyBy; + node.send(msg); + } catch (ex) { + msg.error = 'Error: ' + ex; + } + }); + } + + RED.nodes.registerType('test-node', TestNode); +}; diff --git a/test.js b/test.js new file mode 100644 index 0000000..67a8754 --- /dev/null +++ b/test.js @@ -0,0 +1,21 @@ +/* jshint esversion:8, node:true, strict:true */ +"use strict"; +/** + * Command-line interface for the json-multi-schema Node-RED nodes. + * + * Script to run our Node-RED nodes from terminal without Node-RED and using STDIN / STDOUT. + * + */ + +//Load fake/mocked Node-RED: +//const RED = require('node-red-contrib-mock-cli'); //Use that in real projects +const RED = require('./index.js'); //Only for this local test + +const noderedNode = RED.load(require.main); + +if (noderedNode) { + noderedNode.on('debug', msg => console.warn('Caught event: ' + msg)); + RED.run(); +} else { + console.error('Error loading Node-RED node!'); +}