Jasmine (mocha) nested "it" tests

Viewed 14526

I was trying to test consequent create/delete of items (in mongoDB via mongoose).

The problem that creating is async and it returns ID of created item in callback function, I need this ID to deleted created item, so I tried the following code for mocha (in different ways) but it didn't work.

describe('Item Model', function(){

  it('should be able to create item', function(done){
    var item = new Item({name: {first: "Alex"});
    item.save(function(err, data){

      it('should be able to deleted created item', function(done){                    
        Item.delete({_id: data.id}, function(err, data){
        done(err);
        });
      });

    })
  });

});

Can such test be implemented in mocha or jasmine?

3 Answers

I've had a similar issue and when I tried a nested describe/it structure it actually worked.

describe('Item Model', () => {
   it('should be able to create item', done => {
      var item = new Item({name: {first: "Alex"});
      item.save(function(err, data){
         describe('Item Delete', () => {
            it('should be able to deleted created item', done => {
               Item.delete({_id: data.id}, function(err, data){
                 done(err);
               });
            });
         });
      }); 
   });
});
Related