# Testing functions required by my node

**URL:** https://discourse.nodered.org/t/testing-functions-required-by-my-node/16564
**Category:** General
**Created:** [10 October 2019 16:48 UTC](https://discourse.nodered.org/t/testing-functions-required-by-my-node/16564 "2019-10-10T16:48:20Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![serardica](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/serardica/32/49093_2.png) [@serardica](https://discourse.nodered.org/u/serardica)
#### Post date: [10 October 2019 16:48 UTC](https://discourse.nodered.org/t/testing-functions-required-by-my-node/16564/1 "2019-10-10T16:48:21Z")

</div>

I have a node into a module like below:

```auto
module.exports = function(RED) {
//requires and global vars here
var host = RED.settings.host || "localhost";
var request;

    function myNode(config) {
        this.request = config.request;
        parseJSON(request);
        // do more things here
    }
    RED.nodes.registerType("my node", myNode);

    function parseRequest(request) {
        let json = {};
        try {
            json = JSON.parse(request);
        } catch (e) {
            node.error(RED._("parsing error occur", {location: "parse"}), request );
        }
        return json;
    }
}

```

I'm trying to do some unit test for the above node through [node-red-node-test-helper](https://github.com/node-red/node-red-node-test-helper) and I wonder if there is a way to test the functions needed by the node "myNode" in this case "parseRequest". There could be more complex functions than the one I used on the example so, considering that scenario, if in my test file code I want to do a unit test for that "parseRequest" function, I face some obstacles:

1. I have no way of invoke the "parseRequest" function since the test file, when try to do something like the below, does not recognize like a function.

```auto
var mynode = require("myNode.js");
mynode.parseRequest({id:1, name:"qwerty"});

```

1. my exported module need an instance of RED object as parameter.
2. the only way I can access that function is if I take it out from the node module and exports it separately, but can trigger another error because of the use of RED into the catch.

I hope you understand my point here  
Thanks for the help

---

<div class="post-metadata">

### Author: ![miker256](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/miker256/32/13042_2.png) [@miker256](https://discourse.nodered.org/u/miker256)
#### Post date: [11 October 2019 21:26 UTC](https://discourse.nodered.org/t/testing-functions-required-by-my-node/16564/2 "2019-10-11T21:26:48Z")

</div>

This should hopefully help you out. 😄

[https://nodered.org/docs/creating-nodes/first-node#unit-testing](https://nodered.org/docs/creating-nodes/first-node#unit-testing)

> <https://stackoverflow.com/questions/38206965/how-to-properly-test-node-red-nodes>
