how to write codes for mocking api request using jestjs on axios

Currently, I’m working on a node.js CLI app. This code is written for taking an … Read more how to write codes for mocking api request using jestjs on axios

Currently, I’m working on a node.js CLI app. This code is written for taking an IP address from a user and fetch the information that belongs to that particular IP address from an API. I have used Axios to fetch data from the API server. The source code is given below. So I want to mock an API using Jestjs Please show me the right direction on how to do the test using JEST JS.

#!/usr/bin/env node

let inquirer = require('inquirer');
var Table = require('cli-table');
const chalkAnimation = require('chalk-animation');
const isOnline = require('is-online');
var figlet = require('figlet');
const axios = require('axios');

function getLocation(){
    inquirer.prompt([{   
        type : 'input', 
        message: "Enter your IP address : ",
        name : 'ipaddress'
    }]).then((answer) => {
        const ipaddres = answer.ipaddress;
        if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddres)){
            let url = 'http://ip-api.com/json/' + ipaddres ;
            var table = new Table();

            axios.get(url)
                 .then((response) => {
                    let city = response.data.city;
                    let country = response.data.country;
                    let countryCode = response.data.countryCode;
                    let zip = response.data.zip;
                    let latitude = response.data.lat;
                    let longitude = response.data.lon;
                    let ISP = response.data.isp;
            

                    table.push(
                            { 'City': city }
                          , { 'Country': country }
                          , { 'Country Code': countryCode }
                          , { 'Zip': zip }
                          , { 'Latitude': latitude }
                          , { 'Longitude': longitude }
                          , { 'ISP': ISP }
                        );
                        
                        console.log(table.toString());
                        let rainanim = chalkAnimation.radar("Thank you for using this APP");
                        setTimeout(() => {
                        rainanim.stop();
                        }, 5000);
                
            }).catch((error) => {
                console.log(error);
            });  
        }else{
            let pulseanim = chalkAnimation.pulse("You have entered an Invalid IP address ");
            setTimeout(() => {
                pulseanim.stop();
            }, 5000);
            
            
        }
        
        
    });
}

async function onCheck(){
    if(await isOnline()){
        getLocation();
    }
    else{
        figlet('Offline !', function(err, data) {
            if (err) {
                console.log('Something went wrong...');
                console.log(err);
                return;
            }
            console.log(data)
            let pulseanim = chalkAnimation.pulse("You are offline! Please check your internet connectivity");
            setTimeout(() => {
                pulseanim.stop();
            }, 5000);
        });
    }
};
onCheck();

module.exports = getLocation;

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *