Hktplsm Hktplsm - 4 months ago 20
Node.js Question

Loop in a callback returned array using Mocha

i'm trying to achieve something like this :

describe("TEST",function() {

Offer.find({},{_id:1, title:1}).exec(function(error, offers) {

for (var i = 0; i < offers.length; i++) {
it("Ask transaction : " + offers[i].title, function(done) {
// do something with offers[i];
}
}
...


But Mocha does not even detect a test in the file. Why ?

Answer

Every test case starts with the it("", function(){ /* write test code here */ } ) code block.

If you are looking at performing some test setup like inserting data then you can use the before function to do those.

Example:

describe("TEST",function() {
   before(function() {
       // runs before all tests in this block
   });
   it("should blah", function(done) {
       // Your test case starts here.
   }
}

There are examples in Mocha's official site you can refer to;

See:
https://mochajs.org/