Skip to content

Commit c8b1c6f

Browse files
authored
Merge pull request #10 from mblackstock/init-runtime-path
add helper.init() call to supply NR runtime path
2 parents 1a8e8a2 + e960c2d commit c8b1c6f

6 files changed

+73
-70
lines changed

README.md

+10-38
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,21 @@ Using the test-helper, your tests can start the Node-RED runtime, load a test fl
66

77
## Adding to your node project dependencies
88

9-
To add unit tests your node project test dependencies, add this test helper as follows:
9+
Node-RED is required by the helper as a peer dependency, meaning it must be installed along with the helper itself. To create unit tests for your node project, add this test helper and Node-RED as follows:
1010

11-
npm install node-red-node-test-helper --save-dev
11+
npm install node-red-node-test-helper node-red --save-dev
1212

13-
This will add the helper module to your `package.json` file as a development dependency:
13+
This will add the helper module to your `package.json` file:
1414

1515
```json
1616
...
1717
"devDependencies": {
18+
"node-red":"^0.18.4",
1819
"node-red-node-test-helper": "^0.1.6"
1920
}
2021
...
2122
```
2223

23-
The test-helper requires the node-red runtime to run its tests, but Node-RED is **not** installed as a dependency. The reason for this is that test-helper is (or will be) used in Node-RED core tests, and Node-RED itself has a large number of dependencies that you may not want to download if you already have it installed.
24-
25-
You can install the Node-RED runtime available for your unit tests one of two ways:
26-
27-
1. as a dev dependency in your project:
28-
29-
```
30-
npm install node-red --save-dev
31-
```
32-
33-
2. or link to Node-RED installed globally (recommended) using:
34-
35-
```
36-
npm install -g node-red
37-
npm link node-red
38-
```
39-
40-
Both [Mocha](https://mochajs.org/) and [Should](https://shouldjs.github.io/) will be pulled in with the test helper. Mocha is a unit test framework for Javascript; Should is an assertion library. For more information on these frameworks, see their associated documentation.
41-
42-
## Linking to additional test dependencies
43-
44-
To reduce disk use further, you can install the test-helper and additional dev dependencies globally and then link them to your node project. This may be a better option especially if you are developing more than one node.
45-
46-
See the `package.json` file for the additional dependencies used by test-helper.
47-
48-
For example to install express globally:
49-
50-
npm install -g express
51-
52-
Then link to it in your project:
53-
54-
npm link express
55-
56-
Depending on the nodes in your test flow, you may also want to link to other global packages. If a test indicates that a package cannot be found, and you expect to need it for testing other nodes, consider installing the package globally and then linking it to your node project the same way.
57-
5824
## Adding test script to `package.json`
5925

6026
To run your tests you can add a test script to your `package.json` file in the `scripts` section. To run all of the files with the `_spec.js` prefix in the test directory for example:
@@ -84,6 +50,8 @@ var should = require("should");
8450
var helper = require("node-red-node-test-helper");
8551
var lowerNode = require("../lower-case.js");
8652

53+
helper.init(require.resolve('node-red'));
54+
8755
describe('lower-case Node', function () {
8856

8957
afterEach(function () {
@@ -121,6 +89,10 @@ In this example, we require `should` for assertions, this helper module, as well
12189

12290
We then have a set of mocha unit tests. These tests check that the node loads correctly, and ensures it makes the payload string lower case as expected.
12391

92+
## Initializing Helper
93+
94+
To get started, we need to tell the helper where to find the node-red runtime. this is done by calling `helper.init(require.resolve('node-red'))` as shown.
95+
12496
## Getting nodes in the runtime
12597

12698
The asynchronous `helper.load()` method calls the supplied callback function once the Node-RED server and runtime is ready. We can then call the `helper.getNode(id)` method to get a reference to nodes in the runtime. For more information on these methods see the API section below.

examples/comment_spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
var should = require("should");
1818
var helper = require("../index.js");
19+
helper.init(require.resolve('node-red'));
20+
1921
var commentNode = require("./nodes/90-comment.js");
2022

2123
describe('comment Node', function() {

examples/function_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
var should = require("should");
1818
var helper = require("../index.js");
19+
helper.init(require.resolve('node-red'));
1920

2021
var functionNode = require("./nodes/80-function.js");
2122

examples/lower-case_spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var should = require("should");
22
var helper = require("../index.js");
33
var lowerNode = require("./nodes/lower-case.js");
44

5+
helper.init(require.resolve('node-red'));
6+
57
describe('lower-case Node', function () {
68

79
afterEach(function () {

index.js

+54-29
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,59 @@
1414
* limitations under the License.
1515
**/
1616

17+
var path = require("path");
1718
var should = require("should");
1819
var sinon = require("sinon");
1920
var when = require("when");
2021
var request = require('supertest');
2122
var express = require("express");
2223
var http = require('http');
2324
var stoppable = require('stoppable');
25+
const readPkgUp = require('read-pkg-up');
26+
27+
var RED;
28+
var redNodes;
29+
var flows;
30+
var comms;
31+
var log;
32+
var context;
33+
var events;
34+
35+
var runtimePath;
36+
var package = readPkgUp.sync();
37+
if (package.pkg.name === 'node-red') {
38+
runtimePath = path.join(process.cwd(),package.pkg.main);
39+
initRuntime(runtimePath);
40+
} else {
41+
try {
42+
runtimePath = require.resolve('node-red');
43+
initRuntime(runtimePath);
44+
} catch (err) {
45+
// no runtime path - init must be called from test
46+
}
47+
}
48+
49+
function initRuntime(requirePath) {
50+
51+
try {
52+
RED = require(requirePath);
2453

25-
try {
26-
var RED = require('node-red');
27-
var redNodes = require("node-red/red/runtime/nodes");
28-
var flows = require("node-red/red/runtime/nodes/flows");
29-
var credentials = require("node-red/red/runtime/nodes/credentials");
30-
var comms = require("node-red/red/api/editor/comms.js");
31-
var log = require("node-red/red/runtime/log.js");
32-
var context = require("node-red/red/runtime/nodes/context.js");
33-
var events = require('node-red/red/runtime/events');
34-
} catch (err) {
35-
// no node-red in helper-test dependencies so assume we're testing node-red
36-
var nrPath = process.cwd();
37-
var RED = require(nrPath+"/red/red.js");
38-
var redNodes = require(nrPath+"/red/runtime/nodes");
39-
var flows = require(nrPath+"/red/runtime/nodes/flows");
40-
var credentials = require(nrPath+"/red/runtime/nodes/credentials");
41-
var comms = require(nrPath+"/red/api/editor/comms.js");
42-
var log = require(nrPath+"/red/runtime/log.js");
43-
var context = require(nrPath+"/red/runtime/nodes/context.js");
44-
var events = require(nrPath+"/red/runtime/events.js");
54+
// public runtime API
55+
redNodes = RED.nodes;
56+
events = RED.events;
57+
log = RED.log;
58+
59+
// access internal Node-RED runtime methods
60+
var prefix = requirePath.substring(0, requirePath.indexOf('/red.js'));
61+
context = require(prefix+"/runtime/nodes/context");
62+
comms = require(prefix+"/api/editor/comms");
63+
} catch (err) {
64+
// ignore, assume init will be called again by a test script supplying the runtime path
65+
}
4566
}
4667

68+
initRuntime(runtimePath);
69+
4770
var app = express();
4871

4972
var address = '127.0.0.1';
@@ -58,6 +81,7 @@ function helperNode(n) {
5881
}
5982

6083
module.exports = {
84+
init: initRuntime,
6185
load: function(testNode, testFlow, testCredentials, cb) {
6286
var i;
6387

@@ -98,17 +122,17 @@ module.exports = {
98122
};
99123

100124
redNodes.init({events:events,settings:settings, storage:storage,log:log,});
101-
RED.nodes.registerType("helper", helperNode);
125+
redNodes.registerType("helper", helperNode);
102126
if (Array.isArray(testNode)) {
103127
for (i = 0; i < testNode.length; i++) {
104128
testNode[i](red);
105129
}
106130
} else {
107131
testNode(red);
108132
}
109-
flows.load().then(function() {
110-
flows.startFlows();
111-
should.deepEqual(testFlow, flows.getFlows().flows);
133+
redNodes.loadFlows().then(function() {
134+
redNodes.startFlows();
135+
should.deepEqual(testFlow, redNodes.getFlows().flows);
112136
cb();
113137
});
114138
},
@@ -117,18 +141,17 @@ module.exports = {
117141
// TODO: any other state to remove between tests?
118142
redNodes.clearRegistry();
119143
logSpy.restore();
144+
// internal API
120145
context.clean({allNodes:[]});
121-
return flows.stopFlows();
146+
return redNodes.stopFlows();
122147
},
123148

124149
getNode: function(id) {
125-
return flows.get(id);
150+
return redNodes.getNode(id);
126151
},
127152

128-
credentials: credentials,
129-
130153
clearFlows: function() {
131-
return flows.stopFlows();
154+
return redNodes.stopFlows();
132155
},
133156

134157
request: function() {
@@ -146,6 +169,7 @@ module.exports = {
146169
server.on('listening', function() {
147170
port = server.address().port;
148171
url = 'http://' + address + ':' + port;
172+
// internal API
149173
comms.start();
150174
done();
151175
});
@@ -155,6 +179,7 @@ module.exports = {
155179
stopServer: function(done) {
156180
if (server) {
157181
try {
182+
// internal API
158183
comms.stop();
159184
server.stop(done);
160185
} catch(e) {

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
},
1515
"dependencies": {
1616
"express": "4.16.2",
17-
"should": "^8.4.0",
17+
"read-pkg-up": "3.0.0",
18+
"should": "8.4.0",
1819
"sinon": "1.17.7",
20+
"stoppable": "1.0.6",
1921
"supertest": "3.0.0",
20-
"stoppable": "boneskull/stoppable#boneskull-patch-1",
2122
"when": "3.7.8"
2223
},
2324
"peerDependencies": {
24-
"node-red": "0.18.x"
25+
"node-red": "~0.18.4"
2526
},
2627
"devDependencies": {
2728
"mocha": "~5.0.4"

0 commit comments

Comments
 (0)