Sunday, July 3, 2022

Mock node-fetch with JEST

Recently I tried my hands on Jest - the popular JavaScript Testing Library. In my application we were using node-fetch to make API calls. I used Jest to mock and test this API calls. Here in this blog I will explain on how to mock node-fetch with Jest. 

Following is my function to call the GET API. 


const fetch = require('node-fetch');

module.exports = async () => {

    return await fetch('http://YOUR_GET_URL').then(res => res.json());


Now to test this create a test file with name test.fetch_get.js

Step 1: First import the function.

const fetch_get = require('../fetch_get');

Step 2 : Mock the node-fetch with Jest

const fetch = require('node-fetch');
jest.mock('node-fetch', ()=>jest.fn())

Step 3: Create Mock response

const mockedRes = {
  "success": true,
  "data": [
        id: 1,
        text: 'Test Data 1'
        id: 1,
        text: 'Test Data 2'

Step 4: Add Test 

describe("Test get fetch", () => {
    let data;
    it('It should return the data', () => {
      const response = Promise.resolve({
            ok: true,
            status: 200,
            json: () => {
                return mockedRes;
        fetch.mockImplementation(()=> response)
        data = await fetch_get();

In above code we have created a rest and mocked the response to return our response when we call .json() method for response. Then we are simply comparing the response. This is a very simple test. Here in stead of comparing data, you may have other tests like checking length of data or checking specific data etc.

Step 5: Run Test

Run the test with npm test or npm run test and you will see the following result.

Test get fetch
    ✓ It should return the data (17 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.313 s, estimated 1 s

Hope this helps you.

Saturday, January 29, 2022

How to Execute Shell Script from Dockerfile

Recently I faced a problem with Docker build. Before we build the image there was a requirement to cleanup something from MongoDB. For that we tried to create shell script and execute it from Dockerfile before we start the service. Here in this blog I will explain how you can execute the shell script from Dockerfile. 

For that first create the shell script in the same context of Dockerfile. That means the file should be in the same folder or subfolder where Dockerfile is there. 

We can not access file outside the Docker build context so file has to be in the same folder.


You can put your content in this file for the tasks you want to do with this file. 

Now first we will copy the file in docker build context. So that it can be executed. This step is mandatory or else it will not find file. Add following lines of code in your Dockerfile.

FROM alpine3.15


Next step we have to change access rights of the script so it can be executed.

RUN chmod +x ./

Now since we have file in the Docker build context and it has proper access rights we can execute the file. 

RUN sh ./

That's it and now when to service starts it will execute the file and do all the actions which you have added in shell script.

Hope this helps you. 

Monday, January 3, 2022

Kubernetes ImagePullBackOff error

Recently I faced an issue while working Kubernetes cluster. I was trying to build docker image from local repository to test some changes in production before we merge the code. After building the docker image and recreating the container it was showing ImagePullBackOff error when I check the status with 

kubectl get pods

In this blog I am going to mention the possible cause for this error and will explain the resolution which worked for me.

In a Kubernetes, there’s an agent on for node which is called kubelet. This is responsible for running containers in the nodes. For some reason if image can't be pulled the kublet will throw ImagePullBackOff error. 

There are some possible reasons for this when kubectl is not able to pull image from mentioned registry. 

Either image or tag is not available. 

The name of the image or tag is wrong in deployment yaml file

kubectl does not have access to image registry. 

In my case there was no such issues mentioned above. The image with tag was there. I have specified the correct name in deployment yaml file and because it was a local registry, there was no need of authentication. 

So here is how I solved this issue. I set image pull policy to following value in my deployment yaml file.

imagePullPolicy: IfNotPresent

The reason it worked is, the kubectl was trying to find image in registry but it was not present because it was local registry so then it tried to find image in local repository and caches it. 

This solution worked for me because I was using local registry. If you are facing such issue try setting above value to imagePullPolicy and it may work. Hope this helps you.