Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-red-node-test-helper

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-red-node-test-helper - npm Package Compare versions

Comparing version 0.2.7 to 0.3.0

6

CHANGELOG.md

@@ -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);

16

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc