Ida*_*mit 1 unit-testing handler amazon-web-services typescript aws-lambda
我已按照typescriptLambda说明创建基本的 typescript lambda。现在,我想像testNodeJsLambda一样在本地执行代码,但我在定义事件和上下文时遇到了困难。
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)});
}
Run Code Online (Sandbox Code Playgroud)
如何定义事件和上下文?空物体{}是不可接受的。
事件的代码是:
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+}"
};
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用的是 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;
}
};Run Code Online (Sandbox Code Playgroud)