# Unit Test calles multiple times done

**URL:** <https://discourse.nodered.org/t/unit-test-calles-multiple-times-done/46991>\
**Category:** Developing Nodes\
**Created:** [10 June 2021 15:30 UTC](https://discourse.nodered.org/t/unit-test-calles-multiple-times-done/46991 "2021-06-10T15:30:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TruFox](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@TruFox](https://discourse.nodered.org/u/TruFox)\
**Post date:** [10 June 2021 15:30 UTC](https://discourse.nodered.org/t/unit-test-calles-multiple-times-done/46991/1 "2021-06-10T15:30:27Z")

</div>

Hello, i have a problem with a unit test.  
The node i develope sends msg in a loop like this:

```auto
for( let i = 0; i < array.lenght; i++){
      node.send([array[i], null, null,]);
}

```

Unit Test:

```auto
it('should get data (Array)', function (done) {
            const flow = getDefaultFlow();
            helper.load([CheckNode, serverNode], flow, function () {
                var nCheck = helper.getNode("Check");
                var nhelp = helper.getNode("Help");

                nCheck .receive({
                    payload: false
                });

                nhelp.on("input", function(msg) {
                    try {

                        msg.should.have.property('topic', "Data");
                        done();
                    } catch (err) {
                        console.log('err ' + err);
                        done(err);
                    }                
                });

                nCheck.receive({
                    payload: true
                });
            });
        });

```

My problem is that if i just send only one msg in the loop the test is working, but if i send multiple msg in the loop i get the error:  
**done() called multiple times**

Can somebody tell me what i`m doing wrong?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 June 2021 15:38 UTC](https://discourse.nodered.org/t/unit-test-calles-multiple-times-done/46991/2 "2021-06-10T15:38:08Z")

</div>

Hi @TruFox

The handler for the `input` event is called for each message you send that node. Inside that handler you check the value of the `topic` property and then call `done()`.

So yes, if you send multiple messages, you will end up calling `done()` multiple times.

A different approach is to push each message into an array within the `input` handler, and only once you have received the number you expect, loop through the array checking their contents and then call `done` at the end.

---

<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:** [9 August 2021 15:38 UTC](https://discourse.nodered.org/t/unit-test-calles-multiple-times-done/46991/3 "2021-08-09T15:38:21Z")

</div>

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