简体   繁体   English

grunt没有在幻像上运行QUnit测试

[英]grunt not running QUnit tests on phantom

I've got a repository which is integrated with travis. 我有一个与travis集成的存储库 I've got QUnit tests which I'd like to run from grunt/node server side and AMD (requirejs). 我有QUnit测试,我想从grunt / node服务器端和AMD(requirejs)运行。 This is the source of my AMD init.js: 这是我的AMD init.js的来源:

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

    require(["../test/suites/basic",
        '../test/qunit-extend',
        'qunit'
    ], function(BasicTests) {
        QUnit.config.autoload = false;
        QUnit.config.autostart = false;
        BasicTests.run();
        QUnit.load();
        QUnit.start();
    });
}());

When I run those QUnit tests within my browser - everything works perfectly. 当我在浏览器中运行这些QUnit测试时 - 一切都运行良好。 But when I try to run them from grunt level (server-side using phantomjs), it fails . 但是当我尝试从grunt级别(服务器端使用phantomjs)运行它们时, 它失败了 I get: 我明白了:

Running "qunit:all" (qunit) task
Testing test/index.html 
Warning: PhantomJS timed out, possibly due to a missing QUnit start() call. Use --force to continue.

all the time. 每时每刻。 I was trying to do evetyrhing the same way as it's done in this tutorial , but still I get wrong results (phantom being hanged instead serving QUnit tests)... 我试图按照本教程中的方式进行evetyrhing,但仍然得到错误的结果(幻影被绞死而不是服务QUnit测试)...

I am using grunt-contrib-qunit to run QUnit tests via grunt. 我正在使用grunt-contrib-qunit通过grunt运行QUnit测试。 It uses phantomjs internally. 它在内部使用phantomjs。

I was getting the same error as the OP after upgrading grunt-contrib-qunit to the latest version (0.7.0): 将grunt-contrib-qunit升级到最新版本(0.7.0)后,我得到了与OP相同的错误:

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. 要解决这个问题,我必须首先通过require()加载QUnit,然后执行QUnit.start()并在此之后定义我的所有QUnit模块和测试。

The HTML file looks something like this: HTML文件看起来像这样:

<!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: 然后是mytests.js文件:

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. 这是因为是由咕噜qunit注入的页面放在那里之前qunit由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. 所以你的测试可能会运行,但是grunt qunit不知道它,因为它没有报告。

I did a quick test placing the bridge code at the end in your qunit extend module and it worked fine. 我做了一个快速测试,将桥代码放在你的qunit扩展模块的最后,它工作正常。

You could probably create a qunit bridge module and call that as well in your qunit extend or similar. 您可以创建一个qunit桥接模块,并在您的qunit扩展或类似中调用它。

The code from the official bridge should work fine. 官方桥梁的代码应该可以正常工作。 Just make sure it's fetched after qunit. 确保在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. Grunt qunit仍然会注入脚本但是因为QUnit未定义而失败,但可能不会对您的测试造成任何伤害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM