grunt not running QUnit tests on phantom

懵懂的女人 提交于 2019-11-28 12:18:43

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);
    });
});

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!