如何使用Chai-jQuery的断言来测试jQuery?

时间:2017-03-16 03:07:33

标签: jquery unit-testing mocha chai assertion

我正在使用Mocha / Chai / Chai-jquery来测试用Jquery编写的代码。虽然我在HTML中包含了jquery和chai-jquery,但是当我运行这个测试时:

  it("is focused", function () { expect($("#movie")).should.have.focus();});

使用chai-jquery的断言:

 (expect = require('chai-jquery').expect) 

我收到此错误:

 TypeError: expect(...).should.have.focus is not a function

那么如何设置它来使用Chai-jquery的断言here?这是我的代码:

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

});

0 个答案:

没有答案