'Class extends value undefined is not a constructor or null when testing in jest
I'm trying to create a base class that contains common logic between multiple AWS Lambdas. However, my test fails with:
Class extends value undefined is not a constructor or null
I tried to create a base handler like this:
import { APIGatewayEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { Logger } from '..';
export abstract class BaseHandler<T extends APIGatewayEvent> {
abstract process(event: T, context: Context): Promise<APIGatewayProxyResult>;
public handle = async (event: T, context: Context): Promise<APIGatewayProxyResult> => {
Logger.debug(`Received event: ${JSON.stringify(event)}`);
Logger.debug(`Received context: ${JSON.stringify(context)}`);
try {
const result: APIGatewayProxyResult = await this.process(event, context);
Logger.debug(`Returning response: ${JSON.stringify(result)}`);
return result;
} catch (e) {
Logger.error(`Caught error in handler: ${JSON.stringify(e)}`);
const errorResponse: APIGatewayProxyResult = {
statusCode: statusCode,
body: JSON.stringify({
code: e.statusCode,
message: e.message
})
};
return errorResponse;
}
};
}
module.exports.BaseHandler;
And then a specific lambda that inherits this:
import { BaseHandler } from '../base-handler.abstract.class';
import { APIGatewayProxyResult, Context } from 'aws-lambda';
import { InheritedLambdaRequest } from '../types/inherited-lambda-request.type';
class InheritedLambda extends BaseHandler<InheritedLambdaRequest> {
constructor() {
super();
}
public process = async (event: InheritedLambdaRequest, context: Context): Promise<APIGatewayProxyResult> => {
//business logic
}
}
export const handler = new InheritedLambda();
export const inheritedLambda = handler.process;
However, my jest test fails:
import { inheritedLambda } from './inherited-lambda';
describe('Handle Inherited Lambda', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return 204', (done) => {
inheritedLambda(request, undefined).then((result) => {
expect(result.statusCode).toEqual(204);
expect(result.body).toEqual('');
done();
});
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
