node.jsamazon-web-servicesjestjsamazon-qldb

How to mock AWS QLDB in Node js


I want to do Jest test cases on QLDB to be covered my code lines as much I can.

is there any way to do QLDB mock with our code covered ( Jest )


Solution

  • Worked!

    For now, this is the only way to do the Jest Test on QLDB ( Mocking DB ).

    Sample Code - Ref this article -

    var chai = require('chai');
    var qldb = require('amazon-qldb-driver-nodejs');
    var sinon = require("sinon");
    
    class VehicleRegistration {
        constructor() {
            var serviceConfigurationOptions = {
                region: "us-east-1",
                httpOptions: {
                    maxSockets: 10
                }
            };
            this.qldbDriver = new qldb.QldbDriver("vehicle-registration", serviceConfigurationOptions)
        }
    
        async getPerson(firstName) {
            return await this.qldbDriver.executeLambda("SELECT * FROM People WHERE FirstName = ?", firstName);
        }
    }
    
    describe("VehicleRegistration", () => {
        const sandbox = sinon.createSandbox();
        let vehicleRegistration = new VehicleRegistration();
    
        it("should return person when calling getPerson()", async () => { 
            const testPersonResult = {"FirstName": "John", "LastName": "Doe"};
            const executeStub = sandbox.stub(vehicleRegistration.qldbDriver, "executeLambda");
            executeStub.returns(Promise.resolve(testPersonResult));
    
            let result = await vehicleRegistration.getPerson("John");
            chai.assert.equal(result, testPersonResult);
        });
    });