Writing tests using Chai
Chai is an assertion library. Instead of needing to write several variations of assert
, Chai provides several different methods that we can use to create tests that are easy to read. Chai also uses method chaining to create a more English-like version of the test, so they are both easy to read and write.
Note
For more information on Chai, visit http://www.chaijs.com.
Chai is useful because it allows us to write expressive tests without a lot of work. It also caters to you, the developer, by letting you choose one of several provided interfaces. There are three:
assert
: Tests use an assert-styled interface, akin to what we used in the prior section. There are a lot of utility methods that you can use, such asassert.typeOf
,assert.equal
, and more. One can also just useassert(expression, message)
as we did in the previous section.expect
: Tests useexpect
and method chaining to form an English-like statement. For example,expect(x).to.equal(5)
orexpect(a).to.have.property...