I am writing AWS Lambda
code on Serverless Framework
.
But I'm struggling to make my code testable. My test framework of choice is jest
.
At first, my project structure as follows.
import { fooService } from '../services';
export const createFooHandler: APIGatewayProxyHandlerV2 = async (event) => {
// some validation process
const { code, message } = async fooService.create(event.body);
return { statusCode: code, body: JSON.stringify({message}) }
}
import * as storage from '../storage';
export const fooService = {
create: async (json: any) => {
const foo = Foo.parse(json)
const result = await storage.createFoo(foo);
// .. some process
return { code, message };
}
}
const db = !process.env.IS_OFFLINE
? new DynamoDB.DocumentClient()
: new DynamoDB.DocumentClient({
region: "localhost",
endpoint: "http://localhost:8000"
});
export async function createFoo(foo: Foo) {...}
In this structure, do I have to use DI or not in order to write testable code?