3

I'm using grunt-contrib-jasmine to test my AMD modules. Out of the box it seems like the tests are affecting eachother.

The output doesn't separate tests by file. This can be verified by logging something in a tests beforeEach. The same callback is executed for all test files, in all tests.

How can I make the tests isolated from eachother i.e. separated by test spec files? Is the only solution to add another level of nesting?

grunt config

options: {
    specs: 'test/specs/unit/**/*spec.js',
    keepRunner: true,
    template: require('grunt-template-jasmine-requirejs'),
    templateOptions: {
        requireConfig: requireConfig
    }
}

sample1.spec.js:

define(['Squire', 'sinon'], function(Squire, sinon){
    'use strict';

    var sut,
        injector,
        fakeServer;

    beforeEach(function(done){
        fakeServer = sinon.fakeServer.create();
        console.log('create fake server'); // this is logged for all test files
        injector = new Squire(); 
        injector.require(['core/http-service'], function(httpService) {
             sut = httpService; 
             done();  
        });  
    }); 

    afterEach(function(){
        fakeServer.restore();
        injector.remove(); 
    }); 

    it('', function(){
        expect(1).toBe(1);
    });
});
Johan
  • 35,120
  • 54
  • 178
  • 293
  • Are you nesting any more tests apart from the example you're showing here? – fnune Oct 03 '16 at 10:26
  • There are several `x.spec.js` test files with 1-2 levels of `describe()`. Do I really need to have _all_ my tests in the same spec in order for this plugin to work? – Johan Oct 03 '16 at 10:29
  • Maybe you're using a `beforeEach` or `afterEach` in a describe which contains nested `describe`s, expecting them to only affect the inner `describe`? Might be obvious but it's worth a check. – fnune Oct 03 '16 at 10:34
  • My last idea is that the problem must be coming from the template, because everything else looks fine, I can't figure out anything else after checking the docs for `grunt-contrib-jasmine` – fnune Oct 03 '16 at 10:50
  • @FaustoNA Yeah, it's probably due to the template being "flat". I'll dig deeper. Thanks – Johan Oct 03 '16 at 10:58
  • Post the answer here if you ever find it out. – fnune Oct 04 '16 at 08:01
  • which version of jasmine are using? older than 2.4.0? maybe it's a similar issue as reported here https://github.com/kensho/ng-describe/issues/81 that was apparently due to jasmine bug? – jakub.g Oct 05 '16 at 12:17
  • @jakub.g `"grunt-contrib-jasmine": "^1.0.3"` which I think uses Jasmine 2.5.1. At least that's what I see if I open the generated _SpecRunner.html – Johan Oct 05 '16 at 12:25

1 Answers1

3

The problem is that you have a beforeEach and an afterEach outside of a describe. That means they will be called before and after each and every test that grunt-contrib-jasmine finds.

If you want them to be used only for the it inside your define-ed module, you need to put them inside a describe:

define(['Squire', 'sinon'], function(Squire, sinon){
    'use strict';

    describe('some description', function(){

        var sut,
            injector,
            fakeServer;

        beforeEach(function(done){
            fakeServer = sinon.fakeServer.create();
            console.log('create fake server'); // this is logged for all test files
            injector = new Squire(); 
            injector.require(['core/http-service'], function(httpService) {
                 sut = httpService; 
                 done();  
            });  
        }); 

        afterEach(function(){
            fakeServer.restore();
            injector.remove(); 
        }); 

        it('', function(){
            expect(1).toBe(1);
        });
    });
});
cartant
  • 57,105
  • 17
  • 163
  • 197
  • Yeah, as I thought then. I was just curious is there was a way to tell the plugin to separate them by name without the nesting. Thanks for verifying. BTW, you can't get the bounty for another 23 hours :) – Johan Oct 05 '16 at 12:46