【问题标题】:Why does the value of $ change in a qunit test?为什么在 qunit 测试中 $ 的值会发生变化?
【发布时间】:2020-12-03 21:59:53
【问题描述】:

我正在运行 jquery 3.3.1、qunit 2.11.2、MacOS Chrome 87.0.4280.67

我有一些代码在生产中运行时可以正常工作,但在 qunit 下失败。经过调查,看起来 $ 在这两个环境中具有不同的值。在这个函数中:

function findSpiTemplate(page) {
    const $html = $($.parseHTML(page));
    // return $html.find("table[typeof='mw:Transclusion'][data-mw*='sock']").attr('data-mw');                                                                        
    console.log($);
    return $html.find("table[typeof='mw:Transclusion']").filter(function() {
        const x = $(this).attr('data-mw').match(/[sS]ock/);
        return x;
    }).attr('data-mw');
};

在我的生产环境中,控制台显示:

tag-check.js:49 ƒ (selector,context){return new jQuery.fn.init(selector,context);}

但在 qunit 下我得到:

ƒ (e,t){return new w.fn.init(e,t)}

我的测试司机是:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test Suite</title>
    <link rel="stylesheet" href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/qunit/2.11.2/qunit.min.css">
  </head>
  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
    <script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/qunit/2.11.2/qunit.min.js"></script>
    <!-- Tests need ajax support, so loading the non-slim version of jQuery. -->
    <script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="tests.js"></script>
  </body>
</html>

我的生产代码加载 jquery:

<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>

【问题讨论】:

    标签: jquery qunit


    【解决方案1】:

    像往常一样,睡个好觉带来了一些清晰。我仍然不明白一切,但我为什么会得到一个大问题:

    ƒ (selector,context){return new jQuery.fn.init(selector,context);
    

    ƒ (e,t){return new w.fn.init(e,t)}
    

    是后一个是当我包含jquery的最小化版本时。而且,是的,我上面为我的生产代码显示的脚本标签是磨损的;这实际上不是在生产中使用的那个。

    【讨论】:

    • 而且,事实证明,我在一个环境中拥有最小化版本,而不是另一个完全是兔子洞。我的测试用例无法正常工作,原因似乎与 this answer 相关,即 jquery.find() 找不到顶级节点,而我在编写测试用例时并不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多