Expect
The Chai expect style allows a developer to chain the natural language assertions together.
Consider the code below.
const expect = require('chai').expect
, foo = 'mall'
, companies = { name: [ 'Google', 'Amazon', 'Coding Ninjas' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('mall');
expect(foo).to.have.lengthOf(4);
expect(companies).to.have.property('name').with.lengthOf(3);

You can also try this code with Online Javascript Compiler
Run Code
Should
The Chai should style also allows the developer to implement chainable assertions same as the expect style. However, The difference is that the should style extends each object with a should property to start the chain.
Consider the code below.
const should = require('chai').should() //this actually //calls the function.
, foo = 'mall'
, companies = { name: [ 'Google', 'Amazon', 'Coding Ninjas' ] };
foo.should.be.a('string');
foo.should.equal('mall');
foo.should.have.lengthOf(4);
companies.should.have.property('name').with.lengthOf(3);

You can also try this code with Online Javascript Compiler
Run Code
Assert
The Chai assert style provides the developer with a classic assert-dot notation similar to what is packaged with node.js. The assert style module also provides several additional tests and browser compatibility privileges.
Consider the code below.
var assert = require('chai').assert
, foo = 'mall'
, companies = { name: [ 'BMW', 'Audi', 'Bentley' ] };
assert.typeOf(foo, 'string'); // this will not display // an optional message
assert.typeOf(foo, 'string', 'foo is a string'); // this will display an optional message
assert.equal(foo, 'mall', 'foo equal `mall`');
assert.lengthOf(foo, 4, 'foo`s value has a length of 4');
assert.lengthOf(companies.name, 3, 'companies has 3 names');

You can also try this code with Online Javascript Compiler
Run Code
FAQs
-
What is Expect style in Chai.js?
The Chai expect style allows a developer to chain the natural language assertions together.
-
What is Should style in Chai.js?
The Chai should style also allows the developer to implement chainable assertions same as the expect style. However, The difference is that the should style extends each object with a should property to start the chain.
-
What is Assert style in Chai.js?
The Chai assert style provides the developer with a classic assert-dot notation similar to what is packaged with node.js. The assert style module also provides several additional tests and browser compatibility privileges.
-
What is Chai Assertion Library?
Chai is an assertion library which is essentially a tool that can be used to verify that things are correct. Basically, Assertion Libraries helps a developer test their code and significantly reduce the time taken to write thousands of lines of code (statements).
-
How to install Chai.js?
Installing Chai.js is pretty straightforward as it can be downloaded using node.js by typing the following command.
$ npm install chai
Key Takeaways
In this article, we have extensively discussed the Chai.js Assertion Library and its various features. If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Chai.js and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!”