简体   繁体   English

在Internet Explorer 9中运行任何测试之前,将调用QUnit安装程序回调

[英]QUnit setup callbacks all being called before running any tests in Internet Explorer 9

I've come across a very strange problem when executing my QUnit tests in IE9. 在IE9中执行QUnit测试时遇到了一个非常奇怪的问题。 The test structure is something like this: 测试结构是这样的:

function setupTest() {
    // Adding some extra items to the DOM
}

function teardownTest() {
    // Removing dirtied items from the DOM
}

module("MODULE A", {setup: setupTest, teardown: teardownTest});

test("TEST 1", function () {
    // Some simple assertions
});

module("MODULE B", {setup: setupTest, teardown: teardownTest});

test("TEST 2", function () {
    // Some simple assertions
});

About 50% of the time these tests fail. 这些测试失败的时间约为50%。 I have made sure the tests are entirely independent and the setup/teardown fully completes before/after a test runs. 我确保测试完全独立,并且在测试运行之前/之后完全完成了设置/拆卸。 The problem comes down to the setup callbacks being called out of sequence half of the time. 问题归结于一半时间没有按顺序调用安装程序回调。 With logging I can see this happening: 通过日志记录,我可以看到这种情况:

Successful run: 成功运行:

LOG: completing setup
LOG: MODULE A: TEST 1
LOG: completing teardown
LOG: completing setup
LOG: MODULE B: TEST 2
LOG: completing teardown 

Unsuccessful Run: 运行失败:

LOG: completing setup
LOG: completing setup    <-- ?
LOG: MODULE B: TEST 2
LOG: completing teardown
LOG: MODULE A: TEST 1
LOG: completing teardown 

As you can see on the unsuccessful run the setup callback is correctly executed twice, but before either of the tests are executed. 如您所见,在未成功运行的情况下,设置回调正确执行了两次,但在执行任何一项测试之前。 Since teardown removes the required DOM elements the second executed test is bound to fail as its setup was called too early and the first executing test (correctly) ripped it out with its teardown. 由于拆解删除了必需的DOM元素,因此第二个执行的测试注定会失败,因为它的设置被调用为时过早,并且第一个执行的测试(正确)通过拆解将其删除。

This problem always occurs when the second test is executed before the first, but I can see no reason that this should cause a problem. 当在第一个测试之前执行第二个测试时,总是会出现此问题,但是我看不出有什么理由会引起问题。 The issue never appears in Chrome. 该问题从未出现在Chrome中。

I had the same issue in Chrome (exactly 50% of the time my tests fail) and solved it in next way: 我在Chrome中遇到了相同的问题(大约50%的测试失败了),并通过以下方式解决了该问题:

Code with bug: 带有错误的代码:

require(["jquery", "jquerymobile", "QUnit", "underscore", 
    "units/general.unit", "units/mapping.unit"],
function( $, jqm, __QUnit, _ ) {
    var test_modules = Array.prototype.splice.call(arguments, 4);
    _.each( test_modules, function(test){
        test.start();
    });
    QUnit.load();
    QUnit.start();
});

Corrected version 更正版本

require(["jquery", "jquerymobile", "QUnit", "underscore", 
    "units/general.unit", "units/mapping.unit"],
function( $, jqm, __QUnit, _ ) {
    QUnit.load();
    QUnit.start();
    var test_modules = Array.prototype.splice.call(arguments, 4);
    _.each( test_modules, function(test){
        test.start();
    });
});

QUnit.load() and QUnit.start() moved before the tests invocation and it do the trick. QUnit.load()QUnit.start()在测试调用之前就已经移动了,并且可以解决问题。

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

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