RocketChat / Rocket.Chat.js.SDK

Utility for apps and bots to interact with Rocket.Chat via DDP and/or API
MIT License
136 stars 95 forks source link

Problem with asynchronous test assertions #1

Closed timkinnane closed 6 years ago

timkinnane commented 6 years ago

I had to disable the following tests because they were breaking the next set of tests. I think the assertions related to connection timeouts that should return an error, are actually returning null. However, due to the structure of the test, the test resolves before the assertion is made, so the assertion fails during following tests that have nothing to do with the original error. Need to rewrite and enable these, but it's only a testing issue, the actual methods work.

https://github.com/RocketChat/Rocket.Chat.js.SDK/blob/665a0d5c2de285cdd7dd051a352056b22c341be6/src/lib/driver.spec.ts#L18-L104

mikaelmello commented 6 years ago

This happens because inside driver.connect, whenever a connection reaches the timeout, it calls the callback (or rejects the Promise), however the Promise is still there executing, with events.once('connected') waiting for a connection

Therefore, when the next tests tries to connect and succeeds, all the previous tests that were waiting for a connected event get one each in the 4 following tests.

Fixed by removing the listener once the connection times out and not even adding it in the first place if the connection had already timed out.