I am writing a test where I need to loop over the results of an async api call and dynamically make mocha \'Its\' to test each iteration of the response. I found some other
The version of your test that tries to generate tests by calling it
inside getter.each
cannot work because Mocha has no provisions for generating tests asynchronously which is what you are trying to do. As I've explained here:
What you get has to do with how Mocha discovers your test. Basically Mocha does this:
Read all your test files and execute them. The callbacks passed to
describe
are executed right away. The callbacks passed toit
and to the hooks (before, beforeEach
, etc.) are recorded for later execution.Mocha executes what it has recorded for later execution (according to some sensible order which is not important here).
The problem with trying to generate tests asynchronously is that by the time the asynchronous code executes you're out of your describe
block, and the tests are ignored. There is no way to tell Mocha to wait for an asynchronous operation before it considers a describe
block over.