I am using Mocha/Chai/Chai-jquery to test code written in Jquery. And although I've included the jquery and chai-jquery in HTML, when I run this test:
it("is focused", function () { expect($("#movie")).should.have.focus();});
using chai-jquery's assertion:
(expect = require('chai-jquery').expect)
I get this error:
TypeError: expect(...).should.have.focus is not a function
So how can I set this up to use Chai-jquery's assertion as shown here? Here's my code:
html:
<body>
<div id="mocha"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/jquery/src/jquery.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script src="node_modules/chai-jquery/chai-jquery.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
var expect = chai.expect;
</script>
<script src="script.js"></script>
<script src="test.js"></script>
<script>
{ mocha.run(); }
</script>
<input id="movie"/>
</body>
script.js:
$(document).ready(function() {
$("#movie").focus();
//etc
});
test.js:
var jsdom = require('jsdom');
var document = jsdom.jsdom("");
var window = document.defaultView;
global.window = window
global.$ = require('jquery');
var assert = require('chai').assert,
expect = require('chai').expect,
should = require('chai').should();
describe("Search input test", function () {
it("is in the DOM", function () {
expect(searchInput).to.not.equal(null);
}); // pass
it("is a child of the body", function () {
expect(searchInput.parentElement).to.equal(window.body);
}); //pass
it("is focused", function () {
expect($("#movie")).should.have.focus();
}); // TypeError: expect(...).should.have.focus is not a function
});