Skip to content

Commit 9560186

Browse files
authored
Merge pull request #67 from SenseTecnic/npm-5-test
use npm install <folder> to test with npm 5, minor edits
2 parents 994eb8c + a26ae24 commit 9560186

1 file changed

Lines changed: 89 additions & 20 deletions

File tree

docs/creating-nodes/first-node.md

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ title: Creating your first node
77
Nodes get created when a flow is deployed, they may send and receive some messages
88
whilst the flow is running and they get deleted when the next flow is deployed.
99

10-
They consist of a pair of files; a JavaScript file that defines what the
10+
They typically consist of a pair of files; a JavaScript file that defines what the
1111
node does, and an html file that defines the node's properties, edit dialog and
1212
help text.
1313

14-
When packaged as an npm module, a `package.json` file is used to pull it all together.
14+
A `package.json` file is used to package it all together as an npm module.
1515

1616
- [Creating a simple node](#creating-a-simple-node)
1717
- [package.json](#package-json)
1818
- [lower-case.js](#lower-casejs)
1919
- [lower-case.html](#lower-casehtml)
2020
- [Testing your node in Node-RED](#testing-your-node-in-node-red)
2121

22-
2322
### Creating a simple node
2423

2524
This example will show how to create a node that converts message payloads to
2625
all lower-case characters.
2726

28-
Create a directory where you will develop your code. Within that directory,
29-
create the following files:
27+
Ensure you have the recommended LTS version of Node.js installed on your system. As of this writing this is version **LTS 8.x** which includes npm version 5.x.
28+
29+
Create a directory where you will develop your code. Within that directory, create the following files:
3030

3131
- `package.json`
3232
- `lower-case.js`
@@ -65,7 +65,7 @@ to the [packaging guide](packaging).
6565

6666
<h4 id="lower-casejs"><i class="fa fa-file-o"></i> lower-case.js</h4>
6767

68-
{% highlight javascript %}
68+
```javascript
6969
module.exports = function(RED) {
7070
function LowerCaseNode(config) {
7171
RED.nodes.createNode(this,config);
@@ -77,9 +77,9 @@ module.exports = function(RED) {
7777
}
7878
RED.nodes.registerType("lower-case",LowerCaseNode);
7979
}
80-
{% endhighlight %}
80+
```
8181

82-
The node is wrapped as a node module. The module exports a function that gets called
82+
The node is wrapped as a Node.js module. The module exports a function that gets called
8383
when the runtime loads the node on start-up. The function is called with a single
8484
argument, `RED`, that provides the module access to the Node-RED runtime api.
8585

@@ -106,7 +106,7 @@ For more information about the runtime part of the node, see [here](node-js).
106106
<h4 id="lower-casehtml"><i class="fa fa-file-o"></i> lower-case.html</h4>
107107

108108

109-
{% highlight html %}
109+
```html
110110
<script type="text/javascript">
111111
RED.nodes.registerType('lower-case',{
112112
category: 'function',
@@ -133,7 +133,7 @@ For more information about the runtime part of the node, see [here](node-js).
133133
<script type="text/x-red" data-help-name="lower-case">
134134
<p>A simple node that converts the message payloads into all lower-case characters</p>
135135
</script>
136-
{% endhighlight %}
136+
```
137137

138138
A node's HTML file provides the following things:
139139

@@ -149,16 +149,85 @@ For more information about the editor part of the node, see [here](node-html).
149149

150150
### Testing your node in Node-RED
151151

152-
Once you have created a basic node module as described above, you can install
153-
it into your Node-RED runtime.
152+
Once you have created a basic node module as described above, you can install it into your Node-RED runtime.
153+
154+
To test a node module locally using npm 5.x, the [`npm install <folder>`](https://docs.npmjs.com/cli/install) command can be used. This allows you
155+
to develop the node in a local directory and have it linked into a local node-red install during development.
156+
157+
In your node-red user directory, typically `~/.node-red`, run:
158+
159+
npm install <location of node module>
160+
161+
For example, on Mac OS or linux, if your node is located at `~/dev/node-red-contrib-example-lower-case` you would type the following:
162+
163+
cd ~/.node-red
164+
npm install ~/dev/node-red-contrib-example-lower-case
165+
166+
This creates a symbolic link to your node module project directory in `~/.node-red/node_modules` so that Node-RED will discover the node when it starts. Any changes to the node's file can be picked up by simply restarting Node-RED. On Windows, again, using npm 5.x or greater:
167+
168+
cd C:\Users\my_name\.node_red
169+
npm install C:\Users\my_name\Documents\GitHub\node-red-contrib-example-lower-case
170+
171+
If you are using an older version of npm, you can create a symbolic link manually to your project. For example, on Mac or linux systems:
172+
173+
cd ~/.node-red/node_modules
174+
ln -s ~/dev/node-red-contrib-example-lower-case .
175+
176+
On Windows with older versions of npm, use `mklink` instead:
177+
178+
cd C:\Users\my_name\.node_red
179+
mklink /D node_modules\node-red-contrib-example-lower-case C:\Users\my_name\Documents\GitHub\node-red-contrib-example-lower-case
180+
181+
<div class="doc-callout">
182+
<em>Note</em>: npm 5 will add your module as a dependency in the <code>package.json</code> file located in your user directory. If you want to prevent this, use the npm <code>--no-save</code> option.
183+
</div>
184+
185+
### Unit Testing
186+
187+
To support unit testing, an npm module called [`node-red-node-test-helper`](https://www.npmjs.com/package/node-red-node-test-helper) can be used. The test-helper is a framework built on the Node-RED runtime to make it easier to test nodes.
188+
189+
Using this framework, you can create test flows, and then assert that your node properties and output is working as expected. For example, to add a unit test to the lower-case node you can add a `test` folder to your node module package containing a file called `_spec.js`
190+
191+
<h4><i class="fa fa-file-o"></i> test/_spec.js</h4>
192+
193+
```javascript
194+
var should = require("should");
195+
var helper = require("node-red-test-helper");
196+
var lowerNode = require("../lower-case.js");
197+
198+
describe('lower-case Node', function () {
199+
200+
afterEach(function () {
201+
helper.unload();
202+
});
203+
204+
it('should be loaded', function (done) {
205+
var flow = [{ id: "n1", type: "lower-case", name: "test name" }];
206+
helper.load(lowerNode, flow, function () {
207+
var n1 = helper.getNode("n1");
208+
n1.should.have.property('name', 'test name');
209+
done();
210+
});
211+
});
212+
213+
it('should make payload lower case', function (done) {
214+
var flow = [{ id: "n1", type: "lower-case", name: "test name",wires:[["n2"]] },
215+
{ id: "n2", type: "helper" }];
216+
helper.load(lowerNode, flow, function () {
217+
var n2 = helper.getNode("n2");
218+
var n1 = helper.getNode("n1");
219+
n2.on("input", function (msg) {
220+
msg.should.have.property('payload', 'uppercase');
221+
done();
222+
});
223+
n1.receive({ payload: "UpperCase" });
224+
});
225+
});
226+
});
227+
```
154228

155-
To test a node module locally, the `npm link` command can be used. This allows you
156-
to develop the node in a local directory and have it linked into a local node-red
157-
install, as if it had been npm installed.
229+
These tests check to see that the node is loaded into the runtime correctly, and that it correctly changes the payload to lower case as expected.
158230

159-
1. in the directory containing the node's `package.json` file, run: `sudo npm link`.
160-
2. in your node-red user directory, typically `~/.node-red` run: `npm link <name of node module>`.
231+
Both tests load the node into the runtime using `helper.load` supplying the node under test and a test flow The first checks that the node in the runtime has the correct name property. The second test uses a helper node to check that the output from the node is, in fact, lower case.
161232

162-
This creates the appropriate symbolic links between the two directories so Node-RED
163-
will discover the node when it starts. Any changes to the node's file can be picked
164-
up by simply restarting Node-RED.
233+
The helper module contains other examples of tests taken from the Node-RED core nodes. For more information on the helper module, see the associated README.

0 commit comments

Comments
 (0)