gruntjs / grunt-contrib-qunit

Run QUnit tests in Headless Chrome.
MIT License
214 stars 105 forks source link

Not able to detect assertions when using requirejs #35

Closed kctang closed 11 years ago

kctang commented 11 years ago

My qunit tests run fine when accessed from a browser but when executing them through grunt qunit, it say:

± |master ✗| → grunt qunit
Running "qunit:all" (qunit) task
Testing test/test.htmlOK
Warning: 0/0 assertions ran (15ms) Use --force to continue.

Aborted due to warnings.

The test.js file looks something like this:

require.config({
    baseUrl: "../app/"
});

require(['my-mod'], function (myMod) {
    test('simple tests', 1, function () {
        // do stuff with myMod
        ok(true, 'works.');
    });
});

Once I remove the require() functions and move the test()block to the top level of the script file, it works:

± |master ✗| → grunt qunit
Running "qunit:all" (qunit) task
Testing test/test.html.OK
>> 1 assertions passed (17ms)

Done, without errors.

Is there a workaround for this or not a supported feature?

shama commented 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.');
    });
});
hendrixski commented 10 years ago

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!

hlfcoding commented 9 years ago

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.

StephaneTrebel commented 9 years ago

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