By passing one argument to it
(usually called done
) and
then calling that argument when you want to finish your test.
This way you are signalling Mocha that this is an async test, and you want to wait until you call done()
to finish the test, which you will do in an event listener for the "done" event from your GeneratorService
instance.
Here's an example:
const chai = require('chai')
const EventEmitter = require('events').EventEmitter
chai.should()
class Client {
constructor() {
this.evt = new EventEmitter()
}
fireDone() {
setTimeout(() => {
this.evt.emit('done')
}, 2000)
}
}
describe('Client', function () {
// increase mocha's default timeout to 3000ms, otherwise
// the test will timeout after 2000ms.
this.timeout(3000)
const client = new Client()
it('emits done after 2000 ms', function(done) {
const now = Date.now()
client.evt.on('done', function end(value) {
(Date.now() - now).should.be.at.least(2000)
// Do more assertions here; perhaps add tests for `value`.
// Here we call done, signalling to mocha
// that this test is finally over.
done()
// remove listener so it doesn't re-fire on next test.
client.evt.removeListener('done', end)
})
client.fireDone()
})
})
Note: I replaced GeneratorService
with Client
and made it more compact for brevity.
Also, you can probably use Mocha's default 2000ms timeout limit to check that the event is indeed fired within 2 seconds, which negates the need to add the time comparison I check I've added in my example: (Date.now() - now).should.be.at.least(2000)
.