Skip to content

Commit

Permalink
refactor(tests): testing and clean up from developer mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
biancode committed Jun 16, 2024
1 parent a35e479 commit 65176fa
Show file tree
Hide file tree
Showing 34 changed files with 1,450 additions and 1,204 deletions.
3 changes: 3 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"timeout": 7000
}
42 changes: 42 additions & 0 deletions list-flows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs')
const path = require('path')

const flows = {}

// Recursive function to read subfolders and files
function readSubfoldersAndFiles (directory) {
fs.readdirSync(directory).forEach(file => {
const fullPath = path.join('./', directory, file)
if (fs.lstatSync(fullPath).isDirectory()) {
readSubfoldersAndFiles(fullPath)
} else {
if (path.extname(fullPath) === '.js' && fullPath.indexOf('flows') !== -1) {
flows[fullPath] = require('./' + fullPath)
}
}
})
}

// convert Object to JSON formatted string
function formatJSON (obj) {
for (const data in obj) {
obj[data] = JSON.parse(JSON.stringify(obj[data]))
}
return obj
}

readSubfoldersAndFiles('./test')
// console.log(flows);

for (const flowEntry in flows) {
console.log(flowEntry)
const flowData = require('./' + flowEntry)

for (const flow in flowData) {
flowData[flow] = formatJSON(flowData[flow])
}

// console.log(flowData);
}

// console.log(flows);
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@
"homepage": "https://plus4nodered.com/",
"scripts": {
"changelog": "npm run rewrite-changelog",
"test": "npm run lint && mocha ./test --parallel --recursive --reporter dot --timeout 7000",
"test:slow": "npm run lint && mocha ./test --recursive --reporter dot --timeout 7000",
"test:verbose": "npm run lint && mocha ./test --recursive --bail --timeout 8000",
"test": "npm run lint && mocha ./test --parallel --recursive --reporter dot",
"test:slow": "npm run lint && mocha ./test --recursive --reporter dot",
"test:verbose": "npm run lint && mocha ./test --recursive",
"test:withStop": "npm run lint && mocha ./test --recursive --bail",
"test:e2e": "npm run lint && mocha ./test/core/modbus-io-core-test.js --parallel --recursive --reporter dot --timeout 100000",
"lint": "standard --fix --ignore test/e2e/flows/**",
"test:units": "npm run lint && mocha './test/units/*-test.js' --parallel --recursive --timeout 7000",
"test:core": "npm run lint && mocha './test/core/*-test.js' --parallel --recursive --timeout 7000",
"test-nyc": "nyc --reporter=html --reporter=text mocha --recursive --timeout=7000",
"lint": "standard --fix",
"test:units": "npm run lint && mocha './test/units/*-test.js' --parallel --recursive",
"test:core": "npm run lint && mocha './test/core/*-test.js' --parallel --recursive",
"test-nyc": "nyc --reporter=html --reporter=text mocha --recursive",
"test-npx": "npx nyc@latest --reporter=html --reporter=text mocha ./test --parallel --recursive --reporter dot --timeout 10000",
"test-with-coverage": "nyc --reporter=lcovonly mocha --recursive --timeout 7000 -R spec && cat ./coverage/lcov.info | codacy-coverage --token $CODACY_COVERAGE_TOKEN && rm -rf ./coverage",
"coverage": "npm run lint && nyc mocha ./test --parallel --recursive --reporter dot --timeout 10000",
"coverage-nyc": "npm run lint && nyc report --reporter=text-lcov | coveralls",
"build": "npm run lint && gulp",
"prepublishOnly": "npm run lint && npm run build && npm run rewrite-changelog && npm test && mocha test --recursive --timeout 7000 --reporter dot && nyc mocha --recursive --timeout 7000",
"prepublishOnly": "npm run lint && npm run build && npm run rewrite-changelog && npm test && mocha test --recursive --reporter dot && nyc mocha --recursive",
"ci-publish": "ci-publish",
"release": "standard-version -a",
"release:beta": "standard-version --prerelease beta",
Expand Down Expand Up @@ -146,7 +147,10 @@
"examples/",
"modbus/",
"docs",
"extras"
"extras",
"test/e2e/flows/**",
"test/integrations/flows/**",
"test/units/flows/**"
]
},
"funding": {
Expand Down
8 changes: 4 additions & 4 deletions src/modbus-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function (RED) {
const internalDebugLog = require('debug')('contribModbus:config:client')
const _ = require('underscore')

function ModbusClientNode(config) {
function ModbusClientNode (config) {
RED.nodes.createNode(this, config)

// create an empty modbus client
Expand Down Expand Up @@ -132,7 +132,7 @@ module.exports = function (RED) {
node.serverInfo += ' default Unit-Id: ' + node.unit_id
}
/* istanbul ignore next */
function verboseWarn(logMessage) {
function verboseWarn (logMessage) {
if (RED.settings.verbose && node.showWarnings) {
node.updateServerinfo()
node.warn('Client -> ' + logMessage + ' ' + node.serverInfo)
Expand All @@ -145,13 +145,13 @@ module.exports = function (RED) {
}
}

function verboseLog(logMessage) {
function verboseLog (logMessage) {
if (RED.settings.verbose && node.showLogs) {
coreModbusClient.internalDebug('Client -> ' + logMessage + ' ' + node.serverInfo)
}
}

function stateLog(logMessage) {
function stateLog (logMessage) {
if (node.stateLogEnabled) {
verboseLog(logMessage)
}
Expand Down
4 changes: 2 additions & 2 deletions src/modbus-flex-fc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function (RED) {
}
/* istanbul ignore next */
verboseWarn('open node ' + node.id)
const modbusClient = RED.nodes.getNode(config.server)
const modbusClient = RED.nodes.getNode(config.server)
if (!modbusClient) {
return
}
Expand Down Expand Up @@ -227,7 +227,7 @@ module.exports = function (RED) {
})

this.on('close', function (done) {
//TODO
// TODO
// node.resetAllReadingTimer()
node.removeNodeListenerFromModbusClient()
setNodeStatusWithTimeTo('closed')
Expand Down
4 changes: 2 additions & 2 deletions src/modbus-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (RED) {
const mbBasics = require('./modbus-basics')
const internalDebugLog = require('debug')('contribModbus:server')

function ModbusServer(config) {
function ModbusServer (config) {
RED.nodes.createNode(this, config)

const bufferFactor = 8
Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = function (RED) {
}
})

function buildMessage(msg) {
function buildMessage (msg) {
return [
{ type: 'holding', message: msg, payload: node.modbusServer.holding },
{ type: 'coils', message: msg, payload: node.modbusServer.coils },
Expand Down
46 changes: 0 additions & 46 deletions test/core/mocha-example-test.js

This file was deleted.

Loading

0 comments on commit 65176fa

Please sign in to comment.