Test for single-instance

20 views
Skip to first unread message

Reiner Ebel

unread,
Mar 27, 2019, 8:34:15 AM3/27/19
to Mocha
Hello,

I'm doing my first tests with mocha.

I use the node-single-instance and would like to write a test, but I can not find a beginning.

The following tests should be carried out:
Success and error case with 2 equal instances

Here is my Code with the single-instance:

const locker = new singleInstance("MY_SERVICES");                      // Instanz Name
locker.lock()                                                           // Überwachung der Instanzen
    .then(() => {
        const start = new Main();
        start.init();
    })
    .catch((err) => {
        logger.default.error("Eine Instanz dieses Services wurde schon gestartet!");
    });




How can I build my test? Can you help me here?

Thank you

Reiner

Vlad GURDIGA

unread,
Mar 28, 2019, 5:08:16 AM3/28/19
to Mocha
Hey Reiner,

It’s a little hard for me to understand the whole context, but it seems like you’re trying to test some promise-based code. If that is the case, then you’re lucky because Mocha has built-in support for testing promise-based code, check the documentation here: https://mochajs.org/#working-with-promises.

Let me see if I correctly understand your question: you want to verify that when lock succeedes then the start.init() is called and when the lock fails, the error is logged?

Reiner Ebel

unread,
Mar 28, 2019, 5:56:38 AM3/28/19
to Mocha
Hi Vlad,

You understood it right. I want to test both scenarios - success and failure with this code.
At the moment, I do not really understand how I can do that with Mocha. These are my first touches with Tests.

Thanks

Reiner

Vlad GURDIGA

unread,
Mar 29, 2019, 2:49:33 PM3/29/19
to Mocha
Hey Reiner,

Since this code doesn’t do anything but create a promise and attach success and failure handlers to it, I would say that it is not worth testing because you would test the built-in implementation of Promises. 🤷🏻‍♂️

I imagine this may sound confusing to you now, and I am sorry for that, but you need to first read some articles on basic unit testing. Here is a Google query I have verified to yield useful articles: unit testing 101 js mocha. 🤓

Good luck!
Reply all
Reply to author
Forward
0 new messages