I have this handler using a middleware with Middy.
import middy from '@middy/core';
import httpErrorHandler from '@middy/http-error-handler';
import jsonBodyParser from '@middy/http-json-body-parser';
import asyncFunction from 'asyncFunction';
import { FeeLookupRequest } from '../types';
async function sampleFunction (
event: { body: Partial<FeeLookupRequest> }
) {
const body = event.body;
try {
const data = await asyncFunction(body);
return {
statusCode: 200,
body: JSON.stringify(data)
};
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify(error)
};
}
}
export const handler = middy(sampleFunction)
.use(jsonBodyParser())
.use(httpErrorHandler());
If this handler would be without the middleware (export const handler = middy(sampleFunction).use etc) I just simply do my test like this:
import sampleFunction from 'itsRoute';
describe('description'. ()0> {
it('without middleware', () => {
const asyncFunction = jest.fn();
// then a mock of the response and a mock of the input
const response = sampleFunction(mockedInput)
expect(response).toEqual(mockedResponse);
But here, by using export const handler = middy(sampleFunction).use etc I'm not sure how should I test, because I can't import directly the sampleFunction so how will I get the response of the handler?
Becase, I will import handler but it's the result of another function called middy that receives as parameter sampleFunction, so how should mock