Skip to content

Commit

Permalink
Add a basic automated test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Dec 21, 2019
1 parent a715f86 commit c309c82
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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.
Expand All @@ -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.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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;
});
},
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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'"
}
}
32 changes: 32 additions & 0 deletions test-node.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script>
RED.nodes.registerType('test-node', {
category: 'function',
defaults: {
name: { value: '', },
multiplyBy: { value: 2, },
},
inputs: 1,
outputs: 1,
icon: 'function.png',
label: function () {
return this.name || 'test-node';
},
});
</script>

<script type="text/x-red" data-template-name="test-node">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-multiplyBy"><i class="icon-tag"></i> Multiply by</label>
<input type="number" id="node-input-multiplyBy" placeholder="2" />
</div>
</script>

<script type="text/x-red" data-help-name="test-node">
<p>
This test node will multiply the payload by the value given in the <code>multiplyBy</code> property.
</p>
</script>
26 changes: 26 additions & 0 deletions test-node.js
Original file line number Diff line number Diff line change
@@ -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);
};
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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!');
}

0 comments on commit c309c82

Please sign in to comment.