Skip to content

Commit

Permalink
Optionally preload catch/status/complete nodes in test cases
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
knolleary committed Feb 1, 2021
1 parent 9adb40d commit 0a663e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
36 changes: 30 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function findRuntimePath() {
return path.join(dirpath, pkg.main);
} catch (ignored) {}
}
// case 4: NR & NRNTH are git repos sat alongside each other
try {
const nrpkg = require("../node-red/package.json");
return "../node-red/packages/node_modules/node-red"
} catch(ignored) {}
}


Expand Down Expand Up @@ -122,6 +127,14 @@ class NodeTestHelper extends EventEmitter {
this._NodePrototype = require(path.join(prefix, '@node-red/runtime/lib/nodes/Node')).prototype;
this._settings = RED.settings;
this._events = RED.runtime.events;

this._nodeModules = {
'catch': require(path.join(prefix, '@node-red/nodes/core/common/25-catch.js')),
'status': require(path.join(prefix, '@node-red/nodes/core/common/25-status.js')),
'complete': require(path.join(prefix, '@node-red/nodes/core/common/24-complete.js'))
}


}
} catch (ignored) {
console.log(ignored);
Expand Down Expand Up @@ -240,13 +253,24 @@ class NodeTestHelper extends EventEmitter {
});
}

if (Array.isArray(testNode)) {
testNode.forEach(fn => {
fn(red);
});
} else {
testNode(red);
let preloadedCoreModules = new Set();
testFlow.forEach(n => {
if (this._nodeModules.hasOwnProperty(n.type)) {
// Go find the 'real' core node module and load it...
this._nodeModules[n.type](red);
preloadedCoreModules.add(this._nodeModules[n.type]);
}
})

if (!Array.isArray(testNode)) {
testNode = [testNode];
}
testNode.forEach(fn => {
if (!preloadedCoreModules.has(fn)) {
fn(red);
}
});

return redNodes.loadFlows()
.then(() => {
redNodes.startFlows();
Expand Down
6 changes: 3 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.5",
"version": "0.2.6",
"description": "A test framework for Node-RED nodes",
"main": "index.js",
"scripts": {
Expand All @@ -16,10 +16,10 @@
"express": "4.17.1",
"body-parser": "1.19.0",
"read-pkg-up": "7.0.1",
"semver": "7.3.2",
"semver": "7.3.4",
"should": "^13.2.3",
"should-sinon": "0.0.6",
"sinon": "9.0.2",
"sinon": "9.2.4",
"stoppable": "1.1.0",
"supertest": "4.0.2"
},
Expand Down

0 comments on commit 0a663e1

Please sign in to comment.