Search code examples
typescriptamazon-web-servicesunit-testingaws-lambdahandler

How to test AWS Lambda handler locally using typescript


I have followed typescriptLambda inscructions to create basic typescript lambda. Now , I would like to execute the code locally like in testNodeJsLambda , but I run into difficults to define the event and the context.

import {APIGatewayProxyCallback, Context} from 'aws-lambda';
import {APIGatewayEvent} from "aws-lambda";

     export const handler = async (
         event: APIGatewayEvent, context: Context, callback: APIGatewayProxyCallback ) => {
         console.log (`Test`)
         callback(null, {
             statusCode: 200,
          body: JSON.stringify(recordingStatus)});   
    }

How do I define the event and the context ? empty object {} is not acceptable.

The code for event is :

import {APIGatewayEvent, Context} from 'aws-lambda';
const event : APIGatewayEvent = {
        body: null,
        headers: {},
        multiValueHeaders: {},
        httpMethod: "POST",
        isBase64Encoded: false,
        path: "/path/to/resource",
        pathParameters : null,
        queryStringParameters : null,
        multiValueQueryStringParameters : null,
        stageVariables : null,
        requestContext: {
            accountId: "123456789012",
            apiId: "1234567890",
            authorizer: {},
            protocol: "HTTP/1.1",
            httpMethod: "POST",
            identity: {
                accessKey: null,
                accountId: null,
                apiKey : null,
                apiKeyId : null,
                caller: null,
                clientCert: null,
                cognitoAuthenticationProvider: null,
                cognitoAuthenticationType: null,
                cognitoIdentityId: null,
                cognitoIdentityPoolId: null,
                principalOrgId : null,
                sourceIp: "127.0.0.1",
                userArn: null,
                userAgent: "Custom User Agent String",
                user: null
            },
            path: "/prod/path/to/resource",
            stage: "prod",
            requestId: "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
            requestTime: "09/Apr/2015:12:34:56 +0000",
            requestTimeEpoch: 1428582896000,
            resourceId: "123456",
            resourcePath: "/{proxy+}",
        },
        resource: "/{proxy+}"
    };

Solution

  • You can use this one, if you're using the AWS-SDK v2.

    import { Context } from 'aws-lambda';
    
    const mockedContext: Context = {
        callbackWaitsForEmptyEventLoop: false,
        functionName: 'mocked',
        functionVersion: 'mocked',
        invokedFunctionArn: 'mocked',
        memoryLimitInMB: 'mocked',
        awsRequestId: 'mocked',
        logGroupName: 'mocked',
        logStreamName: 'mocked',
        getRemainingTimeInMillis(): number {
            return 999;
        },
        done(error?: Error, result?: any): void {
            return;
        },
        fail(error: Error | string): void {
            return;
        },
        succeed(messageOrObject: any): void {
            return;
        }
    };