node-red-node-test-helper
Advanced tools
Comparing version 0.2.7 to 0.3.0
@@ -0,2 +1,8 @@ | ||
#### 0.3.0 | ||
- Require node.js >=14 | ||
- Add `setFlows` so that node being tested can modify flows (#54) @Steve-Mcl | ||
#### 0.2.7 | ||
- Wait for startFlows to resolve before returning from loadFlow call - required with Node-RED 1.3+ | ||
@@ -3,0 +9,0 @@ - README.md: Update example unit test to report assertion failures |
@@ -57,2 +57,26 @@ var helper = require("../index.js"); | ||
}); | ||
it('should modify the flow then lower case of payload', async function () { | ||
const flow = [ | ||
{ id: "n2", type: "helper" } | ||
] | ||
await helper.load(lowerNode, flow) | ||
const newFlow = [...flow] | ||
newFlow.push( { id: "n1", type: "lower-case", name: "lower-case", wires:[['n2']] },) | ||
await helper.setFlows(newFlow) | ||
const n1 = helper.getNode('n1') | ||
n1.should.have.a.property('name', 'lower-case') | ||
await new Promise((resolve, reject) => { | ||
const n2 = helper.getNode('n2') | ||
n2.on('input', function (msg) { | ||
try { | ||
msg.should.have.property('payload', 'hello'); | ||
resolve() | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
n1.receive({ payload: 'HELLO' }); | ||
}); | ||
}); | ||
}); |
57
index.js
@@ -202,3 +202,2 @@ /** | ||
if (typeof testCredentials === 'function') { | ||
@@ -208,6 +207,11 @@ cb = testCredentials; | ||
} | ||
var storage = { | ||
const conf = {flows:testFlow,credentials:testCredentials|| {}} | ||
const storage = { | ||
conf: conf, | ||
getFlows: function () { | ||
return Promise.resolve({flows:testFlow,credentials:testCredentials}); | ||
return Promise.resolve(conf); | ||
}, | ||
saveFlows: function(conf) { | ||
storage.conf = conf; | ||
return Promise.resolve(); | ||
} | ||
@@ -286,3 +290,3 @@ }; | ||
this._redNodes.clearRegistry(); | ||
this._logSpy.restore(); | ||
this._logSpy && this._logSpy.restore(); | ||
this._sandbox.restore(); | ||
@@ -308,2 +312,45 @@ | ||
/** | ||
* Update flows | ||
* @param {object|object[]} testFlow Flow data to test a node | ||
* @param {"full"|"flows"|"nodes"} type The type of deploy mode "full", "flows" or "nodes" (defaults to "full") | ||
* @param {object} [testCredentials] Optional node credentials | ||
* @param {function} [cb] Optional callback (not required when called with await) | ||
* @returns {Promise} | ||
*/ | ||
setFlows(testFlow, type, testCredentials, cb) { | ||
const helper = this; | ||
if (typeof testCredentials === 'string' ) { | ||
cb = testCredentials; | ||
testCredentials = {}; | ||
} | ||
if(!type || typeof type != "string") { | ||
type = "full" | ||
} | ||
async function waitStarted() { | ||
return new Promise((resolve, reject) => { | ||
let timeover = setTimeout(() => { | ||
if (timeover) { | ||
timeover = null | ||
reject(Error("timeout waiting event")) | ||
} | ||
}, 300); | ||
function hander() { | ||
clearTimeout(timeover) | ||
helper._events.off('flows:started', hander) | ||
if (timeover) { | ||
timeover = null | ||
resolve() | ||
} | ||
} | ||
helper._events.on('flows:started', hander); // call resolve when its done | ||
}); | ||
} | ||
return this._redNodes.setFlows(testFlow, testCredentials || {}, type) | ||
.then(waitStarted) | ||
.then(() => { | ||
if(cb) cb(); | ||
}); | ||
} | ||
request() { | ||
@@ -310,0 +357,0 @@ return request(this._httpAdmin); |
{ | ||
"name": "node-red-node-test-helper", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"description": "A test framework for Node-RED nodes", | ||
@@ -16,14 +16,14 @@ "main": "index.js", | ||
"dependencies": { | ||
"express": "4.17.1", | ||
"body-parser": "1.19.0", | ||
"body-parser": "1.20.0", | ||
"express": "4.18.1", | ||
"read-pkg-up": "7.0.1", | ||
"semver": "7.3.4", | ||
"semver": "7.3.7", | ||
"should": "^13.2.3", | ||
"should-sinon": "0.0.6", | ||
"sinon": "9.2.4", | ||
"sinon": "11.1.2", | ||
"stoppable": "1.1.0", | ||
"supertest": "4.0.2" | ||
"supertest": "6.2.3" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~7.1.2" | ||
"mocha": "9.2.2" | ||
}, | ||
@@ -50,4 +50,4 @@ "contributors": [ | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=14" | ||
} | ||
} |
@@ -317,4 +317,41 @@ # Node Test Helper | ||
* testCredentials: (object) Optional node credentials. | ||
* cb: (function) Function to call back when testFlows has been started. | ||
* cb: (function) Function to call back when testFlows has been started (not required when called wih `await`) | ||
### setFlows(testFlow, type, testCredentials, cb) | ||
Updates the currently loaded flow. This function has the following arguments: | ||
* testFlow: (array of objects) Flow data to test a node. If you want to use flow data exported from Node-RED editor, export the flow to the clipboard and paste the content into your test scripts. | ||
* type: (string) Flow data to test a node. If you want to use flow data exported from Node-RED editor, export the flow to the clipboard and paste the content into your test scripts. | ||
* testCredentials: (object) Optional node credentials. | ||
* cb: (function) Function to call back when testFlows has been loaded (not required when called wih `await`) | ||
#### Example | ||
```js | ||
it('should modify the flow then lower case of payload', async function () { | ||
const flow = [ | ||
{ id: "n2", type: "helper" } | ||
] | ||
await helper.load(lowerNode, flow) | ||
const newFlow = [...flow] | ||
newFlow.push( { id: "n1", type: "lower-case", name: "lower-case", wires:[['n2']] },) | ||
await helper.setFlows(newFlow, "nodes") //update flows | ||
const n1 = helper.getNode('n1') | ||
n1.should.have.a.property('name', 'lower-case') | ||
await new Promise((resolve, reject) => { | ||
const n2 = helper.getNode('n2') | ||
n2.on('input', function (msg) { | ||
try { | ||
msg.should.have.property('payload', 'hello'); | ||
resolve() | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
n1.receive({ payload: 'HELLO' }); | ||
}); | ||
}); | ||
``` | ||
### unload() | ||
@@ -321,0 +358,0 @@ |
83497
1352
434
+ Added@sinonjs/commons@3.0.1(transitive)
+ Added@sinonjs/fake-timers@11.3.17.1.2(transitive)
+ Added@sinonjs/samsam@6.1.3(transitive)
+ Addedasap@2.0.6(transitive)
+ Addedbody-parser@1.20.0(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcookie@0.5.0(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddezalgo@1.0.4(transitive)
+ Addeddiff@5.2.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedexpress@4.18.1(transitive)
+ Addedfast-safe-stringify@2.1.1(transitive)
+ Addedfinalhandler@1.2.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedformidable@2.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhexoid@1.0.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjust-extend@6.2.0(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmime@2.6.0(transitive)
+ Addednise@5.1.9(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-to-regexp@6.3.0(transitive)
+ Addedqs@6.10.36.14.0(transitive)
+ Addedraw-body@2.5.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsemver@7.3.7(transitive)
+ Addedsend@0.18.0(transitive)
+ Addedserve-static@1.15.0(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedsinon@11.1.2(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedsuperagent@7.1.6(transitive)
+ Addedsupertest@6.2.3(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removed@sinonjs/fake-timers@6.0.1(transitive)
- Removed@sinonjs/samsam@5.3.1(transitive)
- Removedbody-parser@1.19.0(transitive)
- Removedbytes@3.1.0(transitive)
- Removedcontent-disposition@0.5.3(transitive)
- Removedcookie@0.4.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddebug@3.2.7(transitive)
- Removeddepd@1.1.2(transitive)
- Removeddestroy@1.0.4(transitive)
- Removeddiff@4.0.2(transitive)
- Removedexpress@4.17.1(transitive)
- Removedextend@3.0.2(transitive)
- Removedfinalhandler@1.1.2(transitive)
- Removedform-data@2.5.2(transitive)
- Removedformidable@1.2.6(transitive)
- Removedhttp-errors@1.7.2(transitive)
- Removedinherits@2.0.3(transitive)
- Removedisarray@0.0.11.0.0(transitive)
- Removedjust-extend@4.2.1(transitive)
- Removedms@2.1.1(transitive)
- Removednise@4.1.0(transitive)
- Removedon-finished@2.3.0(transitive)
- Removedpath-to-regexp@1.9.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedqs@6.7.0(transitive)
- Removedraw-body@2.4.0(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsemver@7.3.4(transitive)
- Removedsend@0.17.1(transitive)
- Removedserve-static@1.14.1(transitive)
- Removedsetprototypeof@1.1.1(transitive)
- Removedsinon@9.2.4(transitive)
- Removedstatuses@1.5.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedsuperagent@3.8.3(transitive)
- Removedsupertest@4.0.2(transitive)
- Removedtoidentifier@1.0.0(transitive)
Updatedbody-parser@1.20.0
Updatedexpress@4.18.1
Updatedsemver@7.3.7
Updatedsinon@11.1.2
Updatedsupertest@6.2.3