1

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"}}}}, {}
zlZimon
  • 2,334
  • 4
  • 21
  • 51

1 Answers1

1

So the reason for this is that apparently middy calls the base handler under the hood which receives two arguments await baseHandler(request.event, request.context);

A hint should've been the {} at the end of the Received output of the testrunner.

So the updated matcher should look like this:

    expect(mockHandler)
    .toHaveBeenCalledWith(expect.objectContaining({ 
     importantData:'important'} ), expect.anything());

This raises an assertion so you have to add expect.assertions(1) at the start of the test.

I not completely understand why an assertion is happening, but I assume that .toHaveBeenCalledWith raises one since we do not have an exact match?

zlZimon
  • 2,334
  • 4
  • 21
  • 51