Importance of Testing and its types ,Using Jest framework

Testing basics using JEST Testing Framework

·

3 min read

Testing is an important factor before deploying any application. # What is testing ? Testing is a way by which we can compare actual output with expected output .

In this blog ,i want to you to understand key points which is important for a new programmer to understand without writing jargon technical terms . This article will be pin point some important factors of testing ,so that within 10 minutes you will learn what exactly is testing .

# Conventional way of testing-

  1. Start writing code
  2. test the code
  3. If test case passed deploy the application

## Challenges of the above approach : ● Testing is added very late in the cycle of development ● Last minute issues, might lead to the delay in the release because of re-development ● Compromised testing because of the rush of the release

What are the best way to solve this issue

TDD- Test Driven Development

write the testing code even before you start developing the application and then write the code to pass all the test cases and carry out refactoring as required. Refracting refers to writing the clean code ,removing the duplicate code, and improving the performance.

This is also known as test code refractor or red green refractor .

BDD-Behavioral Driven Development*-

its an extension of TDD .It is mostly done by QA engineers by keeping customer or user prospective in mind.

## #Note In TDD, you test the written code by writing test cases for each method. ● In BDD, you test the BEHAVIOR of the written code by writing test cases for components or classes.

In TDD, we will write test cases to check whether all the methods are working as desired. ● In BDD, we will write test cases to check whether the Authentication Service is working as desired. We should understand what is Unit testing and Integration Testing as well. Testing types -- Unit testing means testing single unit at a time its called Unit testing . Integration testing ,we test more than one unit at a same time called integration testing .

Which testing framework -JEST -It is a JavaScript framework ,maintained by Facebook ,easy to install .

*How to install jest - npm install jest --save-dev

## Testing script -

{ "name": "testing", "version": "1.0.0", "description": "", "main": "test.js", "scripts": {

  • "test": "jest"* }, "author": "", "license": "ISC" }

Now i will discuss testing cases - Case 1: Testing Object

How to define a test // name of the test // callback function

image.png

image.png

Go to terminal and write --npm test image.png

These two have different meanings ---

  • expect(data).toEqual({two:2,one:1});// Its checks data is same ,so it will pass . expect(data).toBe({two:2,one:1});//It will checks objects are same not data, it will fail. *

Testing by Importing a file Here, we will understand how to test a method by importing the JavaScript file. Step 1: Create new file named file.js and add a simple function in it as shown below: Step 2: Create test file named file.test.js and write below code to test it.

image.png

image.png

image.png

image.png

Now you can see different types of testing ---

image.png

image.png

image.png

image.png