Mocha: await -require

Created on 20 Sep 2017  路  7Comments  路  Source: mochajs/mocha

There's a script populating the database before tests.
There are tests manipulating on data in the database.
And those manipulations are deterministic therefore dealing with exact certain rows and exact certain values.
Those values are taken from the database (could be random as well), but since the process of taking the values is asynchronous the tests don't wait for it and proceed with executing themselves therefore failing.
Is there any way to wait for some asynchronous code before the tests run.

question

All 7 comments

You can make the tests asynchronous either using a callback or using promises*. I don't know which database library/API you're using, but it probably uses either callbacks or promises also, and I'd recommend using the same technique in your tests that the code being tested uses. (Note also that async functions are just syntactic sugar over promise chains and follow the same principles.) If you need to get the values before the tests, but the tests will always be the same, the same technique can be used in before and beforeEach. If the tests need different names or a different number of tests depending on the values then there is the --delay option.

Please let us know if that's what you needed!

* The example here showcases chai-as-promised, but you can use then to get the value and then just perform your actions on the value in the then callback.

Thanks for the detailed response.

So you're saying that beforeEach can be async and that each test will wait for it to complete before starting?

The setTimeout thing won't do because it's magic.
It could be something like loadData().then(() => describe(...)) but doing it in every test file would not do.

So you're saying that beforeEach can be async and that each test will wait for it to complete before starting?

Yes; any asynchronous hook or test will wait for completion before anything else runs.

The setTimeout thing won't do because it's magic.

setTimeout is just used for example of how the Mocha behavior and functions work. Substitute your own API for setTimeout.

It could be something like loadData().then(() => describe(...)) but doing it in every test file would not do.

Using --delay and run() with multiple test files should work, but I haven't double-checked recently because I rarely have to use --delay at all since beforeEach (or even just putting the asynchronous stuff in the test itself) tends to be sufficient and is simpler.

Yes; any asynchronous hook or test will wait for completion before anything else runs.

Oh, that's cool. Closing this issue then.
It's still not a global asynchronous require and requires adding that code to every test file which is a lot of copy-pasta.

If it needs to be the same in all the tests, you can actually put before or beforeEach outside of any describe and it should not need to be in multiple files: https://mochajs.org/#root-level-hooks We don't usually recommend this because global variables or behaviors are generally a bad idea in testing just like they're generally a bad idea outside of testing; but if you're literally copy-pasting the same hook into every describe then it might fit what you want to do.

Oh wow, that concludes it then.
Thanks for helping.

You're welcome!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eschwartz picture eschwartz  路  3Comments

Swivelgames picture Swivelgames  路  3Comments

wzup picture wzup  路  3Comments

robertherber picture robertherber  路  3Comments

enigmatic00 picture enigmatic00  路  3Comments