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!!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With