Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine specs not being executed when injecting dependencies with Squire.js

I am attempting to run some Jasmine unit tests for a Backbone view, mocking out dependencies in Squire.

The dependencies of my view are a Baseview, an ICanHaz template and an i18n translation.

I mock out the dependencies after defining Squire and Backbone, and then use a Squire injector to require my view. However, when I run the tests through Grunt, I get the warning message:

Warning: No specs executed, is there a configuration error? Use --force to continue.

Here is my spec:

define(['squire', 'backbone'], function (Squire, Backbone) {
    var injector = new Squire();

    mocks = {
        'views/baseview': function () {
            return Backbone.View.extend({
                grabTemplate: function (options) { }
            });
        },
        'text!templates/menu.htm': '',
        'i18n!nls/menu': {}
    };

    injector.mock(mocks);

    injector.require(['menu'], function (Menu) {

        describe('Menu View', function () {

            it('should be initialisable', function () {
                var menu = new Menu();
                expect(menu).toBeDefined();
            });
        });
    });
});

Does anyone know why my basic unit test is not getting picked up?

like image 540
Simon Adcock Avatar asked Dec 06 '25 05:12

Simon Adcock


1 Answers

After a bit of trial and error, I found a solution I'm vaguely happy with. I modified the solution from this post, utilising Jasmine's run and waitsFor calls:

define(['squire', 'backbone'], function (Squire, Backbone) {

    describe('Menu View module', function () {

        it('should be initialisable', function () {

            var injector = new Squire(),
            mocks,
            menu,
            loaded = false; //track whether our module has loaded

            mocks = {
                'views/baseview': function () {
                  return Backbone.View.extend({
                      grabTemplate: function (options) { }
                  });
                },
                'text!templates/menu.htm': '',
                'i18n!nls/menu': {}
            };

            injector.mock(mocks);

            injector.require(['views/menu'], function (Menu) {
                menu = new Menu();
                loaded = true;
            });

            waitsFor(function() {
                return loaded;
                //when this is true, we'll drop out of waitsFor
            }, 'Menu module not loaded', 10000);

            runs(function() {
                expect(menu).toBeDefined();
            });
          });
    });
});
like image 174
Simon Adcock Avatar answered Dec 08 '25 19:12

Simon Adcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!