concept Mongodb in category docker

appears as: Mongodb
Bootstrapping Microservices with Docker, Kubernetes and Terraform

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform.

Isolation of code is achieved by mocking it’s dependencies. What this means in terms of our metadata microservice is that we’ll be substituting the real Express and MongoDB libraries for fake instances that we can control and bend to our will.

This example is actually quite advanced, but I wanted to get straight to the point and show you some unit testing that is very relevant to microservices. If you struggle to understand this code please don’t be too concerned, just try to read it, get the gist of it and understand which parts of it are for mocking and which parts are for testing. 

Listing 8.7 - Code to test the metadata microservice with Jest (chapter-8/example-2/src/index.test.js)
describe("metadata microservice", () => { #A
 
    const mockListenFn = jest.fn((port, callback) => callback()); #B
    const mockGetFn = jest.fn(); #C
 
    jest.doMock("express", () => { #D
        return () => { #E
            return { #F
                listen: mockListenFn,
                get: mockGetFn,
            };
        };
    });
 
    const mockVideosCollection = {}; #G
 
    const mockDb = { #H
        collection: () => {
            return mockVideosCollection;
        }
    };
 
    const mockMongoClient = { #I
        db: () => {
            return mockDb;
        }
    };
   
    jest.doMock("mongodb", () => { #J
        return {
            MongoClient: { #K
                connect: async () => { #L
                    return mockMongoClient;
                }
            }
        };
    });
 
    const { startMicroservice } = require("./index"); #P
   
    test("microservice starts web server on startup", async () => { #Q
       
        await startMicroservice(); #R
 
        expect(mockListenFn.mock.calls.length).toEqual(1); #S
        expect(mockListenFn.mock.calls[0][0]).toEqual(3000); #T
    });
 
    test("/videos route is handled", async () => { #U
       
        await startMicroservice(); #R
 
        expect(mockGetFn).toHaveBeenCalled(); #W
 
        const videosRoute = mockGetFn.mock.calls[0][0];
        expect(videosRoute).toEqual("/videos"); #X
    });
 
    test("/videos route retrieves data via videos collection", async () => { #Y
 
        await startMicroservice();
 
        const mockRequest = {}; #Z
        const mockJsonFn = jest.fn();
        const mockResponse = {
            json: mockJsonFn
        }; #Z
 
        const mockRecord1 = {}; #AA
        const mockRecord2 = {};
 
        mockVideosCollection.find = () => {
            return {
                toArray: async () => { #BB
                    return [ mockRecord1, mockRecord2 ];
                }
            };
        }; #AA`
 
        const videosRouteHandler = mockGetFn.mock.calls[0][1]; #CC
        await videosRouteHandler(mockRequest, mockResponse); #DD
 
        expect(mockJsonFn.mock.calls.length).toEqual(1); #EE
        expect(mockJsonFn.mock.calls[0][0]).toEqual({
            videos: [ mockRecord1, mockRecord2 ], #FF
        });
    });
 
    #GG
 
});
 

You may have noticed in listing 8.7 that I’m using async and await keywords in this code. I use them a lot in my day to day coding but I haven’t used them yet in this book. The reason I’ve introduced them here is because they fit nicely into Jest tests and they make asynchronous code significantly easier to read.