首页 > 解决方案 > Q-Unit 全局故障

问题描述

我刚刚尝试为我的 javascript 函数设置 Q-Unit 测试。但是,我似乎收到了Global Failure的错误

在我的 JS 文件 :script.js中,我必须在所有 JS 代码之上都有测试代码,否则单元测试不会运行,我只会收到全局失败消息。但是,如果我将单元测试粘贴到所有 JS 函数之上,测试就会运行,但我仍然会收到全局失败错误。 错误2

有人可以指导正确的方向。

测试脚本.html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Q-Unit Tests</title>
    <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.10.0.css">


</head>
<body>

    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
    <script src="https://code.jquery.com/qunit/qunit-2.10.0.js" integrity="sha256-X1fQXHSYGxa4V2bqkEAQW0DQGSxJrKveasahr959o28=" crossorigin="anonymous"></script>

    <script src="script.js"></script>

</body>
</html>

脚本.js:

 function square(x) {
  return x * x;
}

QUnit.test( "square", function( assert ) {
  var result = square(2);
  assert.equal( result, "4", "square(2) should be 4." );
});

//Normal JS functions are below here

标签: javascriptunit-testingqunit

解决方案


正如我认为的那样,我通过包含已解决全局故障错误的 jQuery 的 CDN 链接来发布此问题的答案,因为它不再出现。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

推荐阅读