In this chapter, I would like to give an overview of the Chai Assertion Library. Chai is an assertion library for JavaScript frameworks.
Let us look at what are assertions and why are they required.
Assert statements are basically used to make sure that the assumptions and actual results of the function are exactly same as it is expected to be.
For example, if the function which is supposed to take a number greater than 0, then an assert
statement validates the function, takes a number which is greater than 0. If not, the assertion fails.
We need a lot of assertions in our test to make sure that the actual result is same as the expected one or that meets the acceptance criteria of the test.
In automation testing, be it a unit test, functional, or nonfunctional, we have to do a lot of assertions. There are different ways to do assertions based on the tool or technology that we use for automation.
Chai is such an assertion library, which provides certain interfaces to implement assertions for any JavaScript-based framework.
Chai's interfaces are broadly classified into two: TDD styles and BDD styles.
BDD, also known as behavior-driven development language, provides an expressive language and readable style, whereas TDD also known as test-driven development, provides more of a classical feel.
Let us look at the interfaces now — they are expect
, should
, and assert
.
The first two, that is expect
and should
, are BDD styles, and assert
is a TDD style assertion library.
Now let us look at them one by one.