Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJs loading modules - Qunit

I am pretty new to RequireJS, and having trouble in writing QUnit to a source code which has logic to load modules dynamically using requireJS.

Below is the source code: factory/Factory.js *

getPage: function (callback) {

    //doSomething here

    require(['page/something'], function() {

         callback();

    })
}

The module 'page/something' is never loaded while running QUnit, and callback is never invoked. Is there anything I'm missing here? Appreciate your response.

**QUnit factory/FactoryTests.js*

define(['underscore', 'factory/Factory'],
       function (_, Factory) {

           module("Factory", {
               setup:function () {
               },
               teardown:function () {
               }
           });

           test("GetPage", function () {
              var isCallbackInvoked = false;
              var mockCallback = function () {
                  isCallbackInvoked = true;
              }

              Factory.getPage(mockCallback);
              ok(isCallbackInvoked);

           });

});

*test-require-config.js**

require.config({

    baseUrl: "../../resources/js",

    paths:{
        jquery:'../jquery-1.8.2',
    jquery_star_rating : '../jquery/jquery.rating',
        underscore:'..underscore-1.4.1',
        backbone:'../backbone-0.9.2',
        jquery_star_rating : '../jquery.rating',
        text : '../require-text-2.0.3',
        sinon: '../../../../sinon',
    },
    shim:{
        underscore:{
            exports:'_'
        },
        backbone:{
            deps:["jquery", "underscore"],
            exports:"Backbone"
        }
        jquery_star_rating : {
            deps : ['jquery']
        }
    }

});

var dependencies = [
    'jquery',
    'jquery_star_rating',
    'underscore',
    'backbone',       
    'sinon',
];

require(dependencies, function () {

    require(['../../../../test/js/testsuite'], function(suite){

    })

});

testsuite.js

function () {

    QUnit.config.autostart = false;

    var testModules = [       
        "factory/FactoryTests.js"
    ];

    require(testModules, QUnit.start);
}());

Thanks!!

like image 264
user1861464 Avatar asked Feb 18 '23 08:02

user1861464


1 Answers

First, a clarification: What does your QUnit test page look like? I'm guess it's either listing zero tests, or is a blank page.

If that's the case, I had a lot of trouble with the same thing. The "right" answer is exactly what you are doing. But after a lot of stepping through code, in my setup, QUnit was still starting before any of my tests were defined, despite setting QUnit.config.autostart = false.

At the end of testsuite.js, try calling QUnit.load() instead of QUnit.start() (you can probably drop the autostart = false, too). This is an undocumented function, but it was the only thing that worked for me. I believe it's the function QUnit attaches to the onLoad event by default. Unfortunately, with RequireJS, the onLoad triggers before most of your js files have loaded.

like image 182
keithjgrant Avatar answered Feb 28 '23 03:02

keithjgrant