Skip to content

Commit ca6e56b

Browse files
committed
update unit test example
(formatting cganges by pretttier)
1 parent de93235 commit ca6e56b

File tree

1 file changed

+80
-60
lines changed

1 file changed

+80
-60
lines changed

docs/creating-nodes/first-node.md

+80-60
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ Nodes get created when a flow is deployed, they may send and receive some messag
99
whilst the flow is running and they get deleted when the next flow is deployed.
1010

1111
They consist of a pair of files:
12-
- a JavaScript file that defines what the node does,
13-
- an html file that defines the node's properties, edit dialog and help text.
12+
13+
- a JavaScript file that defines what the node does,
14+
- an html file that defines the node's properties, edit dialog and help text.
1415

1516
A `package.json` file is used to package it all together as an npm module.
1617

17-
- [Creating a simple node](#creating-a-simple-node)
18-
- [package.json](#package-json)
19-
- [lower-case.js](#lower-casejs)
20-
- [lower-case.html](#lower-casehtml)
21-
- [Testing your node in Node-RED](#testing-your-node-in-node-red)
18+
- [Creating a simple node](#creating-a-simple-node)
19+
- [package.json](#package-json)
20+
- [lower-case.js](#lower-casejs)
21+
- [lower-case.html](#lower-casehtml)
22+
- [Testing your node in Node-RED](#testing-your-node-in-node-red)
2223

2324
### Creating a simple node
2425

@@ -30,9 +31,9 @@ the time of writing this is 10.x.
3031

3132
Create a directory where you will develop your code. Within that directory, create the following files:
3233

33-
- `package.json`
34-
- `lower-case.js`
35-
- `lower-case.html`
34+
- `package.json`
35+
- `lower-case.js`
36+
- `lower-case.html`
3637

3738
<h4 id="package-json"><i class="fa fa-file-o"></i> package.json</h4>
3839

@@ -63,22 +64,22 @@ For more information about how to package your node, including requirements on
6364
naming and other properties that should be set before publishing your node, refer
6465
to the [packaging guide](packaging).
6566

66-
**Note**: Please do ***not*** publish this example node to npm!
67+
**Note**: Please do **_not_** publish this example node to npm!
6768

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

7071
```javascript
71-
module.exports = function(RED) {
72-
function LowerCaseNode(config) {
73-
RED.nodes.createNode(this,config);
74-
var node = this;
75-
node.on('input', function(msg) {
76-
msg.payload = msg.payload.toLowerCase();
77-
node.send(msg);
78-
});
79-
}
80-
RED.nodes.registerType("lower-case",LowerCaseNode);
81-
}
72+
module.exports = function (RED) {
73+
function LowerCaseNode(config) {
74+
RED.nodes.createNode(this, config);
75+
var node = this;
76+
node.on('input', function (msg) {
77+
msg.payload = msg.payload.toLowerCase();
78+
node.send(msg);
79+
});
80+
}
81+
RED.nodes.registerType('lower-case', LowerCaseNode);
82+
};
8283
```
8384

8485
The node is wrapped as a Node.js module. The module exports a function that gets called
@@ -107,41 +108,43 @@ For more information about the runtime part of the node, see [here](node-js).
107108

108109
<h4 id="lower-casehtml"><i class="fa fa-file-o"></i> lower-case.html</h4>
109110

110-
111111
```html
112112
<script type="text/javascript">
113-
RED.nodes.registerType('lower-case',{
114-
category: 'function',
115-
color: '#a6bbcf',
116-
defaults: {
117-
name: {value:""}
118-
},
119-
inputs: 1,
120-
outputs: 1,
121-
icon: "file.svg",
122-
label: function() {
123-
return this.name||"lower-case";
124-
}
125-
});
113+
RED.nodes.registerType('lower-case', {
114+
category: 'function',
115+
color: '#a6bbcf',
116+
defaults: {
117+
name: { value: '' }
118+
},
119+
inputs: 1,
120+
outputs: 1,
121+
icon: 'file.svg',
122+
label: function () {
123+
return this.name || 'lower-case';
124+
}
125+
});
126126
</script>
127127

128128
<script type="text/html" data-template-name="lower-case">
129-
<div class="form-row">
130-
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
131-
<input type="text" id="node-input-name" placeholder="Name">
132-
</div>
129+
<div class="form-row">
130+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
131+
<input type="text" id="node-input-name" placeholder="Name" />
132+
</div>
133133
</script>
134134

135135
<script type="text/html" data-help-name="lower-case">
136-
<p>A simple node that converts the message payloads into all lower-case characters</p>
136+
<p>
137+
A simple node that converts the message payloads into all lower-case
138+
characters
139+
</p>
137140
</script>
138141
```
139142

140143
A node's HTML file provides the following things:
141144

142-
- the main node definition that is registered with the editor
143-
- the edit template
144-
- the help text
145+
- the main node definition that is registered with the editor
146+
- the edit template
147+
- the help text
145148

146149
In this example, the node has a single editable property, `name`. Whilst not
147150
required, there is a widely used convention to this property to help distinguish
@@ -171,7 +174,7 @@ On Windows you would do:
171174
cd C:\Users\my_name\.node_red
172175
npm install C:\Users\my_name\Documents\GitHub\node-red-contrib-example-lower-case
173176

174-
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:
177+
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:
175178

176179
<div class="doc-callout">
177180
<em>Note</em> : <code>npm</code> will automatically add an entry for your module in the
@@ -182,50 +185,67 @@ command.
182185

183186
### Unit Testing
184187

185-
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
188+
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
186189
built on the Node-RED runtime to make it easier to test nodes.
187190

188-
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`
191+
To prepare the tests you need to add the test dependencies to your `package.json` file:
192+
193+
```bash
194+
npm install --savedev node-red node-red-node-test-helper
195+
```
196+
197+
The test helper relies on NodeRED, so it needs to be added as development dependency.
198+
199+
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`
189200

190201
<h4 id="lower-casespecjs"><i class="fa fa-file-o"></i> test/lower-case_spec.js</h4>
191202

192203
```javascript
193-
var helper = require("node-red-node-test-helper");
194-
var lowerNode = require("../lower-case.js");
204+
const helper = require('node-red-node-test-helper');
205+
const lowerNode = require('../lower-case.js');
206+
207+
// Initialize access to NodeRED
208+
helper.init(require.resolve('node-red'));
195209

196210
describe('lower-case Node', function () {
211+
beforeEach(function (done) {
212+
helper.startServer(done);
213+
});
197214

198-
afterEach(function () {
215+
afterEach(function (done) {
199216
helper.unload();
217+
helper.stopServer(done);
200218
});
201219

202220
it('should be loaded', function (done) {
203-
var flow = [{ id: "n1", type: "lower-case", name: "test name" }];
221+
var flow = [{ id: 'n1', type: 'lower-case', name: 'test name' }];
204222
helper.load(lowerNode, flow, function () {
205-
var n1 = helper.getNode("n1");
223+
var n1 = helper.getNode('n1');
206224
n1.should.have.property('name', 'test name');
207225
done();
208226
});
209227
});
210228

211229
it('should make payload lower case', function (done) {
212-
var flow = [{ id: "n1", type: "lower-case", name: "test name",wires:[["n2"]] },
213-
{ id: "n2", type: "helper" }];
230+
var flow = [
231+
{ id: 'n1', type: 'lower-case', name: 'test name', wires: [['n2']] },
232+
{ id: 'n2', type: 'helper' }
233+
];
214234
helper.load(lowerNode, flow, function () {
215-
var n2 = helper.getNode("n2");
216-
var n1 = helper.getNode("n1");
217-
n2.on("input", function (msg) {
235+
var n2 = helper.getNode('n2');
236+
var n1 = helper.getNode('n1');
237+
n2.on('input', function (msg) {
218238
msg.should.have.property('payload', 'uppercase');
219239
done();
220240
});
221-
n1.receive({ payload: "UpperCase" });
241+
n1.receive({ payload: 'UpperCase' });
222242
});
223243
});
224244
});
225245
```
226246

227247
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.
228248

229-
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.
249+
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.
230250

231-
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.
251+
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)