2022-10-26

Testing strategy for typescript lambda with database dependency

I have a typescript lambda that calls a database. I have written unit tests for the individual components for this (service that takes the db as a constructor argument for mocking etc, that all works fine). I am looking to write a test that calls the lambda handler itselff, however if I do this, I can no longer pass a db mock, therefore it tries to call the real database. Is there an established pattern for doing this other than spinning up a local database in docker/in memory database etc?

import { APIGatewayProxyCallback } from "aws-lambda";
import { myService } from "./service/myService";
import { myRepository } from "./repository/myRepository";

export const lambdaHandler = async (
    event: any,
    context: any,
    callback: APIGatewayProxyCallback,
): Promise<void> => {

    const service = new myService(new myRepository());
    const res = myService.execute(event); // Contains code for interacting with db

        callback(null, {
          statusCode: 200,
          body: JSON.stringify(res),
        });
};



No comments:

Post a Comment