Make your work valuable by help of Automated Unit Testing in Node

automated unit testing in node

Automated Testing

What is Automated Testing?

“The practice of writing code to test our code, and then run those tests in an automated fashion.”

Benefits of Automated Testing

  1. Test your code frequently, in less time
  2. catch bugs before deploying
  3. Deployed with confidence
  4. Refactor your code with confidence (Refactoring means changing the structure of the code without changing its behavior).
  5. Helps you focus more on the quality of the code

Types of automated tests

Types of automated tests

Unit Test

Tests a unit of an application without its external dependencies.

External dependencies such as database, web services, message queue and so on…

Integration Test

Tests the application with its external dependencies.

End-to-End Test

Drives an application through it’s UI.

A popular tool for end-to-end testing is Selenium

Here we are going to see the unit test in details

Unit test pyramid

Here you can see the importance of the unit test.

Frameworks for unit test

  1. Jasmine
  2. Mocha
  3. Jest

How to work with Jest framework?

Step 1:

First, install the jest n pm package

Install jest npm package

Note: install jest on development environment only we are not going to install on production because this is for testing purpose only

Step 2 :

Open package.json file and checkout scripts property

Scripts property define commands that we can execute in the command line

set jest to test the property of scripts

Jest test

Jest is a command line interface that we used to run our tests.

run our test

When you run test jest command is executes and run all the files that match with “testMatch:**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)”.

Step 3:

Create a route directory in the root

Create lib.js file into route directory and write a simple numeric function

function

Step 4:

Create a test directory in the root

Create lib.test.js file into the test directory and write multiple test cases for absolute function

test cases

Here the test is a function provided by a jest which calls the absolute function with different input and matches expected output. if it’s matched it will return pass else return failed with discription.

here are few functions which are provided by jest to match the expected output

number functions

Step 5 :

Go to terminal and run test command

result

Here you can see all tests are passed.

In case of the test failed you will get output like this

failed

Jest provides a different function to match expected output for string, array, objects and other data types you can checkout jest document for that.

Also, you can group test cases by using describe function just like below and you will get the same output. it’s good practice to use describe function because when you write multiple tests code gets ugly.

test cases

One last little secret add — watchAll to test command for automatically run all test after refactoring it will save you from the pain of hitting the same command again and again.

forever

If you having difficulty in writing tests or have any doubt in your mind hit us always pleased to help you out.

oneclick

This entry has 0 replies

Comments are closed.