Skip to content

Commit 30d2521

Browse files
committed
Handle testing nodes on 0.20 outside node-red src tree
Fixes #32
1 parent f3cc9ea commit 30d2521

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
const path = require("path");
1919
const sinon = require("sinon");
2020
const should = require('should');
21+
const fs = require('fs');
2122
require('should-sinon');
22-
const when = require("when");
2323
const request = require('supertest');
2424
const express = require("express");
2525
const http = require('http');
@@ -84,7 +84,7 @@ class NodeTestHelper extends EventEmitter {
8484
// public runtime API
8585
this._log = RED.log;
8686
// access internal Node-RED runtime methods
87-
const prefix = path.dirname(requirePath);
87+
let prefix = path.dirname(requirePath);
8888
if (checkSemver(RED.version(),"<0.20.0")) {
8989
this._settings = RED.settings;
9090
this._events = RED.events;
@@ -96,19 +96,25 @@ class NodeTestHelper extends EventEmitter {
9696
// information about the latest call
9797
this._NodePrototype = require(path.join(prefix, 'runtime', 'nodes', 'Node')).prototype;
9898
} else {
99-
// This is good enough for running it within the NR git repository - given the
100-
// code layout changes. But it will need some more work when running in the other
101-
// possible locations
99+
if (!fs.existsSync(path.join(prefix, '@node-red/runtime/lib/nodes'))) {
100+
// Not in the NR source tree, need to go hunting for the modules....
101+
if (/node_modules\/node-red\/lib$/.test(prefix)) {
102+
prefix = path.resolve(path.join(prefix,"..",".."));
103+
} else {
104+
throw new Error("Cannot find the NR source tree. Please raise an issue against node-red/node-red-node-test-helper with full details.");
105+
}
106+
}
107+
102108
this._redNodes = require(path.join(prefix, '@node-red/runtime/lib/nodes'));
103-
this._settings = RED.settings;
104-
this._events = RED.runtime.events;
105109
this._context = require(path.join(prefix, '@node-red/runtime/lib/nodes/context'));
106110
this._comms = require(path.join(prefix, '@node-red/editor-api/lib/editor/comms'));
107111
this._registryUtil = require(path.join(prefix, '@node-red/registry/lib/util'));
108112
this.credentials = require(path.join(prefix, '@node-red/runtime/lib/nodes/credentials'));
109113
// proxy the methods on Node.prototype to both be Sinon spies and asynchronously emit
110114
// information about the latest call
111115
this._NodePrototype = require(path.join(prefix, '@node-red/runtime/lib/nodes/Node')).prototype;
116+
this._settings = RED.settings;
117+
this._events = RED.runtime.events;
112118
}
113119
} catch (ignored) {
114120
console.log(ignored);
@@ -157,7 +163,7 @@ class NodeTestHelper extends EventEmitter {
157163

158164
var storage = {
159165
getFlows: function () {
160-
return when.resolve({flows:testFlow,credentials:testCredentials});
166+
return Promise.resolve({flows:testFlow,credentials:testCredentials});
161167
}
162168
};
163169
// this._settings.logging = {console:{level:'off'}};

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-test-helper",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "A test framework for Node-RED nodes",
55
"main": "index.js",
66
"scripts": {
@@ -20,8 +20,7 @@
2020
"should-sinon": "0.0.6",
2121
"sinon": "5.0.3",
2222
"stoppable": "1.1.0",
23-
"supertest": "3.0.0",
24-
"when": "3.7.8"
23+
"supertest": "3.0.0"
2524
},
2625
"peerDependencies": {
2726
"node-red": "~0.20.0"

0 commit comments

Comments
 (0)