3

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 )

Hanoj B
  • 350
  • 2
  • 12

2 Answers2

1

One way in which this could be achieved would be the utilisation of a local QLDB instance that doesn’t make remote calls. DynamoDB for instance has DynamoDB local for these purposes: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html. QLDB however currently doesn’t support a local instance. An alternative to this would be the utilisation of a third party service that enables the development and testing of cloud services offline. One such service is LocalStack: https://localstack.cloud/. LocalStack currently has support for the QLDB APIs: https://localstack.cloud/features/.

1

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);
    });
});
Hanoj B
  • 350
  • 2
  • 12