From 30d252190eba3a7a67b9a84810bbbd813371e392 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 23 Mar 2019 23:26:43 +0000 Subject: [PATCH] Handle testing nodes on 0.20 outside node-red src tree Fixes #32 --- index.js | 22 ++++++++++++++-------- package.json | 5 ++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 542c6db..1f3d33c 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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; @@ -96,12 +96,16 @@ 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')); @@ -109,6 +113,8 @@ class NodeTestHelper extends EventEmitter { // 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); @@ -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'}}; diff --git a/package.json b/package.json index 79ebf34..3c38581 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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"