0

I'm trying my hand at writing unit tests with mocha and chai, however I'm running into an issue where I'm getting timeout errors instead of actual errors being thrown and I can't seem to find a workaround.

Error I'm getting:

  1) Unit Tests for Networks Array
 should check that networks array is not empty:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

In my code I've made it that the opposite of what I'm testing for is true, so I have an expected failure.

Is there a standard way of fixing this sort of thing?

describe('Unit Tests for Networks Array\n', function () {

    it('should check that networks array is not empty', function (done) {
        git.getNetworks(function (networks) {
            expect(networks).to.be.empty();  // expected failure
            done();
        });
    });
Juan Picado
  • 1,823
  • 18
  • 33
  • that code should works. I ignore if the **git.getNetworks** it's a promise or not, but in that case, you are missing the use case, what happens if that callback is never resolved? Can you provide more info? – Juan Picado Oct 17 '16 at 14:39
  • Yes, the git.getNetworks is a promise. And it just times out when it doesn't resolved. I would like an error to be thrown instead. –  Oct 17 '16 at 14:41

1 Answers1

0

Promises works in this way, you can rejected or resolve it. But, if there is an error within your promise logic and you don't catch the error because it's swallowed and silently dies and will never show up.

git.getNetworks.then(function() { 
/* do something with the result */
}).catch(function() {
 /* error :( */
})

Ensure you are covering the rejection callback (optional) and the catch (always), otherwise the Promise silenty throws and error and that's why Mocha sends timeout

Juan Picado
  • 1,823
  • 18
  • 33
  • I ran it like this ` it('should check that networks array is not empty', function (done) { git.getNetworks(function (networks){}.then(function() { expect(networks).to.be.empty(); done() }).catch(function(){ } )); });` . But I got a (intermediate value).then is not a function. –  Oct 17 '16 at 15:27
  • then gitNetworks is not a Promise – Juan Picado Oct 17 '16 at 15:29