Skip to content

Commit

Permalink
Handle testing nodes on 0.20 outside node-red src tree
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
knolleary committed Mar 23, 2019
1 parent f3cc9ea commit 30d2521
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
const path = require("path");
const sinon = require("sinon");
const should = require('should');
const fs = require('fs');
require('should-sinon');
const when = require("when");
const request = require('supertest');
const express = require("express");
const http = require('http');
Expand Down Expand Up @@ -84,7 +84,7 @@ class NodeTestHelper extends EventEmitter {
// public runtime API
this._log = RED.log;
// access internal Node-RED runtime methods
const prefix = path.dirname(requirePath);
let prefix = path.dirname(requirePath);
if (checkSemver(RED.version(),"<0.20.0")) {
this._settings = RED.settings;
this._events = RED.events;
Expand All @@ -96,19 +96,25 @@ class NodeTestHelper extends EventEmitter {
// information about the latest call
this._NodePrototype = require(path.join(prefix, 'runtime', 'nodes', 'Node')).prototype;
} else {
// This is good enough for running it within the NR git repository - given the
// code layout changes. But it will need some more work when running in the other
// possible locations
if (!fs.existsSync(path.join(prefix, '@node-red/runtime/lib/nodes'))) {
// Not in the NR source tree, need to go hunting for the modules....
if (/node_modules\/node-red\/lib$/.test(prefix)) {
prefix = path.resolve(path.join(prefix,"..",".."));
} else {
throw new Error("Cannot find the NR source tree. Please raise an issue against node-red/node-red-node-test-helper with full details.");
}
}

this._redNodes = require(path.join(prefix, '@node-red/runtime/lib/nodes'));
this._settings = RED.settings;
this._events = RED.runtime.events;
this._context = require(path.join(prefix, '@node-red/runtime/lib/nodes/context'));
this._comms = require(path.join(prefix, '@node-red/editor-api/lib/editor/comms'));
this._registryUtil = require(path.join(prefix, '@node-red/registry/lib/util'));
this.credentials = require(path.join(prefix, '@node-red/runtime/lib/nodes/credentials'));
// proxy the methods on Node.prototype to both be Sinon spies and asynchronously emit
// information about the latest call
this._NodePrototype = require(path.join(prefix, '@node-red/runtime/lib/nodes/Node')).prototype;
this._settings = RED.settings;
this._events = RED.runtime.events;
}
} catch (ignored) {
console.log(ignored);
Expand Down Expand Up @@ -157,7 +163,7 @@ class NodeTestHelper extends EventEmitter {

var storage = {
getFlows: function () {
return when.resolve({flows:testFlow,credentials:testCredentials});
return Promise.resolve({flows:testFlow,credentials:testCredentials});
}
};
// this._settings.logging = {console:{level:'off'}};
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-test-helper",
"version": "0.2.0",
"version": "0.2.1",
"description": "A test framework for Node-RED nodes",
"main": "index.js",
"scripts": {
Expand All @@ -20,8 +20,7 @@
"should-sinon": "0.0.6",
"sinon": "5.0.3",
"stoppable": "1.1.0",
"supertest": "3.0.0",
"when": "3.7.8"
"supertest": "3.0.0"
},
"peerDependencies": {
"node-red": "~0.20.0"
Expand Down

0 comments on commit 30d2521

Please sign in to comment.