Installation
Chai is available for both user with both node.js and the browser using any test framework you like. Chai tests itself using mocha.
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>
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);
Should tests do not run in the browser.
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');
Getting Help
If you have questions or issues, please use this projects Github Issues.
For Contributors
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