Testing: Wait until Mongo indices are set up

While setting up Mocha tests, I noticed that they sometimes fail on startup because Mongo takes some time to setup its indices on the test data I am prefilling. For normal indices, this would not be noticeable, but geospatial queries fail if the geonear index is not available.

I workarounded this issue by setting a pretty large timeout (20s) before the tests are being run, but this is pretty annoying.

Is there a programmatic way to check if Mongo has already setup all indices?

1 Like

Did you already explore these?

Of course, none of these is “reactive” in any way, i.e. no callback will conveniently fire once all desired indexes are created, but even some ugly polling at 1-2s interval is better than a 20 seconds wait timeout.

1 Like

Thanks. Yes, I ended up using indexExists() and polling now.