Skip to content

guram21/Unit_testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript — Unit Testing using Mocha and Chai

About Unit Testing

When you test your codebase, you take a piece of code — typically a function — and verify it behaves correctly in a specific situation. Unit testing is a structured and automated way of doing this. As a result, the more tests you write, the bigger the benefit you receive. You will also have a greater level of confidence in your codebase as you continue to develop it.

Setting up the Tools

Your Machine should have node and npm installed. Install the node.js LTS version from Node website. npm gets installed along with node automatically.

Run below in command line to check the successful installation of node and npm.

npm -v // will return installed npm version

node -v // will return installed node version

Mocha

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

npm install --global mocha

npm install --save-dev mocha

Note: To run Mocha, we need Node.js v4 or newer.

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

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

Mocha Basic Spec

  • assert helps to determine the status of the test, it determines failure of the test.
  • describe is a function which holds the collection of tests. It takes two parameters, first one is the meaningful name to functionality under test and second one is the function which contains one or multiple tests. We can have nested describe as well.
  • it is a function again which is actually a test itself and takes two parameters, first parameter is name to the test and second parameter is function which holds the body of the test.

Steps to Run the test

  • Download or clone the Github Repo, navigate to the repo in command line
  • Run npm install to install all dependencies from package.json
  • Run npm test to run all test sepcs.
  • npm test works because of below test script in package.json file.
"scripts";: {
    "test";: "mocha"
  }

Test Assertion

  • Assertion is an expression which helps system (Mocha in this case) to know code under test failed.
  • Assert’s job is to just throw an error when things are not correct or right.
  • Assert tests the expression and it does nothing when expression passes but throws exception in case of failure to tell the test framework about it.
  • We can just throw an exception to fail the test as well.

Chai

  • 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)

npm install --save-dev chai

Assertion interfaces and styles

  • There are two popular way of assertion in Chai, expect and should
  • The expect interface provides function for assertion.
  • The should interface extends each object with a should property for assertion.
  • should property gets added to the Object.Prototype, so that all object can access it through prototype chain.

Project JavaScript Files

You can navigate the files using the following directory:

Useful Links and Documentation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published