Search code examples
jqueryjsonajaxgruntjsqunit

Grunt did not run qunit test with ajax calls


I created QUnit test to ensure that my JSON loaded and parsed to array correctly via AJAX, it all works fine in browser, but when i run it via Grunt i get error:

Running "qunit:files" (qunit) task
Testing test/index.html ...
>> PhantomJS timed out, possibly due to:
>> - QUnit is not loaded correctly.
>> - A missing QUnit start() call.
>> - Or, a misconfiguration of this task.
Warning: 1 tests completed with 1 failed, 0 skipped, and 0 todo.
0 assertions (in 0ms), passed: 0, failed: 0 Use --force to continue.

Aborted due to warnings.

here my test:

QUnit.test("TSO json loader test", function (assert) {

    assert.expect(2);
    var done = assert.async();
    var done1 = assert.async();

    var tsoGrid = new TsoGrid(".testCont");
    tsoGrid.loadJsonDataArray("/test/ajax.json", {}, function (jsonResult) {
        assert.ok(jsonResult[0][0] === "Lorem ipsum", 'ajaxImportFromJson');
        done();
        var jsonRes = jsonResult.importFromJsonArray();

        var resArray = [["Lorem ipsum"],
            ["Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante."],
            [
                [["Lorem ipsum dolor sit amet, consectetuer adipiscing elit."],
                    ["Aliquam tincidunt mauris eu risus.", "Aliquam tincidunt mauris eu risus.1", "Aliquam tincidunt mauris eu risus.2"],
                    ["Vestibulum auctor dapibus neque."]]
            ]];
        assert.ok(jsonRes.isEquals(resArray), 'AjaxImportFromJsonArray');
        done1();
    });

});

I try to google about this, but after QUnit moves to async() and stops using start() and stop() all answers are obsolete :(

I find problem, here solution:

I set URL for query from root of my site, when i remove it, set to file itself, now it pass tests from grunt!

So we need change string to this:

tsoGrid.loadJsonDataArray("ajax.json", {}, function (jsonResult) {

removing /test/ from path.


Solution

  • I find problem, here solution:

    I set URL for query from root of my site, but it must be local path!

    So you need change path to local file, i my case it stored in the same folder with test file.

    Here's the working test:

    QUnit.test("TSO json loader test", function (assert) {
    
        assert.expect(2);
        var done = assert.async();
    
        var tsoGrid = new TsoGrid(".testCont");
        tsoGrid.loadJsonDataArray("ajax.json", {}, function (jsonResult) {
            assert.ok(jsonResult[0][0] === "Lorem ipsum", 'ajaxImportFromJson');
            var jsonRes = jsonResult.importFromJsonArray();
    
            var resArray = [["Lorem ipsum"],
                ["Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante."],
                [
                    [["Lorem ipsum dolor sit amet, consectetuer adipiscing elit."],
                        ["Aliquam tincidunt mauris eu risus.", "Aliquam tincidunt mauris eu risus.1", "Aliquam tincidunt mauris eu risus.2"],
                        ["Vestibulum auctor dapibus neque."]]
                ]];
            assert.ok(jsonRes.isEquals(resArray), 'AjaxImportFromJsonArray');
            done();
        });
    
    });