【问题标题】:I can run only 7 QUnit tests at the same time我只能同时运行 7 个 QUnit 测试
【发布时间】:2026-02-05 10:20:05
【问题描述】:

我在运行 QUnit 测试时遇到问题。我的页面上有一些异步测试。

var sendInvalidData = function (httpMethod, data) {
    $.ajax({
        url: '@AppSettings.ApiUrl' + 'api/parties',
        type: httpMethod,
        headers: { 'Authorization': 'Bearer ' + accessToken },
        dataType: "json",
        data: data,
        timeout: 5000
    }).done(function (response) {
        ok(!response.Success, _.first(response.Errors));
    }).fail(function (x, text, thrown) {
        ok(false, "Ajax failed: " + text);
    }).always(function () {
        start();
    });
};

asyncTest("GET api/parties/Available", function () {
    $.ajax({
        url: '@AppSettings.ApiUrl' + 'api/parties/Available',
        type: "GET",
        headers: { 'Authorization': 'Bearer ' + accessToken },
        timeout: 5000
    }).done(function (data) {
        ok(data.Success, "Is response success");
    }).fail(function (x, text, thrown) {
        ok(false, "Ajax failed: " + text);
    }).always(function () {
        start();
    });
});

asyncTest("POST api/parties", function () {
    // The Name field is required.
    sendInvalidData("POST" ,{
        IsPrivate: false,
        Color: 1
    });

    sendInvalidData("POST", {
        Name: "new party",
        Color: 1
    });

    sendInvalidData("POST", {
        Name: "new party",
        IsPrivate: true,
        Color: 1
    });

    sendInvalidData("POST", {
        Name: "new party",
        IsPrivate: false
    });

    sendInvalidData("POST", {
        Name: "new party",
        IsPrivate: false,
        Password: "123",
        Color: 1
    });

    $.ajax({
        url: '@AppSettings.ApiUrl' + 'api/parties',
        type: "POST",
        headers: { 'Authorization': 'Bearer ' + accessToken },
        dataType: "json",
        data: {
            Name: "new party",
            IsPrivate: false,
            Color: 1
        },
        timeout: 5000
    }).done(function (response) {
        ok(response.Success, "Party has created successfully.");
    }).fail(function (x, text, thrown) {
        ok(false, "Ajax failed: " + text);
    }).always(function () {
        start();
    });
});

asyncTest("POST api/parties", function () {
    // another N tests
});

// more async tests

所有测试都运行清晰并分别通过。但如果它们同时运行,则只运行前 7 个测试。

我想我错过了什么……你能帮帮我吗?

【问题讨论】:

    标签: javascript unit-testing qunit


    【解决方案1】:

    我通过将每个 ajax 请求包装在 asyncTest() 方法中解决了这个问题。

    【讨论】: