I am writing Mocha unit tests for Typescript code containing Jquery. I'm using jsdom for getting the document object. When I compile my TS code to JS and run the tests, it throws an error [ReferenceError: $ is not defined].
My Typescript code is here
export function hello(element) : void {
$(element).toggleClass('abc');
};
My unit test code is as follows:
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', () => {
it('should run', (done) => {
hello($('div'));
done();
});
});
When I run Mocha test it shows
<failure message="$ is not defined"><![CDATA[ReferenceError: $ is not defined
at Object.hello (D:\newGadgets\gadgets\base\gadget\DrawPad\out\src\defaultTools\dummy.js:3:5)
at Context.<anonymous> (D:\newGadgets\gadgets\base\gadget\DrawPad\out\test\dummy.test.js:10:17)
at callFnAsync (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runnable.js:349:8)
at Test.Runnable.run (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runnable.js:301:7)
at Runner.runTest (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:422:10)
at D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:528:12
at next (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:342:14)
at D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:352:7
at next (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (D:\newGadgets\gadgets\build\node_modules\mocha\lib\runner.js:320:5)
]]></failure>
Also tried using global.$ = require("jquery"); but does not work.
Aucun commentaire:
Enregistrer un commentaire