Sinon and Spies

— Spies are used to watching a single function within an object
Reference –



Setup and Teardown

Example without setup/teardown

Class Name – beforeAndAfterEach.js


In above example it is failing because num value got increased in 1st test case. Below is how we can solve that by adding beforeEach which run for every test case.

Mocha and Chai


  • Mocha is a JavaScript Test Framework.
  • Runs on Node.js and Browser
  • Installation: (Run the below commands in terminal or cmd)

—- global helps to install the Mocha on the computer at a global level which helps to run the mocha test through the command line.

—- save-dev helps to add the mocha as a dependency in the package.jsonfile for that particular project.



  • Chai is BDD/TDD assertion library.
  • Can be paired with any javascript testing framework.
  • Assertion with Chai provides natural language assertions, expressive and readable style.
  • Installation: (Run the below commands in terminal or cmd)


Example: test.js


Command to run