Mocking a dependent library in a UnitTest

I'm developing a node that uses a library to call out to an external system. To unit test I need to mock it. What do I do? My node is roughly like this:

const backend = require('./theDarkSide`);
module.exports = function (RED) {
 function LightSaber(config) {
    const node = this;
    node.color = config.color || 'RED';
    node.on('input', (msg, send, done) => {
       backend.fight(node.color, msg.payload.jedi)
                    .then(result => {
                     // catch omitted here
   RED.nodes.registerType('light-saber", LightSaber);

There are lots of ways you could do this but it isn't really a Node-RED question since you can simply use standard node.js ways. Either replace theDarkSide.js with a mock function that returns an object or mock the call in your input function.

I propose you check the documentation of node-red-node-test-helper. It's a brilliant source of knowledge how to unit test a node.

1 Like

Did that, proposed a PR in the documentation, since the test-helper has evolved

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.