Propagate errors when testing

Testing with Mocha requires errors raised within flows to propagate
to the test runner, not getting swallowed by Node.

This commit tests whether Mocha's global functions exist and,
if they do, propagates exceptions that are raised there.
This commit is contained in:
Matthias Urlichs 2024-11-13 16:37:56 +01:00
parent e48607c743
commit c25ada8dcb
3 changed files with 9 additions and 0 deletions

View File

@ -41,6 +41,7 @@
"cors": "2.8.5", "cors": "2.8.5",
"cronosjs": "1.7.1", "cronosjs": "1.7.1",
"denque": "2.1.0", "denque": "2.1.0",
"detect-mocha": "^0.1.0",
"express": "4.21.1", "express": "4.21.1",
"express-session": "1.18.1", "express-session": "1.18.1",
"form-data": "4.0.0", "form-data": "4.0.0",

View File

@ -21,6 +21,7 @@ const flowUtil = require("./util");
const context = require('../nodes/context'); const context = require('../nodes/context');
const hooks = require("@node-red/util").hooks; const hooks = require("@node-red/util").hooks;
const credentials = require("../nodes/credentials"); const credentials = require("../nodes/credentials");
const detectMocha = require("detect-mocha");
let Subflow; let Subflow;
let Log; let Log;
@ -804,6 +805,9 @@ function deliverMessageToDestination(sendEvent) {
} catch(err) { } catch(err) {
Log.error(`Error delivering message to node:${sendEvent.destination.node._path} [${sendEvent.destination.node.type}]`) Log.error(`Error delivering message to node:${sendEvent.destination.node._path} [${sendEvent.destination.node.type}]`)
Log.error(err.stack) Log.error(err.stack)
if (detectMocha()) {
throw err;
}
} }
} }
} }

View File

@ -21,6 +21,7 @@ var redUtil = require("@node-red/util").util;
var Log = require("@node-red/util").log; var Log = require("@node-red/util").log;
var context = require("./context"); var context = require("./context");
var flows = require("../flows"); var flows = require("../flows");
var detectMocha = require("detect-mocha");
const hooks = require("@node-red/util").hooks; const hooks = require("@node-red/util").hooks;
@ -218,6 +219,9 @@ Node.prototype._emitInput = function(arg) {
); );
} catch(err) { } catch(err) {
node.error(err,arg); node.error(err,arg);
if (detectMocha()) {
throw err;
}
} }
} else if (node._inputCallbacks) { } else if (node._inputCallbacks) {
// Multiple callbacks registered. Call each one, tracking eventual completion // Multiple callbacks registered. Call each one, tracking eventual completion