Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
How to test jQuery using Chai-jQuery's assertion?

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

});

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!