Bluebird Each loop in Mocha not working

前端 未结 2 1056
深忆病人
深忆病人 2021-01-16 22:59

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

2条回答
  •  悲哀的现实
    2021-01-16 23:42

    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:

    1. Read all your test files and execute them. The callbacks passed to describe are executed right away. The callbacks passed to it and to the hooks (before, beforeEach, etc.) are recorded for later execution.

    2. 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.

提交回复
热议问题