title: How to fix "$ is not defined" error when unit testing Jquery with Typescript using Mocha?
body: <p>I am writing <strong>Mocha</strong> unit tests for <strong>Typescript</strong> code containing <strong>Jquery</strong>. I'm using <strong>jsdom</strong> for getting the document object. When I compile my TS code to JS and run the tests, it throws an error <strong>[ReferenceError: $ is not defined]</strong>.</p>
  
  <p>My Typescript code is here</p>
  
  <pre><code>export function hello(element) : void {
      $(element).toggleClass('abc');
  };
  </code></pre>
  
  <p>My unit test code is as follows:</p>
  
  <pre><code>import {hello} from '../src/dummy';
  
  var expect = require('chai').expect;
  var jsdom = require('jsdom');
  var document = jsdom.jsdom();
  var window = document.defaultView;
  
  var $ = require('jquery')(window);
  
  describe('TEST NAME', () =&gt; {
  
      it('should run', (done) =&gt; {
          hello($('div'));
          done();
      });
  });
  </code></pre>
  
  <p>When I run Mocha test it shows</p>
  
  <pre><code>    &lt;failure message="$ is not defined"&gt;&lt;![CDATA[ReferenceError: $ is not defined ...
  ]]&gt;&lt;/failure&gt;
  </code></pre>
  
  <p>Also tried using <em>global.$ = require("jquery");</em> but does not work.</p>