0

I am trying to write a short mocha/chai Node test for some async process, expecting it to ignore irrelevant input. It basically looks like this (compared to the test of relevant input). The problem is how do I write the second test? It's an async process that eventually does nothing, no error/success emits...

it('should process input', function(done) {
    object
    .on('success', function(result) {
       expect.result.to.equal("OK");
       done();
    })
    .asyncDoSomething('relevant input');
});

it('should ignore input', function(done) {
    object.asyncDoSomething('irrelevant input');
    // TODO: how do I verify the async process eventually did nothing?
});
Sagi Mann
  • 2,967
  • 6
  • 39
  • 72

2 Answers2

0

That's a good one - the only solution that comes to mind is to wait for a timeout and assume if it didn't happen in this time, then it will not happen. But this is not good design and needlessly slows down the test suite.

Have you thought about isolating the decision logic to somewhere where it could be tested synchronously and then make a test for that?

pspi
  • 11,189
  • 1
  • 20
  • 18
  • Well, since I have 2 events: 'success' and 'error', I was trying to think how I can maybe verify neither of them was called... Problem is, again, this is an async process so I am not sure how to test this exactly. – Sagi Mann Aug 02 '16 at 11:44
0

For the moment (still awaiting possibly better solutions?), I have updated the emitter to emit some sort of an 'ignored' event for all cases where it decides to ignore the input asynchronously. For testing, I check the "cause" of the ignore using:

expect(cause).to.equal(expectedCause)
Sagi Mann
  • 2,967
  • 6
  • 39
  • 72