grunt not running QUnit tests on phantom

前端 未结 2 1223
無奈伤痛
無奈伤痛 2020-12-11 05:38

I\'ve got a repository which is integrated with travis. I\'ve got QUnit tests which I\'d like to run from grunt/node server side and AMD (requirejs). This is the source of m

相关标签:
2条回答
  • 2020-12-11 06:14

    It's because the bridge that is injected into the page by grunt qunit is placed there before qunit is loaded by requirejs.

    And it needs to be after. So your tests probably run, but grunt qunit does not know about it because it does not report back.

    I did a quick test placing the bridge code at the end in your qunit extend module and it worked fine.

    You could probably create a qunit bridge module and call that as well in your qunit extend or similar.

    The code from the official bridge should work fine. Just make sure it's fetched after qunit.

    Grunt qunit will still inject the script but just fail since QUnit is undefined, but probably won't do any harm to your tests.

    0 讨论(0)
  • 2020-12-11 06:26

    I am using grunt-contrib-qunit to run QUnit tests via grunt. It uses phantomjs internally.

    I was getting the same error as the OP after upgrading grunt-contrib-qunit to the latest version (0.7.0):

    PhantomJS timed out, possibly due to a missing QUnit start() call.

    To fix this problem, I had to first load QUnit via require() and then execute QUnit.start() and define all my QUnit modules and tests after that.

    The HTML file looks something like this:

    <!DOCTYPE html>
    <html>
    <head>
        <title>QUnit + RequireJS + PhantomJS</title>
        <link rel="stylesheet" href="lib/qunit/qunit/qunit.css">
    </head>
    <body>
        <div id="qunit"></div>
        <div id="qunit-fixture"></div>
        <script src="lib/requirejs/require.js"></script>
        <script src="mytests.js"></script>
    </body>
    </html>
    

    Then the mytests.js file:

    require.config({
        paths: {
            'qunit': 'lib/qunit/qunit/qunit'
        }
    });
    
    require(['qunit'], function(QUnit) {
    
        QUnit.start();
    
        QUnit.module('My Module');
    
        QUnit.test('some normal test', function(assert) {
    
            assert.ok(true, 'can run a normal QUnit test');
        });
    
        QUnit.test('some asynchronous test', function(assert) {
    
            var done = assert.async();
    
            setTimeout(function() {
    
                assert.ok(true, 'can run an asynchronous QUnit test');
                done();
    
            }, 50);
        });
    });
    
    0 讨论(0)
提交回复
热议问题