Closed kctang closed 11 years ago
I believe this is because qunit is starts before requirejs has a chance to load the tests. So you need to wrap them and delay start qunit:
// test.js
QUnit.config.autostart = false;
require(['test.all.js'], function() {
QUnit.start();
});
// test.all.js
require(['my-mod'], function (myMod) {
test('simple tests', 1, function () {
// do stuff with myMod
ok(true, 'works.');
});
});
I wasn't able to put this together until I looked at the code in maxzhang's post (not in English).
In case anybody else is confused, I hope the information below helps close the loop on the information above.
I had gotten qunit + require.js to work in the browser by putting qunit in a shim with autostart=false; like this:
shim: {
'QUnit': {
exports: 'QUnit',
init: function() {
QUnit.config.autostart = false;
But that only worked in the browser but not in phantomjs + grunt-bower-config. When I ran it in grunt I got: "PhamtomJS timed out, possibly due to a missing QUnit start() call." Even though I clearly had a QUnit.start() call, and my logs showed that it was getting executed. So I removed qunit from my require.js dependencies, removed the shim shown in the code sample above, and placed it in a SCRIPT tag, then moved the autostart=false into its own script tag, like this:
<html><head>
<link rel="stylesheet" href="bower_components/qunit/qunit/qunit.css">
<script src="bower_components/qunit/qunit/qunit.js"></script>
<script>QUnit.config.autostart = false; </script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script type="text/javascript" src="bower_components/sinonjs/sinon.js"></script>
<script type="text/javascript" src="bower_components/requirejs/require.js" data-main="js/test.js"></script>
</body>
</html>
oh, and I removed "qunit" from the define() dependencies in my unit test js files, so that I wasn't loading qunit twice (which caused an extra failed test to show up) and BANG! it worked!
I don't believe this works anymore. At least not for my case using v0.7.0. Last good version for me is v0.5.1.
Same issue here, rolled back to 0.5.1 and it works just fine
UPDATE: actually I had to inline QUnit.config.autoload = false;
too
My qunit tests run fine when accessed from a browser but when executing them through
grunt qunit
, it say:The test.js file looks something like this:
Once I remove the
require()
functions and move thetest()
block to the top level of the script file, it works:Is there a workaround for this or not a supported feature?