# Mocking a dependent library in a UnitTest

**URL:** <https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728>\
**Category:** Developing Nodes\
**Tags:** node-red-node-test-helper\
**Created:** [9 November 2023 04:16 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728 "2023-11-09T04:16:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stwissel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stwissel/32/126_2.png) [@Stwissel](https://discourse.nodered.org/u/Stwissel)\
**Post date:** [9 November 2023 04:16 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728/1 "2023-11-09T04:16:56Z")

</div>

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:

```js
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 => {
                          send(result);
                          done();
                      });
                     // catch omitted here
         });
   }
 }
   RED.nodes.registerType('light-saber", LightSaber);
}

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 November 2023 08:59 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728/2 "2023-11-09T08:59:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [9 November 2023 09:35 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728/3 "2023-11-09T09:35:42Z")

</div>

> [@Stwissel](#):
>
> What do I do?

I propose you check the documentation of [node-red-node-test-helper](https://github.com/node-red/node-red-node-test-helper). It's a brilliant source of knowledge how to unit test a node.

---

<div class="post-metadata">

**Author:** ![Stwissel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stwissel/32/126_2.png) [@Stwissel](https://discourse.nodered.org/u/Stwissel)\
**Post date:** [22 November 2023 12:05 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728/4 "2023-11-22T12:05:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 December 2023 12:06 UTC](https://discourse.nodered.org/t/mocking-a-dependent-library-in-a-unittest/82728/5 "2023-12-06T12:06:41Z")

</div>

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