Skip to content

Commit aac6498

Browse files
committed
add info on windows, info on unit testing with helper
1 parent 863967d commit aac6498

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

docs/creating-nodes/first-node.md

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,76 @@ In your node-red user directory, typically `~/.node-red`, run:
158158

159159
npm install <location of node module>
160160

161-
For example, if your node is located at `~/dev/node-red-contrib-example-lower-case` you would type the following:
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:
162162

163163
cd ~/.node-red
164164
npm install ~/dev/node-red-contrib-example-lower-case
165165

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.
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:
167167

168-
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:
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:
169172

170173
cd ~/.node-red/node_modules
171174
ln -s ~/dev/node-red-contrib-example-lower-case .
172175

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+
173181
<div class="doc-callout">
174-
<em>Note</em>: npm 5 will add your module as a dependency in the <code>package.json</code> file located in your user directory. To prevent this, use the npm <code>--no-save</code> option.
175-
</div>
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-contrib-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+
```
228+
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.
230+
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.
232+
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)