I want to test my middy middleware for my lambda handler using jest. In the middleware I assign a propperty to the request.event
which then will be used in the handler.
//hander.ts
const main = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
return {
statusCode: 200,
body: `some`
};
};
export const handler = middy(main)
.use(myCustomMiddware('somedata'));
// myCustomMiddleware.ts
export const myCustomMiddleware = (data: string): middy.MiddlewareObj<APIGatewayProxyEvent, APIGatewayProxyResult> => {
const before: middy.MiddlewareFn<APIGatewayProxyEvent, APIGatewayProxyResult> = async (request): Promise<void> => {
// Do stuff
Object.assign(request.event, { importantData: 'important' } as { importantData: string });
};
};
return {
before
};
Now I want to write a test if the handler receives the correct value:
// in my .test.ts file
describe('test', () => {
const mockHandler = jest.fn(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
return {
statusCode: 200,
body: ''
};
});
it('event should contain assigned value', () => {
const request = {
event: {
requestContext: {
authorizer: {
claims: {
'custom:SomeClaimA': 'claimA',
'custom:SomeClaimB': '1'
}
}
}
}
};
const middleware = middy(mockHandler).use(myCustomMiddleware('somedata'));
await middleware.before(request as never);
expect(mockHandler).toHaveBeenCalledWith(expect.objectContaining({ importantData:'important'} ));
});
But for some reason jest tells me that the importantData
is not part of the received parameter when it actually is. This is the output I get from the jest runner:
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: ObjectContaining {"importantData": "important"}
Received: {"importantData": "important", "requestContext": {"authorizer": {"claims": {"custom:SomeClaimA": "SomeClaimA", "custom:SomeClaimB": "1"}}}}, {}