1

I'm running jquery 3.3.1, qunit 2.11.2, MacOS Chrome 87.0.4280.67

I have some code which works correctly when I run it in production, but fails under qunit. Investigating, it looks like $ has different values in those two environments. In this function:

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');
};

in my production environment, the console shows:

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

but under qunit I get:

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

My test driver is:

<!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>

And my production code loads jquery with:

<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
Roy Smith
  • 2,039
  • 3
  • 20
  • 27

1 Answers1

1

As usual, a good night's sleep brought some clarity. I still don't understand everything, but the big question of why I get:

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

vs

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

is that the later one is when I included the minimized version of jquery. And, yes, the script tag I showed above for my production code is worng; that's not actually the one that's used in production.

Roy Smith
  • 2,039
  • 3
  • 20
  • 27
  • And, it turns out tracking down that I had the minimized version in one environment and not the other was a total rabbit hole. My test case wasn't working for reasons that seem to be related to [this answer](https://stackoverflow.com/a/27030730/506824), i.e. jquery.find() doesn't find top-level nodes, which I didn't know when I wrote my test case. – Roy Smith Dec 06 '20 at 13:42