Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine (mocha) nested "it" tests

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?

like image 864
WHITECOLOR Avatar asked Jul 13 '12 11:07

WHITECOLOR


People also ask

Can we have nested it blocks in Mocha?

Don't nest it calls.Nested it calls are never okay in Mocha.

Is Mocha better than Jasmine?

Mocha is significantly more flexible and comes with a test runner, but you have to piece it yourself. In the Angular world, Jasmine is the recommended testing framework. This is because Angular CLI, by default, comes with Jasmine and Karma as the test runner.

Does Mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.


2 Answers

I would have two tests for that. One that is testing insert and one that tests remove.

Should look something like this in coffeescript

describe 'Item model', () ->
   item = ''
   before (done) ->
      item = new Item {name: {first: "Alex"}}
      done
    describe 'When inserting Item', () ->
        before (done) ->
            item.save done
        it 'should have been insterted' ->
            #CHECK HERE IT IF IT IS INSERTED

    decribe 'when deleting', () ->
        before (done) ->
            item.save (err,data) ->
                return done err if err
                Item.delete {_id: data.id}, done
        it 'should have been deleted' ->
            #CHECK HERE IT IF IT IS Deleted
like image 136
Marcus Granström Avatar answered Oct 09 '22 10:10

Marcus Granström


See this issue: https://github.com/visionmedia/mocha/issues/438

Seems that the intention is to force tests to be de-coupled. While inconvenient and possibly requiring more mocking, this behavior is useful because it requires less re-testing and provides a clearer image of exactly what is going wrong.

i.e. there are 2 tests, test A and test B, where B is dependent on A.

Test A breaks, B therefore breaking as well. You fix what is breaking test A, but may now be surprised to find that test B broke either in the process of your fix, or for an unrelated reason.

When the tests do not rely on each other, you have better information and fewer surprises.

like image 31
narthur157 Avatar answered Oct 09 '22 10:10

narthur157