Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Latest Update to Github

Loading...
Loading...

Installation

Chai is available for both node.js and the browser using any test framework you like.

Node.js

Package is available through npm:

npm install chai

Recommend adding it to package.json devDependancies.

Browser

Include the chai browser build in your testing suite.

<script src="chai.js" type="text/javascript"></script>

Currently supports all modern browsers: IE 9+, Chrome 7+, FireFox 4+, Safari 5+.

Want to know if your browser is compatible? Run the online test suite.

The latest tagged version will also be available for hot-linking at http://chaijs.com/chai.js.

Assertion Styles

Expect

The expect style is server/browser BDD style assert language.

  var expect = require('chai').expect
    , foo = 'bar'
    , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

  expect(foo).to.be.a('string');
  expect(foo).to.equal('bar');
  expect(foo).to.have.length(3);
  expect(beverages).to.have.property('tea').with.length(3);

Should

The should style was inspired by should.js and is completely API compatible.

  var should = require('chai').should() //actually call the the function
    , foo = 'bar'
    , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

  foo.should.be.a('string');
  foo.should.equal('bar');
  foo.should.have.length(3);
  beverages.should.have.property('tea').with.length(3);

Notice that the expect require is just a reference to the expect function, whereas with the should require, the function is being executed.

Assert

The assert style is like the node.js included assert utility with few extras.

  var assert = require('chai').assert
    , foo = 'bar'
    , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

  assert.typeOf(foo, 'string', 'foo is a string');
  assert.equal(foo, 'bar', 'foo equal `bar`');
  assert.length(foo, 3, 'foo`s value has a length of 3');
  assert.length(beverages.tea, 3, 'beverages has 3 types of tea');

Plugins

Available Plugins

The Chai community is growing! Plugins allow developers to expand Chai's available assertions. Here is what the community has come up with so far:

  • chai-spies is a basic spy implementation for chai. It's also a good resource for building chai plugins that work in both node.js and the browser.
  • chai-jquery by @jfirebaugh provides deep jQuery integration with chai should and expect.
  • jack by @vesln is a mock/stub library that can be used as a stand-alone or with chai.
  • sinon-chai by @domenic extends chai with assertions for the Sinon.js mocking framework.

Getting Help

If you have questions or issues, please use this projects Github Issues. You can also keep up to date on the Google Group or ping @jakeluer directly on Twitter. Chai developers can also be found on Freenode IRC in #letstest.js.

Contributing

Developing

Please avoid making changes to the browser versions of chai if you are developing in the browser. All changes to the library are to be made to lib/* and then packaged for the browser using the make command.

Testing

Tests are written in exports style on mocha test framework. There is a test file for each of the interfaces. The tests for expect and assert must pass in node.js and in the browser, whereas the should tests only need to pass on node.js.

Browsers tests are currently known to pass in Chrome 16 and Firefox 8. Please let me know if you can test in other browsers or other version.

Server Side Testing

It's quite simple...

  make test

Browser Side Testing

It's also quite simple. Open up test/browser/index.html in your nearest browser.

Building

If you have made changes to any of the components, you must rebuild the browser package.

  $ make

Contributors

 commits: 252
 files  : 71
 authors: 
   192  Jake Luer               76.2%
    53  Veselin Todorov         21.0%
     3  Jeff Barczewski         1.2%
     1  Vinay Pulim             0.4%
     1  Jo Liss                 0.4%
     1  Domenic Denicola        0.4%
     1  John Firebaugh          0.4%