You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/creating-nodes/first-node.md
+89-20Lines changed: 89 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,26 +7,26 @@ title: Creating your first node
7
7
Nodes get created when a flow is deployed, they may send and receive some messages
8
8
whilst the flow is running and they get deleted when the next flow is deployed.
9
9
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
11
11
node does, and an html file that defines the node's properties, edit dialog and
12
12
help text.
13
13
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.
15
15
16
16
-[Creating a simple node](#creating-a-simple-node)
17
17
-[package.json](#package-json)
18
18
-[lower-case.js](#lower-casejs)
19
19
-[lower-case.html](#lower-casehtml)
20
20
-[Testing your node in Node-RED](#testing-your-node-in-node-red)
21
21
22
-
23
22
### Creating a simple node
24
23
25
24
This example will show how to create a node that converts message payloads to
26
25
all lower-case characters.
27
26
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:
30
30
31
31
-`package.json`
32
32
-`lower-case.js`
@@ -65,7 +65,7 @@ to the [packaging guide](packaging).
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:
<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`
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
+
```
154
228
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.
158
230
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.
161
232
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