API Reference
BDD essentially uses expect and should styles of assertion along with chaining methods and language. A chainable method essentially helps us in combining many methods of previous types into a single keyword that works as a keyword in the assertion. Apart from using these as methods, we can also use them as properties, giving it a property, or not giving any as well and including it in the expectation chain. There are many methods that can be used as chainable getters to improve the readability of your assertions.
Some of them are:
- to
- be
- been
- that
- which
- and
- at
As there are numerous methods we will be learning about some of the most commonly used.
.not
This method works as a negation and negates all assertions that follow the chain.
Example
expect(5).to.equal(5);
expect(5).to.not.equal(4);

You can also try this code with Online Javascript Compiler
Run Code
However, when using this in cases where you assert whether the undesirable outcome is produced or not. Here, due the fact that there may be countless unexpected outputs we can not perfectly assert whether the desirable outcome was released.
.deep
When this method is called, all assertions like .equal, .include, .members, etc use deep equality instead of strict equality. When using deep equality it basically means that the keys in the objects are checked recursively until a primitive datatype is found that can be checked for referential equality.
Example
expect({"hello": {"chai":"world"}}).to.deep.equal({"hello": {"chai":"world"}}); //checks each key and returns true.
expect({"hello": {"chai":"world"}}).to.equal({"hello": {"chai.js":"world"}}); //checks referential equality which returns true.

You can also try this code with Online Javascript Compiler
Run Code
.ordered
In some cases we need to check the order in which members occur as well. This is where .ordered comes into play. When using this method all .members assertions that follow in the chain to require that the members be in the same order.
Example
expect([4,5]).to.have.ordered.members([4,5]); //returns true
expect({4,5]).to.not.have.ordered.members([5,4]); //returns false

You can also try this code with Online Javascript Compiler
Run Code
.include
A great example of chaining methods is using .include in conjunction to .ordered. This combination checks both arrays from the beginning to check whether the subarray is a part of the given array.
Example
expect([3,4,5]).to.include.ordered.members([4.5]);

You can also try this code with Online Javascript Compiler
Run Code
.all
When we use this, all .keys assertions that follow in the chain require that all the given keys are present target object.
Example
expect({a:1,b:5}).to.have.all.keys('a','b');

You can also try this code with Online Javascript Compiler
Run Code
When .keys is used, it takes .all as the default behaviour without it being specified. However, mentioning it is considered as a best practice as it increases the readability of the code.
.any
.any works as the opposite of .all in the sense that it makes sure that all the .keys assertions that follow require atleast one of the given keys to match the keys in the target object.
Example
expect({a: 1, b: 2}).to.not.have.any.keys('a', 'c');

You can also try this code with Online Javascript Compiler
Run Code
FAQs
1. Why should we use BDD?
Ans: BDD allows us to clearly let us know what functionalities we need to make due to it being readable and very few cases of misinterpretation that focuses on user needs.
2. How is shallow equality different from deep equality?
Ans: Shallow equality returns zero when the reference points of the two objects are the same. However, deep equality traverses through all the keys to check for equality.
3. What are chainable methods?
Ans: Chainable methods is a way to invoke multiple methods in an object-oriented programming.
4. What is the basic concept of chaining methods?
Ans: The basic method is that all methods except the last method in a chain must return an object on which the method can be called.
Key Takeaways
In this blog, we discussed how we can assert chainable methods in BDD using API References from Chai.js.
You may want to learn more about Test-Driven Development and Behaviour-Driven Development in Jest here.
Check out this problem - Maximum Product Subarray
Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Learning!