1818const path = require ( "path" ) ;
1919const sinon = require ( "sinon" ) ;
2020const should = require ( 'should' ) ;
21+ const fs = require ( 'fs' ) ;
2122require ( 'should-sinon' ) ;
22- const when = require ( "when" ) ;
2323const request = require ( 'supertest' ) ;
2424const express = require ( "express" ) ;
2525const 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 ( / n o d e _ m o d u l e s \/ n o d e - r e d \/ l i b $ / . 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'}};
0 commit comments