So, I am testing a component that relies on an event source. For this, I proposed a solution using Promises with Mocha + Chai:
it('should transition with the correct event', (done) => { const cFSM = new CharacterFSM({}, emitter, transitions); let timeout = null; let resolved = false; new Promise((resolve, reject) => { emitter.once('action', resolve); emitter.emit('done', {}); timeout = setTimeout(() => { if (!resolved) { reject('Timedout!'); } clearTimeout(timeout); }, 100); }).then((state) => { resolved = true; assert(state.action === 'DONE', 'should change state'); done(); }).catch((error) => { assert.isNotOk(error,'Promise error'); done(); }); });
In the console, I get "UnhandledPromiseRejectionWarning", although the reject function is called because it instantly displays the message "AssertionError: Promise Error"
(node: 25754) UnhandledPromiseRejectionWarning: raw promise reject (reject ID: 2): AssertionError: promise error: expected {Object (message, showDiff, ...)} to be false 1) must go with the correct event
And then, after 2 seconds, I get
Error: Exceeded latency 2000 ms. Make sure the done () callback is called in this test.
What's even weirder, since the catch callback was executed (I think for some reason the assert error prevented the completion of execution)
The funny thing is, if I comment out assert.isNotOk(error...) , the test will work without any warning in the console. He still “fails” in the sense that he is doing the job.
But still I can not understand these errors with a promise. Can anyone enlighten me?
Jzop Sep 27 '16 at 5:23 2016-09-27 05:23
source share